Skip to content

Commit 70b8df0

Browse files
Merge pull request #6031 from BitGo/WIN-5253
test(sdk-coin-icp): added test cases for verifyTransaction(), explainTransaction() and signTransaction()
2 parents 0cf8c47 + e96596a commit 70b8df0

File tree

3 files changed

+145
-43
lines changed

3 files changed

+145
-43
lines changed

modules/sdk-coin-icp/test/resources/icp.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,17 @@ export const IcpTransactionData = {
9898
};
9999

100100
export const RawTransaction = {
101-
serializedTxHex:
102-
'b90002677570646174657381826b5452414e53414354494f4eb900056b63616e69737465725f69644a000000000000000201016b6d6574686f645f6e616d656773656e645f70626361726758420a0308d20912060a0408c0843d1a0308904e2a220a20c3d30f404955975adaba89f2e1ebc75c1f44a6a204578afce8f3780d64fe252e3a0a0880bef0becffb919a186673656e646572581dd5fc1dc4d74d4aa35d81cf345533d20548113412d32fffdcece2f68a026e696e67726573735f6578706972791b000000000000000070696e67726573735f6578706972696573811b18344814d8f97f00',
101+
serializedTxHex: {
102+
payloads: [
103+
{
104+
account_identifier: { address: '0af815da8259ba8bb3d34fbfb2ac730f07a1adc81438d40d667d91b408b25f2f' },
105+
hex_bytes: '0a69632d7265717565737499ab9106ce953020710fd01193ffdc526a3fcdb4072f711f99bdd267c17cae8b',
106+
signature_type: 'ecdsa',
107+
},
108+
],
109+
unsigned_transaction:
110+
'b90002677570646174657381826b5452414e53414354494f4eb900056b63616e69737465725f69644a000000000000000201016b6d6574686f645f6e616d656773656e645f70626361726758410a02080012060a0408a08d061a0308904e2a220a20c3d30f404955975adaba89f2e1ebc75c1f44a6a204578afce8f3780d64fe252e3a0a0880e2b0a0f4ddcc9c186673656e646572581dd5fc1dc4d74d4aa35d81cf345533d20548113412d32fffdcece2f68a026e696e67726573735f6578706972791b000000000000000070696e67726573735f6578706972696573811b1839332725299100',
111+
},
103112
publicKey:
104113
'042ab77b959e28c4fa47fa8fb9e57cec3d66df5684d076ac2e4c5f28fd69a23dd31a59f908c8add51eab3530b4ac5d015166eaf2198c52fa9a8df7cfaeb8fdb7d4',
105114
};

modules/sdk-coin-icp/test/unit/icp.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
22
import { BitGoAPI } from '@bitgo/sdk-api';
33
import utils from '../../src/lib/utils';
4+
import { getBuilderFactory } from './getBuilderFactory';
45

56
import { Icp, Ticp } from '../../src/index';
67
import nock from 'nock';
8+
import * as testData from '../resources/icp';
9+
import assert from 'assert';
10+
import should from 'should';
711
nock.enableNetConnect();
812

