Beispiel #1
0
        public async Task SerializeAndSignMultiActionTx()
        {
            var keyStore = new InMemoryKeyStore();
            var keyPair  = KeyPair.FromString("ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw");
            await keyStore.SetKeyAsync("test", "test.near", keyPair);

            var publicKey = keyPair.GetPublicKey();

            var actions = new[]
            {
                Action.CreateAccount(),
                Action.DeployContract(new byte[] { 1, 2, 3 }),
                Action.FunctionCall("qqq", new byte[] { 1, 2, 3 }, 1000, 1000000),
                Action.Transfer(123),
                Action.Stake(1000000, publicKey),
                Action.AddKey(publicKey, AccessKey.FunctionCallAccessKey("zzz", new [] { "www" }, null)),
                Action.DeleteKey(publicKey),
                Action.DeleteAccount("123")
            };

            var blockHash = new ByteArray32()
            {
                Buffer = Base58.Decode("244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM")
            };
            var signedTransaction = await SignedTransaction.SignTransactionAsync("123", 1, actions, blockHash, new InMemorySigner(keyStore), "test.near", "test");

            const string expected = "Fo3MJ9XzKjnKuDuQKhDAC6fra5H2UWawRejFSEpPNk3Y";
            var          actual   = Base58.Encode(signedTransaction.Item1);

            Assert.AreEqual(expected, actual);
        }
Beispiel #2
0
        private static Transaction FromRawDataStream(MemoryStream stream)
        {
            using (var reader = new NearBinaryReader(stream, true))
            {
                var signerId   = reader.ReadString();
                var publicKey  = PublicKey.FromStream(ref stream);
                var nonce      = reader.ReadULong();
                var receiverId = reader.ReadString();
                var blockHash  = new ByteArray32()
                {
                    Buffer = reader.ReadBytes(32)
                };
                var actionsCount = reader.ReadUInt();
                var actions      = new List <Action>();

                for (var i = 0; i < actionsCount; i++)
                {
                    actions.Add(Action.FromStream(ref stream));
                }

                return(new Transaction()
                {
                    SignerId = signerId,
                    PublicKey = publicKey,
                    Nonce = nonce,
                    ReceiverId = receiverId,
                    BlockHash = blockHash,
                    Actions = actions.ToArray()
                });
            }
        }
Beispiel #3
0
        public static async Task <Tuple <byte[], SignedTransaction> > SignTransactionAsync(string receiverId, ulong nonce, Action[] actions, ByteArray32 blockHash, Signer signer, string accountId)
        {
            var publicKey = await signer.GetPublicKeyAsync(accountId);

            var transaction = new Transaction
            {
                SignerId   = accountId,
                PublicKey  = publicKey,
                Nonce      = nonce,
                ReceiverId = receiverId,
                Actions    = actions,
                BlockHash  = blockHash
            };
            var message = transaction.ToByteArray();

            byte[] hash;
            using (var sha256 = SHA256.Create())
            {
                hash = sha256.ComputeHash(message);
            }

            var signature = await signer.SignMessageAsync(message, accountId);

            var signedTx = new SignedTransaction
            {
                Transaction = transaction,
                Signature   = new NearSignature(signature.SignatureBytes)
            };

            var result = new Tuple <byte[], SignedTransaction>(message, signedTx);

            return(result);
        }