-
Notifications
You must be signed in to change notification settings - Fork 276
/
wallet-rpc-test.js
417 lines (342 loc) · 13.7 KB
/
wallet-rpc-test.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* eslint-env mocha */
'use strict';
const {NodeClient,WalletClient} = require('hs-client');
const assert = require('bsert');
const FullNode = require('../lib/node/fullnode');
const Network = require('../lib/protocol/network');
const Mnemonic = require('../lib/hd/mnemonic');
const HDPrivateKey = require('../lib/hd/private');
const Script = require('../lib/script/script');
const Address = require('../lib/primitives/address');
const network = Network.get('regtest');
const mnemonics = require('./data/mnemonic-english.json');
// Commonly used test mnemonic
const phrase = mnemonics[0][1];
const ports = {
p2p: 14331,
node: 14332,
wallet: 14333
};
const node = new FullNode({
network: network.type,
apiKey: 'bar',
walletAuth: true,
memory: true,
port: ports.p2p,
httpPort: ports.node,
workers: true,
plugins: [require('../lib/wallet/plugin')],
env: {
'HSD_WALLET_HTTP_PORT': ports.wallet.toString()
}
});
const nclient = new NodeClient({
port: ports.node,
apiKey: 'bar'
});
const wclient = new WalletClient({
port: ports.wallet,
apiKey: 'bar'
});
const {
treeInterval,
biddingPeriod,
revealPeriod
} = network.names;
describe('Wallet RPC Methods', function() {
this.timeout(15000);
let xpub;
before(async () => {
await node.open();
await nclient.open();
await wclient.open();
// Derive the xpub using the well known
// mnemonic and network's coin type
const mnemonic = Mnemonic.fromPhrase(phrase);
const priv = HDPrivateKey.fromMnemonic(mnemonic);
const type = network.keyPrefix.coinType;
const key = priv.derive(44, true).derive(type, true).derive(0, true);
xpub = key.toPublic();
assert.equal(phrase, [
'abandon', 'abandon', 'abandon', 'abandon',
'abandon', 'abandon', 'abandon', 'abandon',
'abandon', 'abandon', 'abandon', 'about'
].join(' '));
});
after(async () => {
await nclient.close();
await wclient.close();
await node.close();
});
describe('getaddressinfo', () => {
const watchOnlyWalletId = 'foo';
const standardWalletId = 'bar';
// m/44'/5355'/0'/0/{0,1}
const pubkeys = [
Buffer.from('03253ea6d6486d1b9cc3a'
+ 'b01a9a321d65c350c6c26a9c536633e2ef36163316bf2', 'hex'),
Buffer.from('02cd38edb6f9cb4fd7380'
+ '3b49aed97bfa95ef402cac2c34e8f551f8537811d2159', 'hex')
];
// set up the initial testing state
before(async () => {
{
// Set up the testing environment
// by creating a wallet and a watch
// only wallet
const info = await nclient.getInfo();
assert.equal(info.chain.height, 0);
}
{
// Create a watch only wallet using the path
// m/44'/5355'/0' and assert that the wallet
// was properly created
const accountKey = xpub.xpubkey(network.type);
const response = await wclient.createWallet(watchOnlyWalletId, {
watchOnly: true,
accountKey: accountKey
});
assert.equal(response.id, watchOnlyWalletId);
const wallet = wclient.wallet(watchOnlyWalletId);
const info = await wallet.getAccount('default');
assert.equal(info.accountKey, accountKey);
assert.equal(info.watchOnly, true);
}
{
// Create a wallet that manages the private keys itself
const response = await wclient.createWallet(standardWalletId);
assert.equal(response.id, standardWalletId);
const info = await wclient.getAccount(standardWalletId, 'default');
assert.equal(info.watchOnly, false);
};
});
// the rpc interface requires the wallet to be selected first
it('should return iswatchonly correctly', async () => {
// m/44'/5355'/0'/0/0
const receive = 'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8';
{
await wclient.execute('selectwallet', [standardWalletId]);
const response = await wclient.execute('getaddressinfo', [receive]);
assert.equal(response.iswatchonly, false);
}
{
await wclient.execute('selectwallet', [watchOnlyWalletId]);
const response = await wclient.execute('getaddressinfo', [receive]);
assert.equal(response.iswatchonly, true);
}
});
it('should return the correct address', async () => {
// m/44'/5355'/0'/0/0
const receive = 'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8';
await wclient.execute('selectwallet', [watchOnlyWalletId]);
const response = await wclient.execute('getaddressinfo', [receive]);
assert.equal(response.address, receive);
});
it('should detect owned address', async () => {
// m/44'/5355'/0'/0/0
const receive = 'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8';
{
await wclient.execute('selectwallet', [watchOnlyWalletId]);
const response = await wclient.execute('getaddressinfo', [receive]);
assert.equal(response.ismine, true);
}
{
await wclient.execute('selectwallet', [standardWalletId]);
const response = await wclient.execute('getaddressinfo', [receive]);
assert.equal(response.ismine, false);
}
});
it('should return the correct program for a p2pkh address', async () => {
// m/44'/5355'/0'/0/0
const receive = 'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8';
const address = Address.fromString(receive);
const addr = address.toString(network);
await wclient.execute('selectwallet', [watchOnlyWalletId]);
const response = await wclient.execute('getaddressinfo', [addr]);
assert.equal(response.witness_program, address.hash.toString('hex'));
});
it('should detect a p2wsh and its witness program', async () => {
const script = Script.fromMultisig(2, 2, pubkeys);
const address = Address.fromScript(script);
const addr = address.toString(network);
const response = await wclient.execute('getaddressinfo', [addr]);
assert.equal(response.isscript, true);
assert.equal(response.witness_program, address.hash.toString('hex'));
});
it('should detect ismine up to the lookahead', async () => {
const info = await wclient.getAccount(watchOnlyWalletId, 'default');
await wclient.execute('selectwallet', [watchOnlyWalletId]);
const addresses = [
'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8', // 0/0
'rs1qwkzdtg56m5zqu4wuwtwuqltn4s9uxd3s93ec2n', // 0/1
'rs1q9atns2u4ayv2hsug0xuha3acqxz450mgvaer4z', // 0/2
'rs1qve2kd3lyhnm6r7knzvl3s5pkgem9a25xk42y5d', // 0/3
'rs1q799vpn8u7524p54kaumclcfuayxxkuga84xle2', // 0/4
'rs1q748rcdq767cxla4d0gkpc35r4cl6kk72z47qdq', // 0/5
'rs1qq7fkaj2ruwcdsdr4l2f4jx0a8nqyg9qdr7y6am', // 0/6
'rs1qm8jx0q9y2tq990tswes08gnjhfhej9vfjcql92', // 0/7
'rs1qf3zef3m8tnl8el5wurtrg5qgt6c2aepu7pqeqc', // 0/8
'rs1qermzxwthx9tz2h64fgmmxwc8k05zhxhfhx5khm', // 0/9
'rs1qlvysusse4qgym5s7mv8ddaatgvgfq6g6vcjhvf' // 0/10
];
// Assert that the lookahead is configured as expected
// subtract one from addresses.length, it is 0 indexed
assert.equal(addresses.length - 1, info.lookahead);
// Each address through the lookahead number should
// be recognized as an owned address
for (let i = 0; i < info.lookahead+1; i++) {
const address = addresses[i];
const response = await wclient.execute('getaddressinfo', [address]);
assert.equal(response.ismine, true);
}
// m/44'/5355'/0'/11
// This address is outside of the lookahead range
const failed = 'rs1qg8h0n5mrdt5u8jaxq9jequ3nekj8fg820lr5xg';
const response = await wclient.execute('getaddressinfo', [failed]);
assert.equal(response.ismine, false);
});
it('should detect change addresses', async () => {
await wclient.execute('selectwallet', [watchOnlyWalletId]);
// m/44'/5355'/0'/1/0
const address = 'rs1qxps2ljf5604tgyz7pvecuq6twwt4k9qsxcd27y';
const info = await wclient.execute('getaddressinfo', [address]);
assert.equal(info.ischange, true);
});
it('should throw for the wrong network', async () => {
// m/44'/5355'/0'/0/0
const failed = 'hs1q4rvs9pp9496qawp2zyqpz3s90fjfk362rl50q4';
const fn = async () => await wclient.execute('getaddressinfo', [failed]);
await assert.rejects(fn, 'Invalid address.');
});
it('should throw for invalid address', async () => {
let failed = 'rs1q4rvs9pp9496qawp2zyqpz3s90fjfk362q92vq8';
// remove the first character
failed = failed.slice(1, failed.length);
const fn = async () => await wclient.execute('getaddressinfo', [failed]);
await assert.rejects(fn, 'Invalid address.');
});
});
describe('signmessage', function() {
const nonWalletAddress = 'rs1q7q3h4chglps004u3yn79z0cp9ed24rfrhvrxnx';
const message = 'This is just a test message';
it('should signmessage with address', async () => {
await wclient.execute('selectwallet', ['primary']);
const address = await wclient.execute('getnewaddress');
const signature = await wclient.execute('signmessage', [
address,
message
]);
const verify = await nclient.execute('verifymessage', [
address,
signature,
message
]);
assert.strictEqual(verify, true);
});
it('should fail with invalid address', async () => {
await assert.rejects(async () => {
await wclient.execute('signmessage', [
'invalid address format',
message
]);
}, {
type: 'RPCError',
message: 'Invalid address.'
});
});
it('should fail with non-wallet address.', async () => {
await assert.rejects(async () => {
await wclient.execute('signmessage', [
nonWalletAddress,
message
]);
}, {
type: 'RPCError',
message: 'Address not found.'
});
});
it('should verify an externally signed message', async () => {
// Created with node RPC signmessagewithprivkey, private key:
// ERYZra8yTpNWNXsd1b3YdnRbZByykPg62TFM7KeXMqF7C2x4EvyW
const address = 'rs1qapm5cx5j60nhyp6mmzx23xrk66yj3atpvj63ek';
const message = 'Handshake is for friends!';
const signature = 'ymnS7vTpo+IBWliXWnubTD7UX2aTbzLT5qg5btFxNlYBq' +
'kdCJyji19FcpkINo+JQvJWgIwIq0IPybTPMBTlrJA==';
const verify = await nclient.execute('verifymessage', [
address,
signature,
message
]);
assert.strictEqual(verify, true);
});
it('should get wallet info', async () => {
const info = await wclient.execute('getwalletinfo', []);
assert.strictEqual(info.walletid, 'primary');
assert.strictEqual(info.height, node.chain.height);
});
});
describe('Wallet RPC Auction', function() {
let addr1, addr2, name1, name2;
it('should create wallets', async () => {
await wclient.createWallet('wallet1');
await wclient.createWallet('wallet2');
});
it('should get wallet addresses', async () => {
await wclient.execute('selectwallet', ['wallet1']);
addr1 = await wclient.execute('getnewaddress', []);
await wclient.execute('selectwallet', ['wallet2']);
addr2 = await wclient.execute('getnewaddress', []);
});
it('should fund wallets', async () => {
await nclient.execute('generatetoaddress', [10, addr1]);
await nclient.execute('generatetoaddress', [10, addr2]);
});
it('should open names', async () => {
name1 = await nclient.execute('grindname', [5]);
name2 = await nclient.execute('grindname', [5]);
await wclient.execute('selectwallet', ['wallet1']);
await wclient.execute('sendopen', [name1]);
await wclient.execute('sendopen', [name2]);
// confirm and advance to bidding phase
await nclient.execute('generatetoaddress', [treeInterval + 1, addr1]);
});
it('should bid on names', async () => {
// wallet1 will win name1
await wclient.execute('selectwallet', ['wallet1']);
await wclient.execute('sendbid', [name1, 10, 10]);
await wclient.execute('sendbid', [name2, 5, 5]);
// wallet2 will win name2
await wclient.execute('selectwallet', ['wallet2']);
await wclient.execute('sendbid', [name1, 5, 5]);
await wclient.execute('sendbid', [name2, 10, 10]);
// confirm and advance to reveal phase
await nclient.execute('generatetoaddress', [biddingPeriod + 1, addr1]);
});
it('should reveal names', async () => {
await wclient.execute('selectwallet', ['wallet1']);
await wclient.execute('sendreveal', []);
await wclient.execute('selectwallet', ['wallet2']);
await wclient.execute('sendreveal', []);
// confirm and advance to close auction
await nclient.execute('generatetoaddress', [revealPeriod + 1, addr1]);
});
it('should get all wallet names', async () => {
await wclient.execute('selectwallet', ['wallet1']);
const wallet1AllNames = await wclient.execute('getnames', []);
await wclient.execute('selectwallet', ['wallet2']);
const wallet2AllNames = await wclient.execute('getnames', []);
assert.strictEqual(wallet1AllNames.length, 2);
assert.deepStrictEqual(wallet1AllNames, wallet2AllNames);
});
it('should only get wallet-owned names', async () => {
await wclient.execute('selectwallet', ['wallet1']);
const wallet1OwnedNames = await wclient.execute('getnames', [true]);
await wclient.execute('selectwallet', ['wallet2']);
const wallet2OwnedNames = await wclient.execute('getnames', [true]);
assert.strictEqual(wallet1OwnedNames.length, 1);
assert.strictEqual(wallet2OwnedNames.length, 1);
assert.strictEqual(wallet1OwnedNames[0].name, name1);
assert.strictEqual(wallet2OwnedNames[0].name, name2);
});
});
});