Search in sources :

Example 1 with Bytes

use of com.novi.serde.Bytes in project starcoin-search by starcoinorg.

the class ElasticSearchHandler method buildTransferRequest.

private List<IndexRequest> buildTransferRequest(Transaction transaction, String indexName) {
    List<IndexRequest> requests = new ArrayList<>();
    UserTransaction userTransaction = transaction.getUserTransaction();
    if (userTransaction == null) {
        return requests;
    }
    RawTransaction rawTransaction = userTransaction.getRawTransaction();
    if (rawTransaction == null) {
        return requests;
    }
    TransactionPayload payload = rawTransaction.getTransactionPayload();
    if (!(payload.getClass() == TransactionPayload.ScriptFunction.class)) {
        // todo script and package handle
        logger.warn("other type must handle in future: {}", payload.getClass());
        return requests;
    }
    org.starcoin.types.ScriptFunction function = ((TransactionPayload.ScriptFunction) payload).value;
    String functionName = function.function.value;
    if (functionName.equals("peer_to_peer") || functionName.equals("peer_to_peer_v2")) {
        Transfer transfer = new Transfer();
        transfer.setTxnHash(transaction.getTransactionHash());
        transfer.setSender(rawTransaction.getSender());
        transfer.setTimestamp(transaction.getTimestamp());
        transfer.setIdentifier(functionName);
        transfer.setReceiver(Hex.encode(function.args.get(0)));
        String amount = Hex.encode(function.args.get(1));
        if (functionName.equals("peer_to_peer")) {
            amount = Hex.encode(function.args.get(2));
        }
        transfer.setAmount(amount);
        transfer.setTypeTag(getTypeTags(function.ty_args));
        IndexRequest request = new IndexRequest(indexName);
        request.source(JSON.toJSONString(transfer), XContentType.JSON);
        requests.add(request);
        return requests;
    } else if (functionName.equals("batch_peer_to_peer") || functionName.equals("batch_peer_to_peer_v2")) {
        // batch handle
        Bytes addresses = function.args.get(0);
        byte[] addressBytes = addresses.content();
        byte[] amountBytes;
        if (functionName.equals("batch_peer_to_peer_v2")) {
            amountBytes = function.args.get(1).content();
        } else {
            amountBytes = function.args.get(2).content();
        }
        int size = addressBytes.length / AccountAddress.LENGTH;
        for (int i = 0; i < size; i++) {
            byte[] addressByte = Arrays.copyOfRange(addressBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
            AccountAddress address = AccountAddress.valueOf(addressByte);
            byte[] amountByte = Arrays.copyOfRange(amountBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
            String amount = Hex.encode(amountByte);
            Transfer transfer = new Transfer();
            transfer.setTxnHash(transaction.getTransactionHash());
            transfer.setSender(rawTransaction.getSender());
            transfer.setTimestamp(transaction.getTimestamp());
            transfer.setIdentifier(functionName);
            transfer.setReceiver(address.toString());
            transfer.setAmount(amount);
            transfer.setTypeTag(getTypeTags(function.ty_args));
            IndexRequest request = new IndexRequest(indexName);
            request.source(JSON.toJSONString(transfer), XContentType.JSON);
            requests.add(request);
        }
        logger.info("batch transfer handle: {}", size);
    } else {
        logger.warn("other scripts not support: {}", function.function.value);
    }
    return requests;
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) AccountAddress(org.starcoin.types.AccountAddress) Bytes(com.novi.serde.Bytes) TransactionPayload(org.starcoin.types.TransactionPayload)

Example 2 with Bytes

use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.

the class ScriptFunctionDeserializer method deserialize.

@Override
public ScriptFunction deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    ModuleId module = ParseUtil.parseObject(jsonParser, node.get("module"), ModuleId.class);
    Identifier function = new Identifier(node.get("function").asText());
    List<TypeTag> tyArgs = ParseUtil.parseObjectList(jsonParser, node.get("type_args"), TypeTag.class);
    List<Bytes> args = ParseUtil.parseBytesList(node.get("args"));
    ;
    return new ScriptFunction(module, function, tyArgs, args);
}
Also used : ModuleId(org.starcoin.types.ModuleId) Bytes(com.novi.serde.Bytes) Identifier(org.starcoin.types.Identifier) ScriptFunction(org.starcoin.types.ScriptFunction) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeTag(org.starcoin.types.TypeTag)

Example 3 with Bytes

use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.

the class SignatureUtils method getPublicKey.

@SneakyThrows
public static Ed25519PublicKey getPublicKey(Ed25519PrivateKey privateKey) {
    Ed25519PrivateKeyParameters key = new Ed25519PrivateKeyParameters(privateKey.value.content(), 0);
    Ed25519PublicKeyParameters publicKeyParameters = key.generatePublicKey();
    Ed25519PublicKey publicKey = new Ed25519PublicKey(new Bytes(publicKeyParameters.getEncoded()));
    return publicKey;
}
Also used : Bytes(com.novi.serde.Bytes) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) SneakyThrows(lombok.SneakyThrows)

Example 4 with Bytes

use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.

the class SignatureUtils method signedMessageCheckAccount.

public static boolean signedMessageCheckAccount(SignedMessage signedMessage, ChainId chainId, AccountResource accountResource) throws DeserializationError, SerializationError {
    AuthenticationKey authenticationKeyInMessage = TransactionAuthenticator.authenticationKey(signedMessage.authenticator);
    AuthenticationKey authenticationKeyOnChain;
    if (accountResource == null) {
        if (signedMessage.account.equals(authenticationKeyInMessage.derivedAddress())) {
            return true;
        } else {
            return false;
        }
    } else {
        if (signedMessage.chain_id.equals(chainId)) {
            Bytes serdeBytes = Bytes.fromList(accountResource.authentication_key);
            if (serdeBytes != null) {
                authenticationKeyOnChain = new AuthenticationKey(serdeBytes);
                if (authenticationKeyOnChain.equals(authenticationKeyInMessage)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Bytes(com.novi.serde.Bytes)

Example 5 with Bytes

use of com.novi.serde.Bytes in project starcoin-java by starcoinorg.

the class SignatureUtils method signTxn.

@SneakyThrows
public static SignedUserTransaction signTxn(Ed25519PrivateKey privateKey, RawUserTransaction rawUserTransaction) {
    byte[] bytes = com.google.common.primitives.Bytes.concat(HashUtils.hashWithStarcoinPrefix("RawUserTransaction"), rawUserTransaction.bcsSerialize());
    byte[] signRst = ed25519Sign(privateKey, bytes);
    Ed25519PublicKey publicKey = getPublicKey(privateKey);
    Ed25519Signature signature = new Ed25519Signature(new Bytes(signRst));
    TransactionAuthenticator transactionAuthenticator = new TransactionAuthenticator.Ed25519(publicKey, signature);
    SignedUserTransaction signedUserTransaction = new SignedUserTransaction(rawUserTransaction, transactionAuthenticator);
    return signedUserTransaction;
}
Also used : HashUtils.hashStarcoinSignedUserTransaction(org.starcoin.utils.HashUtils.hashStarcoinSignedUserTransaction) Bytes(com.novi.serde.Bytes) Ed25519(org.starcoin.types.TransactionAuthenticator.Ed25519) SneakyThrows(lombok.SneakyThrows)

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