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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations