psf-multisig-approval 1.0.4 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@
4
4
 
5
5
  // Global npm libraries
6
6
  const SlpWallet = require('minimal-slp-wallet')
7
+ const assert = require('chai').assert
7
8
 
8
9
  // Local libraries
9
10
  const MultisigApproval = require('../../index')
@@ -21,10 +22,95 @@ describe('#psf-multisig-approval', () => {
21
22
  uut = new MultisigApproval({ wallet })
22
23
  })
23
24
 
24
- describe('#getNftInfo', () => {
25
+ describe('#getNftHolderInfo', () => {
25
26
  it('should get address and pubkeys for Minting Council NFT holders', async () => {
26
- const result = await uut.getNftInfo()
27
- console.log('result: ', result)
27
+ const result = await uut.getNftHolderInfo()
28
+ // console.log('result: ', result)
29
+
30
+ // Assert expected properties exist
31
+ assert.property(result, 'keys')
32
+ assert.property(result, 'keysNotFound')
33
+
34
+ // Assert that each property is an array.
35
+ assert.isArray(result.keys)
36
+ assert.isArray(result.keysNotFound)
37
+ })
38
+ })
39
+
40
+ describe('#createMultisigAddress', () => {
41
+ it('should generate a multisig address from token holder info', async () => {
42
+ const tokenHolderInfo = await uut.getNftHolderInfo()
43
+
44
+ const keys = tokenHolderInfo.keys
45
+
46
+ const result = await uut.createMultisigAddress({ keys })
47
+ // console.log('result: ', result)
48
+
49
+ assert.property(result, 'address')
50
+ assert.property(result, 'scriptHex')
51
+ assert.property(result, 'publicKeys')
52
+ assert.property(result, 'requiredSigners')
53
+ })
54
+ })
55
+
56
+ describe('#getApprovalTx', () => {
57
+ it('should retrieve the latest APPROVAL transaction from an address', async () => {
58
+ const address = 'bitcoincash:qrwe6kxhvu47ve6jvgrf2d93w0q38av7s5xm9xfehr'
59
+
60
+ const result = await uut.getApprovalTx({ address })
61
+ // const result = await uut.getApprovalTx({ address, filterTxids: ['a63f9fbcc901316e6e89f5a8caaad6b2ab268278b29866c6c22088bd3ab93900'] })
62
+ // console.log('result: ', result)
63
+
64
+ assert.equal(result.updateTxid.length, 64)
65
+ })
66
+ })
67
+
68
+ describe('#getUpdateTx', () => {
69
+ it('should retrieve an update transaction', async () => {
70
+ const txid = 'f8ea1fcd4481adfd62c6251c6a4f63f3d5ac3d5fdcc38b350d321d93254df65f'
71
+
72
+ const result = await uut.getUpdateTx({ txid })
73
+ // console.log('result: ', result)
74
+
75
+ // Assert returned object has expected properties
76
+ assert.property(result, 'cid')
77
+ assert.property(result, 'ts')
78
+ assert.property(result, 'txid')
79
+ assert.property(result, 'txDetails')
80
+ })
81
+ })
82
+
83
+ describe('#getCidData', () => {
84
+ it('should get JSON data from an IPFS gateway', async () => {
85
+ const cid = 'bafybeib5d6s6t3tq4lhwp2ocvz7y2ws4czgkrmhlhv5y5aeyh6bqrmsxxi'
86
+ const result = await uut.getCidData({ cid })
87
+ // console.log('result: ', result)
88
+
89
+ // Assert expected properties exist
90
+ assert.property(result, 'groupId')
91
+ assert.property(result, 'keys')
92
+ assert.property(result, 'walletObj')
93
+ assert.property(result, 'multisigAddr')
94
+ assert.property(result, 'p2wdbWritePrice')
95
+ })
96
+ })
97
+
98
+ describe('#validateApproval', () => {
99
+ it('should validate a valid approval transaction', async () => {
100
+ const address = 'bitcoincash:qrwe6kxhvu47ve6jvgrf2d93w0q38av7s5xm9xfehr'
101
+ const approvalObj = await uut.getApprovalTx({ address })
102
+
103
+ const txid = 'f8ea1fcd4481adfd62c6251c6a4f63f3d5ac3d5fdcc38b350d321d93254df65f'
104
+ const updateObj = await uut.getUpdateTx({ txid })
105
+
106
+ const cid = 'bafybeib5d6s6t3tq4lhwp2ocvz7y2ws4czgkrmhlhv5y5aeyh6bqrmsxxi'
107
+ const updateData = await uut.getCidData({ cid })
108
+
109
+ const inObj = { approvalObj, updateObj, updateData }
110
+ const result = await uut.validateApproval(inObj)
111
+ // console.log('result: ', result)
112
+
113
+ assert.equal(result, true)
28
114
  })
29
115
  })
30
116
  })
