Search in sources :

Example 6 with Bytes

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);
}
Also used : org.starcoin.types(org.starcoin.types) Bytes(com.novi.serde.Bytes) File(java.io.File) SneakyThrows(lombok.SneakyThrows)

Example 7 with Bytes

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);
}
Also used : BcsSerializer(com.novi.bcs.BcsSerializer) List(java.util.List) Bytes(com.novi.serde.Bytes) SneakyThrows(lombok.SneakyThrows) BigInteger(java.math.BigInteger) AccountAddress(org.starcoin.types.AccountAddress) Collectors(java.util.stream.Collectors) BcsSerializer(com.novi.bcs.BcsSerializer) Bytes(com.novi.serde.Bytes) SneakyThrows(lombok.SneakyThrows)

Example 8 with 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);
}
Also used : Bytes(com.novi.serde.Bytes) Test(org.junit.Test) SneakyThrows(lombok.SneakyThrows)

Example 9 with Bytes

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))));
}
Also used : TypeObj(org.starcoin.bean.TypeObj) SneakyThrows(lombok.SneakyThrows) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BigDecimal(java.math.BigDecimal) BcsSerializer(com.novi.bcs.BcsSerializer) Ed25519PrivateKey(org.starcoin.types.Ed25519PrivateKey) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Lists(com.google.common.collect.Lists) Bytes(com.novi.serde.Bytes) ScriptFunctionObj(org.starcoin.bean.ScriptFunctionObj) JSONObject(com.alibaba.fastjson.JSONObject) ResourceObj(org.starcoin.bean.ResourceObj) BigInteger(java.math.BigInteger) AccountAddress(org.starcoin.types.AccountAddress) Joiner(com.google.common.base.Joiner) RoundingMode(java.math.RoundingMode) BcsSerializer(com.novi.bcs.BcsSerializer) Bytes(com.novi.serde.Bytes) BigInteger(java.math.BigInteger) SneakyThrows(lombok.SneakyThrows)

Example 10 with Bytes

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;
}
Also used : StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Package(org.starcoin.types.Package) List(java.util.List) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Bytes(com.novi.serde.Bytes) JsonParser(com.fasterxml.jackson.core.JsonParser) org.starcoin.types(org.starcoin.types) Optional(java.util.Optional) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) JsonNode(com.fasterxml.jackson.databind.JsonNode) Bytes(com.novi.serde.Bytes) Package(org.starcoin.types.Package)

Aggregations

Bytes (com.novi.serde.Bytes)10 SneakyThrows (lombok.SneakyThrows)6 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AccountAddress (org.starcoin.types.AccountAddress)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 BcsSerializer (com.novi.bcs.BcsSerializer)2 BigInteger (java.math.BigInteger)2 Test (org.junit.Test)2 org.starcoin.types (org.starcoin.types)2 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)1 Joiner (com.google.common.base.Joiner)1 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 IOException (java.io.IOException)1