-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathicp.ts
More file actions
236 lines (209 loc) · 8.93 KB
/
icp.ts
File metadata and controls
236 lines (209 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
import { BitGoAPI } from '@bitgo/sdk-api';
import utils from '../../src/lib/utils';
import { getBuilderFactory } from './getBuilderFactory';
import { Icp, Ticp } from '../../src/index';
import nock from 'nock';
import * as testData from '../resources/icp';
import assert from 'assert';
import should from 'should';
nock.enableNetConnect();
const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' });
bitgo.safeRegister('icp', Icp.createInstance);
describe('Internet computer', function () {
let bitgo;
let basecoin;
const factory = getBuilderFactory('icp');
let txBuilder: any;
before(async function () {
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
bitgo.safeRegister('icp', Icp.createInstance);
bitgo.safeRegister('ticp', Ticp.createInstance);
bitgo.initializeTestVars();
basecoin = bitgo.coin('icp');
txBuilder = factory.getTransferBuilder();
txBuilder.sender(testData.Accounts.account1.address, testData.Accounts.account1.publicKey);
txBuilder.receiverId(testData.Accounts.account2.address);
txBuilder.amount('10');
txBuilder.memo(testData.MetaDataWithMemo.memo);
await txBuilder.build();
});
after(function () {
nock.pendingMocks().should.be.empty();
nock.cleanAll();
});
it('should return the right info', function () {
const icp = bitgo.coin('icp');
const ticp = bitgo.coin('ticp');
icp.getChain().should.equal('icp');
icp.getFamily().should.equal('icp');
icp.getFullName().should.equal('Internet Computer');
icp.getBaseFactor().should.equal(1e8);
icp.supportsTss().should.equal(true);
ticp.getChain().should.equal('ticp');
ticp.getFamily().should.equal('icp');
ticp.getFullName().should.equal('Testnet Internet Computer');
ticp.getBaseFactor().should.equal(1e8);
icp.supportsTss().should.equal(true);
});
describe('Address creation', () => {
const hexEncodedPublicKey =
'047a83e378053f87b49aeae53b3ed274c8b2ffbe59d9a51e3c4d850ca8ac1684f7131b778317c0db04de661c7d08321d60c0507868af41fe3150d21b3c6c757367';
const hexEncodedPublicKey2 = '02ad010ce68b75266c723bf25fbe3a0c48eb29f14b25925b06b7f5026a0f12702e';
const invalidPublicKey = 'invalid-public-key';
const validAccountID = '8b84c3a3529d02a9decb5b1a27e7c8d886e17e07ea0a538269697ef09c2a27b4';
const validAccountID2 = '2b9b89604362e185544c8bba76cadff1a3af26e1467e8530d13743a08a52dd7b';
it('should return true when validating a hex encoded public key', function () {
basecoin.isValidPub(hexEncodedPublicKey).should.equal(true);
});
it('should return true when validating a hex encoded public key with 33 bytes ', function () {
basecoin.isValidPub(hexEncodedPublicKey2).should.equal(true);
});
it('should return false when validating a invalid public key', function () {
basecoin.isValidPub(invalidPublicKey).should.equal(false);
});
it('should return valid address from a valid hex encoded public key', async function () {
const accountID = await basecoin.getAddressFromPublicKey(hexEncodedPublicKey);
accountID.should.deepEqual(validAccountID);
});
it('should return valid address from a valid hex encoded public key with 33 bytes', async function () {
const accountID = await basecoin.getAddressFromPublicKey(hexEncodedPublicKey2);
accountID.should.deepEqual(validAccountID2);
});
it('should throw an error when invalid public key is provided', async function () {
await basecoin
.getAddressFromPublicKey(invalidPublicKey)
.should.be.rejectedWith(`Invalid hex-encoded public key format.`);
});
it('should return valid address from a valid hex encoded public key', async function () {
const accountID = await utils.getAddressFromPublicKey(hexEncodedPublicKey);
accountID.should.deepEqual(validAccountID);
});
it('should throw an error when invalid public key is provided', async function () {
await utils
.getAddressFromPublicKey(invalidPublicKey)
.should.be.rejectedWith(`Invalid hex-encoded public key format.`);
});
});
describe('Generate wallet key pair: ', () => {
it('should generate key pair', () => {
const kp = basecoin.generateKeyPair();
basecoin.isValidPub(kp.pub).should.equal(true);
basecoin.isValidPrv(kp.prv).should.equal(true);
});
it('should generate key pair from seed', () => {
const seed = Buffer.from('9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60', 'hex');
const kp = basecoin.generateKeyPair(seed);
basecoin.isValidPub(kp.pub).should.equal(true);
basecoin.isValidPrv(kp.prv).should.equal(true);
});
});
describe('Sign a raw txn with a private key', () => {
it('should sign a raw txn with a private key', async () => {
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
unsignedTxn.should.be.a.String();
const payloadsData = txBuilder.transaction.payloadsData;
const serializedTxFormat = {
serializedTxHex: payloadsData,
publicKey: testData.Accounts.account1.publicKey,
};
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
const signedTxn = await basecoin.signTransaction({
txPrebuild: {
txHex: serializedTxHex,
},
prv: testData.Accounts.account1.secretKey,
});
signedTxn.should.be.a.string;
const parsedTransaction = await factory.parseTransaction(signedTxn.txHex, true);
should.equal(parsedTransaction.operations[0].account.address, testData.Accounts.account1.address);
should.equal(parsedTransaction.operations[1].account.address, testData.Accounts.account2.address);
should.equal(parsedTransaction.operations[2].account.address, testData.Accounts.account1.address);
should.equal(parsedTransaction.operations[0].amount.value, '-10');
should.equal(parsedTransaction.account_identifier_signers[0].address, testData.Accounts.account1.address);
});
});
describe('Verify a transaction', () => {
it('should successfully verify a transaction with signable Hex', async () => {
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
unsignedTxn.should.be.a.String();
const payloadsData = txBuilder.transaction.payloadsData;
const serializedTxFormat = {
serializedTxHex: payloadsData,
publicKey: testData.Accounts.account1.publicKey,
};
const signableHex = payloadsData.payloads[0].hex_bytes;
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
const txParams = {
recipients: [
{
address: testData.Accounts.account2.address,
amount: '10',
},
],
};
const response = await basecoin.verifyTransaction({
txPrebuild: {
txHex: serializedTxHex,
txInfo: signableHex,
},
txParams: txParams,
});
assert(response);
});
it('should successfully verify a transaction without signable Hex', async () => {
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
unsignedTxn.should.be.a.String();
const payloadsData = txBuilder.transaction.payloadsData;
const serializedTxFormat = {
serializedTxHex: payloadsData,
publicKey: testData.Accounts.account1.publicKey,
};
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
const txParams = {
recipients: [
{
address: testData.Accounts.account2.address,
amount: '10',
},
],
};
const response = await basecoin.verifyTransaction({
txPrebuild: {
txHex: serializedTxHex,
},
txParams: txParams,
});
assert(response);
});
it('should fail to verify a transaction with wrong signable Hex', async () => {
const unsignedTxn = txBuilder.transaction.unsignedTransaction;
unsignedTxn.should.be.a.String();
const payloadsData = txBuilder.transaction.payloadsData;
const serializedTxFormat = {
serializedTxHex: payloadsData,
publicKey: testData.Accounts.account1.publicKey,
};
const serializedTxHex = Buffer.from(JSON.stringify(serializedTxFormat), 'utf-8').toString('hex');
const txParams = {
recipients: [
{
address: testData.Accounts.account2.address,
amount: '10',
},
],
};
const wrongSignableHexValues =
'0a69632d72657175657374523de3c7c5b4613155b74ede2e54493f6acbe8bf6d910154fbbb3a98ba3e0098';
await basecoin
.verifyTransaction({
txPrebuild: {
txHex: serializedTxHex,
txInfo: wrongSignableHexValues,
},
txParams: txParams,
})
.should.rejectedWith('generated signableHex is not equal to params.signableHex');
});
});
});