@@ -5,18 +5,18 @@
5
5
  // npm libraries
6
6
  const assert = require('chai').assert
7
7
  const sinon = require('sinon')
8
- // const cloneDeep = require('lodash.clonedeep')
8
+ const cloneDeep = require('lodash.clonedeep')
9
9
  const SlpWallet = require('minimal-slp-wallet')
10
10
 
11
11
  // Mocking data libraries.
12
- // const mockDataLib = require('./mocks/util-mocks')
13
12
 
14
- // Unit under test
13
+ // Local libraries
15
14
  const MultisigApproval = require('../../index')
15
+ const mockDataLib = require('./mocks/main-index-mocks')
16
16
 
17
17
  describe('#MultisigApproval.js', () => {
18
18
  let sandbox
19
- // let mockData
19
+ let mockData
20
20
  let uut
21
21
  let wallet
22
22
 
@@ -30,7 +30,7 @@ describe('#MultisigApproval.js', () => {
30
30
  sandbox = sinon.createSandbox()
31
31
 
32
32
  // Clone the mock data.
33
- // mockData = cloneDeep(mockDataLib)
33
+ mockData = cloneDeep(mockDataLib)
34
34
 
35
35
  uut = new MultisigApproval({ wallet })
36
36
  })
@@ -53,14 +53,14 @@ describe('#MultisigApproval.js', () => {
53
53
  })
54
54
  })
55
55
 