913
const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' });
@@ -12,13 +16,23 @@ bitgo.safeRegister('ticp', Ticp.createInstance);
1216
describe('Internet computer', function () {
1317
let bitgo;
1418
let basecoin;
19+
const factory = getBuilderFactory('ticp');
20+
let txBuilder: any;
1521

16-
before(function () {
22+
before(async function () {
1723
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
1824
bitgo.safeRegister('icp', Icp.createInstance);
1925
bitgo.safeRegister('ticp', Ticp.createInstance);
2026
bitgo.initializeTestVars();
2127
basecoin = bitgo.coin('ticp');
28+
29+
txBuilder = factory.getTransferBuilder();
30+
txBuilder.sender(testData.Accounts.account1.address, testData.Accounts.account1.publicKey);
31+
txBuilder.receiverId(testData.Accounts.account2.address);
32+
txBuilder.amount('10');
33+
txBuilder.memo(testData.MetaDataWithMemo.memo);
34+
35+
await txBuilder.build();
2236
});
2337

2438
after(function () {
@@ -90,6 +104,7 @@ describe('Internet computer', function () {
90104
.should.be.rejectedWith(`Invalid hex-encoded public key format.`);
91105
});
92106
});
107+
93108
describe('Generate wallet key pair: ', () => {
94109
it('should generate key pair', () => {
95110
const kp = basecoin.generateKeyPair();
@@ -104,4 +119,58 @@ describe('Internet computer', function () {
104119
basecoin.isValidPrv(kp.prv).should.equal(true);
105120
});
106121
});
122+
123+
describe('Sign a raw txn with a private key', () => {
124+
it('should sign a raw txn with a private key', async () => {
125+
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
126+
unsignedTxn.should.be.a.String();
127+
const payloadsData = txBuilder.transaction.payloadsData;
128+
const serializedTxFormat = {
129+
serializedTxHex: payloadsData,
130+
publicKey: testData.Accounts.account1.publicKey,
131+
};
132+
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
133+
const signedTxn = await basecoin.signTransaction({
134+
txPrebuild: {
135+
txHex: serializedTxHex,
136+
},
137+
prv: testData.Accounts.account1.secretKey,
138+
});
139+
signedTxn.should.be.a.string;
140+
const parsedTransaction = await factory.parseTransaction(signedTxn.txHex, true);
141+
should.equal(parsedTransaction.operations[0].account.address, testData.Accounts.account1.address);
142+
should.equal(parsedTransaction.operations[1].account.address, testData.Accounts.account2.address);
143+
should.equal(parsedTransaction.operations[2].account.address, testData.Accounts.account1.address);
144+
should.equal(parsedTransaction.operations[0].amount.value, '-10');
145+
should.equal(parsedTransaction.account_identifier_signers[0].address, testData.Accounts.account1.address);
146+
});
147+
});
148+
149+
describe('Verify a transaction', () => {
150+
it('should successfully verify a transaction', async () => {
151+
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
152+
unsignedTxn.should.be.a.String();
153+
const payloadsData = txBuilder.transaction.payloadsData;
154+
const serializedTxFormat = {
155+
serializedTxHex: payloadsData,
156+
publicKey: testData.Accounts.account1.publicKey,
157+
};
158+
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
159+
const txParams = {
160+
recipients: [
161+
{
162+
address: testData.Accounts.account2.address,
163+
amount: '10',
164+
},
165+
],
166+
};
167+
const response = await basecoin.verifyTransaction({
168+
txPrebuild: {
169+
txHex: serializedTxHex,
170+
},
171+
txParams: txParams,
172+
});
173+
assert(response);
174+
});
175+
});
107176
});
Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { Transaction } from '../../src';
22
import { coins } from '@bitgo/statics';
33
import assert from 'assert';
4-
// import should from 'should';
4+
import should from 'should';
55
import { Utils } from '../../src/lib/utils';
66
import { InvalidTransactionError } from '@bitgo/sdk-core';
7-
// import { rawTransaction, accounts, parsedRawTransaction } from '../resources/icp';
7+
import * as testData from '../resources/icp';
8+
import { getBuilderFactory } from './getBuilderFactory';
89
import sinon from 'sinon';
910

1011
describe('ICP Transaction', () => {
1112
let tx: Transaction;
1213
let utils: Utils;
13-
// let localRawTransaction: any;
14+
let serializedTxHex: any;
1415
const config = coins.get('ticp');
1516

1617
beforeEach(() => {
1718
utils = new Utils();
1819
tx = new Transaction(config, utils);
19-
// localRawTransaction = JSON.stringify(rawTransaction);
20+
const serializedTxFormat = {
21+
serializedTxHex: testData.PayloadsData,
22+
publicKey: testData.Accounts.account1.publicKey,
23+
};
24+
serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
2025
sinon.stub(utils, 'validateExpireTime').returns(true);
2126
});
2227

@@ -35,42 +40,61 @@ describe('ICP Transaction', () => {
3540
});
3641
});
3742

38-
// describe('from raw transaction', () => {
39-
// it('build a json transaction from raw hex', async () => {
40-
// await tx.fromRawTransaction(localRawTransaction);
41-
// const json = tx.toJson();
42-
// should.equal(json.memo, parsedRawTransaction.metadata.memo);
43-
// should.equal(json.feeAmount, parsedRawTransaction.operations[2].amount.value);
44-
// should.equal(json.sender, parsedRawTransaction.operations[0].account.address);
45-
// should.equal(json.recipient, parsedRawTransaction.operations[1].account.address);
46-
// should.equal(json.type, BitGoTransactionType.Send);
47-
// should.equal(json.senderPublicKey, accounts.account1.publicKey);
48-
// });
49-
// });
43+
describe('build a txn from init() method', () => {
44+
it('start and build a txn with builder init method', async () => {
45+
const txn = new Transaction(config, utils);
46+
txn.icpTransactionData = testData.IcpTransactionData;
47+
const factory = getBuilderFactory('ticp');
48+
const txBuilder = factory.getTransferBuilder();
49+
txBuilder.initBuilder(txn);
50+
await txBuilder.build();
5051

51-
// describe('Explain', () => {
52-
// it('explain transaction', async () => {
53-
// await tx.fromRawTransaction(localRawTransaction);
54-
// const explain = tx.explainTransaction();
52+
const icpTransaction = txBuilder.transaction.icpTransaction;
53+
const payloadsData = txBuilder.transaction.payloadsData;
54+
should.equal(icpTransaction.metadata.memo, testData.IcpTransactionData.memo);
55+
should.equal(icpTransaction.operations[0].account.address, testData.IcpTransactionData.senderAddress);
56+
should.equal(icpTransaction.operations[1].account.address, testData.IcpTransactionData.receiverAddress);
57+
should.equal(icpTransaction.operations[1].amount.value, testData.IcpTransactionData.amount);
58+
should.equal(icpTransaction.operations[2].amount.value, testData.IcpTransactionData.fee);
59+
should.equal(icpTransaction.public_keys[0].hex_bytes, testData.IcpTransactionData.senderPublicKeyHex);
60+
payloadsData.payloads.should.be.an.Array();
61+
payloadsData.payloads.length.should.equal(1);
62+
});
63+
});
64+
65+
describe('from raw transaction', () => {
66+
it('build a json transaction from raw hex', async () => {
67+
await tx.fromRawTransaction(serializedTxHex);
68+
const json = tx.toJson();
69+
should.equal(json.memo, testData.ParsedRawTransaction.metadata.memo);
70+
should.equal(json.feeAmount, testData.ParsedRawTransaction.operations[2].amount.value);
71+
should.equal(json.sender, testData.ParsedRawTransaction.operations[0].account.address);
72+
should.equal(json.recipient, testData.ParsedRawTransaction.operations[1].account.address);
73+
should.equal(json.senderPublicKey, testData.Accounts.account1.publicKey);
74+
should.equal(json.id, testData.OnChainTransactionHash);
75+
});
76+
});
77+
78+
describe('Explain', () => {
79+
it('explain transaction', async () => {
80+
await tx.fromRawTransaction(serializedTxHex);
81+
const explain = tx.explainTransaction();
5582

56-
// explain.outputAmount.should.equal('1000000');
57-
// explain.outputs[0].amount.should.equal('1000000');
58-
// explain.outputs[0].address.should.equal(accounts.account2.address);
59-
// explain.fee.fee.should.equal('-10000');
60-
// explain.changeAmount.should.equal('0');
61-
// if (explain.displayOrder !== undefined) {
62-
// explain.displayOrder.should.deepEqual([
63-
// 'id',
64-
// 'outputAmount',
65-
// 'changeAmount',
66-
// 'outputs',
67-
// 'changeOutputs',
68-
// 'fee',
69-
// ]);
70-
// }
71-
// if (explain.type !== undefined) {
72-
// explain.type.should.equal(BitGoTransactionType.Send);
73-
// }
74-
// });
75-
// });
83+
explain.outputAmount.should.equal('10');
84+
explain.outputs[0].amount.should.equal('10');
85+
explain.outputs[0].address.should.equal(testData.Accounts.account2.address);
86+
explain.fee.fee.should.equal('-10000');
87+
explain.changeAmount.should.equal('0');
88+
if (explain.displayOrder !== undefined) {
89+
explain.displayOrder.should.deepEqual([
90+
'id',
91+
'outputAmount',
92+
'changeAmount',
93+
'outputs',
94+
'changeOutputs',
95+
'fee',
96+
]);
97+
}
98+
});
99+
});
76100
});

0 commit comments

Comments
 (0)