public async Task <OperationResult <PushTransactionResults> > BroadcastActionsAsync(BaseAction[] baseActions, List <byte[]> privateKeys, CancellationToken token) { var initOpRez = await AbiJsonToBinAsync(baseActions, token).ConfigureAwait(false); if (initOpRez.IsError) { return(new OperationResult <PushTransactionResults>(initOpRez)); } var infoResp = await GetInfoAsync(token).ConfigureAwait(false); if (infoResp.IsError) { return(new OperationResult <PushTransactionResults>(infoResp)); } var info = infoResp.Result; var blockArgs = new GetBlockParams { BlockNumOrId = info.HeadBlockId }; var getBlock = await GetBlockAsync(blockArgs, token).ConfigureAwait(false); if (getBlock.IsError) { return(new OperationResult <PushTransactionResults>(getBlock)); } var block = getBlock.Result; var trx = new SignedTransaction { Actions = baseActions, RefBlockNum = (ushort)(block.BlockNum & 0xffff), RefBlockPrefix = block.RefBlockPrefix, Expiration = block.Timestamp.Value.AddSeconds(30) }; var packedTrx = MessageSerializer.Serialize <SignedTransaction>(trx); var chainId = Hex.HexToBytes(info.ChainId); var msg = new byte[chainId.Length + packedTrx.Length + 32]; Array.Copy(chainId, msg, chainId.Length); Array.Copy(packedTrx, 0, msg, chainId.Length, packedTrx.Length); var sha256 = Sha256Manager.GetHash(msg); var pack = new PackedTransaction { PackedTrx = packedTrx, Signatures = new string[privateKeys.Count], PackedContextFreeData = new Bytes(), Compression = CompressionType.None }; for (var i = 0; i < privateKeys.Count; i++) { var key = privateKeys[i]; var sig = Secp256K1Manager.SignCompressedCompact(sha256, key); var sigHex = Base58.EncodeSig(sig); pack.Signatures[i] = sigHex; } return(await PushTransactionAsync(pack, token).ConfigureAwait(false)); }
public async Task <PackedTransaction> CreatePackedTransaction(CreateTransactionArgs args, CancellationToken token) { //1 var infoResp = await GetInfo(token); if (infoResp.IsError) { return(null); } var info = infoResp.Result; //2 var blockArgs = new GetBlockParams { BlockNumOrId = info.HeadBlockId }; var getBlock = await GetBlock(blockArgs, token); if (getBlock.IsError) { return(null); } var block = getBlock.Result; //3 var transaction = new SignedTransaction { RefBlockNum = (ushort)(block.BlockNum & 0xffff), RefBlockPrefix = block.RefBlockPrefix, Expiration = block.Timestamp.Value.AddSeconds(30), Actions = args.Actions }; var packedTrx = MessageSerializer.Serialize <SignedTransaction>(transaction); var chainId = Hex.HexToBytes(info.ChainId); var msg = new byte[chainId.Length + packedTrx.Length + 32]; Array.Copy(chainId, msg, chainId.Length); Array.Copy(packedTrx, 0, msg, chainId.Length, packedTrx.Length); var sha256 = Sha256Manager.GetHash(msg); transaction.Signatures = new string[args.PrivateKeys.Count]; for (var i = 0; i < args.PrivateKeys.Count; i++) { var key = args.PrivateKeys[i]; var sig = Secp256K1Manager.SignCompressedCompact(sha256, key); var sigHex = Base58.EncodeSig(sig); transaction.Signatures[i] = sigHex; } return(new PackedTransaction { PackedTrx = Hex.ToString(packedTrx), Signatures = transaction.Signatures, PackedContextFreeData = "", Compression = "none" }); }