56
- describe('#getNftInfo', () => {
56
+ describe('#getNftHolderInfo', () => {
57
57
  it('should get info about NFTs associated with a group token', async () => {
58
58
  // Mock dependencies and force desired code path
59
59
  sandbox.stub(uut.nfts, 'getNftsFromGroup').resolves()
60
60
  sandbox.stub(uut.nfts, 'getAddrsFromNfts').resolves()
61
61
  sandbox.stub(uut.nfts, 'findKeys').resolves({ keys: [], keysNotFound: [] })
62
62
 
63
- const result = await uut.getNftInfo()
63
+ const result = await uut.getNftHolderInfo()
64
64
 
65
65
  assert.property(result, 'keys')
66
66
  assert.property(result, 'keysNotFound')
@@ -73,7 +73,7 @@ describe('#MultisigApproval.js', () => {
73
73
  // Mock dependencies and force desired code path
74
74
  sandbox.stub(uut.nfts, 'getNftsFromGroup').rejects(new Error('test error'))
75
75
 
76
- await uut.getNftInfo()
76
+ await uut.getNftHolderInfo()
77
77
 
78
78
  assert.fail('Unexpected code path')
79
79
  } catch (err) {
@@ -81,4 +81,292 @@ describe('#MultisigApproval.js', () => {
81
81
  }
82
82
  })
83
83
  })
84
+
85
+ describe('#createMultisigAddress', () => {
86
+ it('should generate a P2SH multisig address', async () => {
87
+ const result = await uut.createMultisigAddress({ keys: mockData.pubkeys01 })
88
+ // console.log('result: ', result)
89
+
90
+ // Assert that expected properties exist
91
+ assert.property(result, 'address')
92
+ assert.property(result, 'scriptHex')
93
+ assert.property(result, 'publicKeys')
94
+ assert.property(result, 'requiredSigners')
95
+
96
+ // Assert that expected address is generated
97
+ assert.equal(result.address, 'bitcoincash:pqntzt6wcp38h8ud68wjnwh437uek76lhvhlwcm4fj')
98
+ })
99
+
100
+ it('should catch, report, and throw errors', async () => {
101
+ try {
102
+ await uut.createMultisigAddress()
103
+
104
+ assert.fail('unexpected result')
105
+ } catch (err) {
106
+ assert.include(err.message, 'keys must be an array containing public keys')
107
+ }
108
+ })
109
+ })
110
+
111
+ describe('#getApprovalTx', () => {
112
+ it('should return object with update txid', async () => {
113
+ // Mock dependencies and force desired code path.
114
+ sandbox.stub(uut.wallet, 'getTransactions').resolves(mockData.txHistory01)
115
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.approvalTxDetails01)
116
+
117
+ const address = 'bitcoincash:fake-addr'
118
+
119
+ const result = await uut.getApprovalTx({ address })
120
+ // console.log('result: ', result)
121
+
122
+ // Assert that the returned object has the expected properties.
123
+ assert.property(result, 'approvalTxid')
124
+ assert.property(result, 'updateTxid')
125
+ assert.property(result, 'approvalTxDetails')
126
+ assert.property(result, 'opReturn')
127
+
128
+ // Assert that TXIDs are returned.
129
+ assert.equal(result.updateTxid.length, 64)
130
+ assert.equal(result.approvalTxid.length, 64)
131
+ })
132
+
133
+ it('should handle SLP addresses', async () => {
134
+ // Mock dependencies and force desired code path.
135
+ sandbox.stub(uut.wallet, 'getTransactions').resolves(mockData.txHistory01)
136
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.approvalTxDetails01)
137
+
138
+ const address = 'simpleledger:qpq4uxk6vc2hn3rw8tevpm570xgs22e6rskpzpenqg'
139
+
140
+ const result = await uut.getApprovalTx({ address })
141
+ // console.log('result: ', result)
142
+
143
+ // Assert that the returned object has the expected properties.
144
+ assert.property(result, 'approvalTxid')
145
+ assert.property(result, 'updateTxid')
146
+ assert.property(result, 'approvalTxDetails')
147
+ assert.property(result, 'opReturn')
148
+
149
+ // Assert that TXIDs are returned.
150
+ assert.equal(result.updateTxid.length, 64)
151
+ assert.equal(result.approvalTxid.length, 64)
152
+ })
153
+
154
+ it('should skip a TXID in the filter list', async () => {
155
+ // Mock dependencies and force desired code path.
156
+ sandbox.stub(uut.wallet, 'getTransactions').resolves(mockData.txHistory02)
157
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.approvalTxDetails01)
158
+
159
+ const address = 'bitcoincash:fake-addr'
160
+
161
+ const result = await uut.getApprovalTx({
162
+ address,
163
+ filterTxids: ['095b299da0be5bb2367e62a5628cef603c7d6e709dd72f532632e9c0acf665d3']
164
+ })
165
+ // console.log('result: ', result)
166
+
167
+ // Assert that the returned object has the expected properties.
168
+ assert.property(result, 'approvalTxid')
169
+ assert.property(result, 'updateTxid')
170
+ assert.property(result, 'approvalTxDetails')
171
+ assert.property(result, 'opReturn')
172
+
173
+ // Assert that TXIDs are returned.
174
+ assert.equal(result.updateTxid.length, 64)
175
+ assert.equal(result.approvalTxid.length, 64)
176
+ })
177
+
178
+ it('should skip a TXID if it does contain APPROVAL in the OP_RETURN', async () => {
179
+ // Mock dependencies and force desired code path.
180
+ sandbox.stub(uut.wallet, 'getTransactions').resolves(mockData.txHistory02)
181
+ sandbox.stub(uut.util, 'getTxData')
182
+ .onCall(0).resolves(mockData.updateTxDetails01)
183
+ .onCall(1).resolves(mockData.approvalTxDetails01)
184
+
185
+ const address = 'bitcoincash:fake-addr'
186
+
187
+ const result = await uut.getApprovalTx({
188
+ address
189
+ })
190
+ // console.log('result: ', result)
191
+
192
+ // Assert that the returned object has the expected properties.
193
+ assert.property(result, 'approvalTxid')
194
+ assert.property(result, 'updateTxid')
195
+ assert.property(result, 'approvalTxDetails')
196
+ assert.property(result, 'opReturn')
197
+
198
+ // Assert that TXIDs are returned.
199
+ assert.equal(result.updateTxid.length, 64)
200
+ assert.equal(result.approvalTxid.length, 64)
201
+ })
202
+
203
+ it('should return null if approval TX can not be found', async () => {
204
+ // Mock dependencies and force desired code path.
205
+ sandbox.stub(uut.wallet, 'getTransactions').resolves(mockData.txHistory01)
206
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.updateTxDetails01)
207
+
208
+ const address = 'bitcoincash:fake-addr'
209
+
210
+ const result = await uut.getApprovalTx({ address })
211
+ // console.log('result: ', result)
212
+
213
+ assert.equal(result, null)
214
+ })
215
+
216
+ it('should throw an error if invalid address format is used', async () => {
217
+ try {
218
+ const address = 'fake-addr'
219
+
220
+ await uut.getApprovalTx({ address })
221
+
222
+ assert.fail('Unexpected code path')
223
+ } catch (err) {
224
+ assert.include(err.message, 'Input address must start with bitcoincash: or simpleledger:')
225
+ }
226
+ })
227
+ })
228
+
229
+ describe('#getUpdateTx', () => {
230
+ it('should get data from an update transaction', async () => {
231
+ // Mock dependencies and force desired code path
232
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.updateTxDetails01)
233
+
234
+ const txid = 'f8ea1fcd4481adfd62c6251c6a4f63f3d5ac3d5fdcc38b350d321d93254df65f'
235
+
236
+ const result = await uut.getUpdateTx({ txid })
237
+ // console.log('result: ', result)
238
+
239
+ // Assert returned object has expected properties
240
+ assert.property(result, 'cid')
241
+ assert.property(result, 'ts')
242
+ assert.property(result, 'txid')
243
+ assert.property(result, 'txDetails')
244
+ })
245
+
246
+ it('should throw an error if a txid is given as input', async () => {
247
+ try {
248
+ await uut.getUpdateTx()
249
+
250
+ assert.fail('Unexpected result')
251
+ } catch (err) {
252
+ assert.include(err.message, 'txid required')
253
+ }
254
+ })
255
+
256
+ it('should throw an error if OP_RETURN can not be decoded', async () => {
257
+ try {
258
+ // Mock dependencies and force desired code path
259
+ mockData.updateTxDetails01.vout[0].scriptPubKey.hex = '0123456789abcdef'
260
+ sandbox.stub(uut.util, 'getTxData').resolves(mockData.updateTxDetails01)
261
+
262
+ const txid = 'f8ea1fcd4481adfd62c6251c6a4f63f3d5ac3d5fdcc38b350d321d93254df65f'
263
+
264
+ await uut.getUpdateTx({ txid })
265
+
266
+ assert.fail('Unexpected result')
267
+ } catch (err) {
268
+ assert.include(err.message, 'Could not parse JSON inside')
269
+ }
270
+ })
271
+ })
272
+
273
+ describe('#getCidData', () => {
274
+ it('should resolve CID data from an IPFS gateway', async () => {
275
+ // Mock dependencies and force desired code path
276
+ sandbox.stub(uut.axios, 'get').resolves({ data: mockData.updateData01 })
277
+
278
+ const cid = 'bafybeib5d6s6t3tq4lhwp2ocvz7y2ws4czgkrmhlhv5y5aeyh6bqrmsxxi'
279
+
280
+ const result = await uut.getCidData({ cid })
281
+ // console.log('result: ', result)
282
+
283
+ // Assert that expected properties exist
284
+ assert.property(result, 'groupId')
285
+ assert.property(result, 'keys')
286
+ assert.property(result, 'walletObj')
287
+ assert.property(result, 'multisigAddr')
288
+ assert.property(result, 'p2wdbWritePrice')
289
+ })
290
+
291
+ it('should throw an error if CID is not specified', async () => {
292
+ try {
293
+ await uut.getCidData()
294
+
295
+ assert.fail('Unexpected code path')
296
+ } catch (err) {
297
+ assert.include(err.message, 'cid a required input')
298
+ }
299
+ })
300
+ })
301
+
302
+ describe('#validateApproval', () => {
303
+ it('should validate a valid approval transaction', async () => {
304
+ // Mock dependencies and force desired code path
305
+ sandbox.stub(uut, 'getNftHolderInfo').resolves(mockData.tokenHolderInfo01)
306
+
307
+ const inObj = {
308
+ approvalObj: mockData.approvalObj01,
309
+ updateObj: mockData.updateObj01,
310
+ updateData: mockData.updateData01
311
+ }
312
+
313
+ const result = await uut.validateApproval(inObj)
314
+
315
+ assert.equal(result, true)
316
+ })
317
+
318
+ it('should throw an error if approvalObj is not specified', async () => {
319
+ try {
320
+ await uut.validateApproval()
321
+
322
+ assert.fail('Unexpected code path')
323
+ } catch (err) {
324
+ assert.include(err.message, 'Output object of getApprovalTx() is expected as input to this function, as \'approvalObj\'')
325
+ }
326
+ })
327
+
328
+ it('should throw an error if updateObj is not specified', async () => {
329
+ try {
330
+ const inObj = {
331
+ approvalObj: mockData.approvalObj01
332
+ }
333
+
334
+ await uut.validateApproval(inObj)
335
+
336
+ assert.fail('Unexpected code path')
337
+ } catch (err) {
338
+ assert.include(err.message, 'Output object of getUpdateTx() is expected as input to this function, as \'updateObj\'')
339
+ }
340
+ })
341
+
342
+ it('should throw an error if updateData is not specified', async () => {
343
+ try {
344
+ const inObj = {
345
+ approvalObj: mockData.approvalObj01,
346
+ updateObj: mockData.updateObj01
347
+ }
348
+
349
+ await uut.validateApproval(inObj)
350
+
351
+ assert.fail('Unexpected code path')
352
+ } catch (err) {
353
+ assert.include(err.message, 'Update CID JSON data is expected as input to this function, as \'updateData\'')
354
+ }
355
+ })
356
+
357
+ it('should return false if addresses do not match', async () => {
358
+ // Mock dependencies and force desired code path
359
+ mockData.approvalObj01.approvalTxDetails.vin[0].address = 'bitcoincash:fake-addr'
360
+
361
+ const inObj = {
362
+ approvalObj: mockData.approvalObj01,
363
+ updateObj: mockData.updateObj01,
364
+ updateData: mockData.updateData01
365
+ }
366
+
367
+ const result = await uut.validateApproval(inObj)
368
+
369
+ assert.equal(result, false)
370
+ })
371
+ })
84
372
  })