Skip to content

Commit

Permalink
Add more tests for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Sep 1, 2019
1 parent f58fa44 commit a22809a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/transaction.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/transaction.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/utils/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/utils/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src.ts/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Signature {
}
}

class Transaction extends Assignable {
export class Transaction extends Assignable {
signerId: string;
publicKey: PublicKey;
nonce: number;
Expand Down Expand Up @@ -137,7 +137,7 @@ export class Action extends Enum {
deleteAccount: DeleteAccount;
}

const SCHEMA = new Map<Function, any>([
export const SCHEMA = new Map<Function, any>([
[Signature, {kind: 'struct', fields: [
['keyType', 'u8'],
['data', [32]]
Expand All @@ -151,7 +151,7 @@ const SCHEMA = new Map<Function, any>([
['publicKey', PublicKey],
['nonce', 'u64'],
['receiverId', 'string'],
['blockHash', [32]],
['blockHash', [32]],
['actions', [Action]]
]}],
[PublicKey, { kind: 'struct', fields: [
Expand Down
3 changes: 2 additions & 1 deletion src.ts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import * as network from './network';
import * as serialize from './serialize';
import * as web from './web';

import { KeyPair, KeyPairEd25519 } from './key_pair';
import { PublicKey, KeyPair, KeyPairEd25519 } from './key_pair';

export {
key_pair,
network,
serialize,
web,

PublicKey,
KeyPair,
KeyPairEd25519
};
40 changes: 37 additions & 3 deletions test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ test('serialize object', async () => {
expect(new_value.q).toEqual(new Uint8Array([1, 2, 3]));
});

test('serialize tx', async() => {
test('serialize and sign multi-action tx', async() => {
const keyStore = new nearlib.keyStores.InMemoryKeyStore();
await keyStore.setKey('test', 'test.near', nearlib.utils.KeyPair.fromString('ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw'));
const publicKey = (await keyStore.getKey('test', 'test.near')).publicKey;
const keyPair = nearlib.utils.KeyPair.fromString('ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw');
await keyStore.setKey('test', 'test.near', keyPair);
const publicKey = keyPair.publicKey;
const actions = [
nearlib.transactions.createAccount(),
nearlib.transactions.deployContract(new Uint8Array([1, 2, 3])),
Expand All @@ -39,3 +40,36 @@ test('serialize tx', async() => {
let [hash] = await nearlib.transactions.signTransaction('123', 1, actions, blockHash, new nearlib.InMemorySigner(keyStore), 'test.near', 'test');
expect(nearlib.utils.serialize.base_encode(hash)).toEqual('Fo3MJ9XzKjnKuDuQKhDAC6fra5H2UWawRejFSEpPNk3Y');
});

test('serialize transfer tx', async() => {
const actions = [
nearlib.transactions.transfer(1),
];
const blockHash = nearlib.utils.serialize.base_decode('244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM');
const transaction = new nearlib.transactions.Transaction({
signerId: 'test.near',
publicKey: nearlib.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'),
nonce: 1,
receiverId: 'whatever.near',
actions,
blockHash
});
const serialized = nearlib.utils.serialize.serialize(nearlib.transactions.SCHEMA, transaction);
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6010000000301000000000000000000000000000000');
});

test('serialize and sign transfer tx', async() => {
const keyStore = new nearlib.keyStores.InMemoryKeyStore();
const keyPair = nearlib.utils.KeyPair.fromString('ed25519:3hoMW1HvnRLSFCLZnvPzWeoGwtdHzke34B2cTHM8rhcbG3TbuLKtShTv3DvyejnXKXKBiV7YPkLeqUHN1ghnqpFv');
await keyStore.setKey('test', 'test.near', keyPair);
const actions = [
nearlib.transactions.transfer(1),
];
const blockHash = nearlib.utils.serialize.base_decode('244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM');

let [hash, signedTx] = await nearlib.transactions.signTransaction('whatever.near', 1, actions, blockHash, new nearlib.InMemorySigner(keyStore), 'test.near', 'test');

expect(Buffer.from(signedTx.signature.data).toString('base64')).toEqual('lpqDMyGG7pdV5IOTJVJYBuGJo9LSu0tHYOlEQ+l+HE8i3u7wBZqOlxMQDtpuGRRNp+ig735TmyBwi6HY0CG9AQ==');
const serialized = nearlib.utils.serialize.serialize(nearlib.transactions.SCHEMA, signedTx);
expect(serialized.toString('base64')).toEqual('CQAAAHRlc3QubmVhcgCRez0mjUtY9/7BsVC9aNab4+5dTMOYVeNBU4Rlu3eGDQEAAAAAAAAADQAAAHdoYXRldmVyLm5lYXIPpHP9JpAd8pa+atxMxN800EDvokNSJLaYaRDmMML+9gEAAAADAQAAAAAAAAAAAAAAAAAAAACWmoMzIYbul1Xkg5MlUlgG4Ymj0tK7S0dg6URD6X4cTyLe7vAFmo6XExAO2m4ZFE2n6KDvflObIHCLodjQIb0B');
});

0 comments on commit a22809a

Please sign in to comment.