use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.
the class StarcoinClient method deployContractPackage.
@SneakyThrows
public String deployContractPackage(AccountAddress sender, Ed25519PrivateKey privateKey, String filePath, ScriptFunctionObj initScriptObj) {
String fileExt = filePath.substring(filePath.lastIndexOf(".") + 1);
org.starcoin.types.Package contractPackage;
if ("mv".equalsIgnoreCase(fileExt)) {
org.starcoin.types.ScriptFunction sf = Objects.isNull(initScriptObj) ? null : initScriptObj.toScriptFunction();
byte[] contractBytes = Files.toByteArray(new File(filePath));
org.starcoin.types.Module module = new org.starcoin.types.Module(new Bytes(contractBytes));
contractPackage = new org.starcoin.types.Package(sender, Lists.newArrayList(module), Optional.ofNullable(sf));
} else {
contractPackage = org.starcoin.types.Package.bcsDeserialize(Files.toByteArray(new File(filePath)));
}
TransactionPayload.Package.Builder builder = new TransactionPayload.Package.Builder();
builder.value = contractPackage;
TransactionPayload payload = builder.build();
return submitTransaction(sender, privateKey, payload);
}
use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.
the class BcsSerializeHelper method serializeListToBytes.
@SneakyThrows
public static Bytes serializeListToBytes(List<String> list) {
List<Bytes> bytesList = list.stream().map(s -> serializeVectorU8ToBytes(s)).collect(Collectors.toList());
BcsSerializer s = new BcsSerializer();
s.serialize_len(bytesList.size());
for (Bytes item : bytesList) {
s.serialize_bytes(item);
}
byte[] bytes = s.get_bytes();
return Bytes.valueOf(bytes);
}
use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.
the class SignatureUtilsTest method testSignedMessageWithResource.
@SneakyThrows
@Test
public void testSignedMessageWithResource() {
String privateKeyString = "0x99d4590f7707c8277c9c591ffd56c2381e4e83c2218a00ce8d9745400843faab";
Ed25519PrivateKey privateKey = SignatureUtils.strToPrivateKey(privateKeyString);
String message = "abcded346ddtest";
// String message = "0f616263646564333436646474657374";
Bytes signingBytes = Bytes.valueOf(message.getBytes());
SigningMessage signingMessage = new SigningMessage(signingBytes.toList());
String signedMsg = "0xfab981cf1ee57d043be6f4f80b5575060f61626364656433343664647465737400204a4f7becc8b33af1ad34ed6195ab1109c4793e915759aa0eb26792fed4674f3d40097e0a748706c30de6457261bfc40ca0b83704072fb7614aac5b2643fe30860ed2e256b832e5160cd9da14d0fa183599d5e89b3169c8aa764ff86fc16f115600fd";
String shouldSigned = SignatureUtils.signPersonalMessage(privateKey, Hex.encode(signingMessage.bcsSerialize()));
SignedMessage signedMessage = SignedMessage.bcsDeserialize(Hex.decode(signedMsg));
byte[] sigature = ((TransactionAuthenticator.Ed25519) signedMessage.authenticator).signature.value.content();
assertEquals(shouldSigned, Hex.encode(sigature));
// verify signature
boolean checked = SignatureUtils.signedMessageCheckSignature(signedMessage);
assertTrue(checked);
// verify check account
String resource = "0x205572bd99b2d9e6161d369a745b04bf9afab981cf1ee57d043be6f4f80b55750601fab981cf1ee57d043be6f4f80b55750601fab981cf1ee57d043be6f4f80b5575064c57000000000000180000000000000000fab981cf1ee57d043be6f4f80b55750630b6020000000000180100000000000000fab981cf1ee57d043be6f4f80b5575060100000000000000180200000000000000fab981cf1ee57d043be6f4f80b5575064c57000000000000";
AccountResource accountResource = AccountResource.bcsDeserialize(Hex.decode(resource));
checked = SignatureUtils.signedMessageCheckAccount(signedMessage, new ChainId((byte) 253), accountResource);
assertTrue(checked);
}
use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.
the class StarcoinClientSample method test.
@SneakyThrows
public void test() {
Long l = 92386324538286L;
System.out.println("u64:" + l + ", after:" + Hex.encode(BcsSerializeHelper.serializeU64ToBytes(l)));
String str = "hello";
System.out.println("string:" + str + ", after:" + Hex.encode(BcsSerializeHelper.serializeVectorU8ToBytes(str)));
BigInteger bigInteger = new BigInteger("500000000000000");
System.out.println("u128:" + bigInteger + ", after:" + Hex.encode(BcsSerializeHelper.serializeU128ToBytes(bigInteger)));
List<String> list = Lists.newArrayList("a", "b", "c");
List<Bytes> bytesList = list.stream().map(s -> {
System.out.println("string:" + s + ",after:" + Hex.encode(BcsSerializeHelper.serializeVectorU8ToBytes(s)));
return BcsSerializeHelper.serializeVectorU8ToBytes(s);
}).collect(Collectors.toList());
BcsSerializer s = new BcsSerializer();
s.serialize_len(bytesList.size());
System.out.println(Hex.encode(s.get_bytes()));
for (Bytes item : bytesList) {
s.serialize_bytes(item);
System.out.println(Hex.encode(s.get_bytes()));
}
System.out.println(Hex.encode(s.get_bytes()));
System.out.println("VectorU8(VectorU8):" + Joiner.on(",").join(list) + ",after:" + Hex.encode(BcsSerializeHelper.serializeListToBytes(list)));
String address = "0xf8af03dd08de49d81e4efd9e24c039cc";
System.out.println("address:" + address + ",after:" + Hex.encode(BcsSerializeHelper.serializeAddressToBytes(AccountAddressUtils.create(address))));
}
use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.
the class TransactionPayloadDeserializer method deserialize.
@Override
public TransactionPayload deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
JsonNode typeTagNode = jsonParser.getCodec().readTree(jsonParser);
String typeName = typeTagNode.get("type_name").textValue();
TransactionPayload transactionPayload = null;
JsonNode node = typeTagNode.get("value");
switch(typeName) {
case "Script":
Bytes code = Bytes.valueOf(Hex.decode(node.get("code").textValue()));
List<TypeTag> typeArgs = ParseUtil.parseObjectList(jsonParser, node.get("type_args"), TypeTag.class);
List<Bytes> args = ParseUtil.parseBytesList(node.get("args"));
transactionPayload = new TransactionPayload.Script(new Script(code, typeArgs, args));
break;
case "ScriptFunction":
ModuleId moduleId = ParseUtil.parseObject(jsonParser, node.get("module"), ModuleId.class);
Identifier function = ParseUtil.parseObject(jsonParser, node.get("function"), Identifier.class);
typeArgs = ParseUtil.parseObjectList(jsonParser, node.get("type_args"), TypeTag.class);
args = ParseUtil.parseBytesList(node.get("args"));
transactionPayload = new TransactionPayload.ScriptFunction(new ScriptFunction(moduleId, function, typeArgs, args));
break;
case "Package":
AccountAddress address = AccountAddress.valueOf(Hex.decode(node.get("package_address").textValue()));
List<org.starcoin.types.Module> bytesList = ParseUtil.parseBytesList(node.get("modules")).stream().map(bytes -> new org.starcoin.types.Module(bytes)).collect(Collectors.toList());
Optional<ScriptFunction> scriptFunction;
if (node.has("init_script")) {
scriptFunction = Optional.of(ParseUtil.parseObject(jsonParser, node.get("init_script"), ScriptFunction.class));
} else {
scriptFunction = Optional.empty();
}
transactionPayload = new TransactionPayload.Package(new Package(address, bytesList, scriptFunction));
break;
}
return transactionPayload;
}
Aggregations