psf-multisig-approval 1.0.3 → 1.1.0

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.
package/lib/util.js CHANGED
@@ -1,47 +1,47 @@
1
1
  /*
2
- An example of a typical utility library. Things to notice:
3
- - This library is exported as a Class.
4
- - External dependencies are embedded into the class 'this' object: this.bitbox
5
- - `_this` maintains top-level context for `this`.
2
+ Generic utility functions that might be used in multiple places of the library.
6
3
  */
7
4
 
8
- 'use strict'
9
-
10
- // npm libraries
11
- const BCHJS = require('@psf/bch-js')
12
-
13
- // Locally global variables.
14
- let _this
15
- const bchjs = new BCHJS()
16
-
17
5
  class UtilLib {
18
- constructor () {
19
- // Embed external libraries into the class, for easy mocking.
20
- this.bchjs = bchjs
6
+ constructor (localConfig = {}) {
7
+ // Dependecy Injection
8
+ this.wallet = localConfig.wallet
9
+ if (!this.wallet) {
10
+ throw new Error('Instance of minimal-slp-wallet must be passed in as a property called \'wallet\', when initializing the util library.')
11
+ }
12
+
13
+ // Bind the 'this' object to all subfunctions.
14
+ this.getTxData = this.getTxData.bind(this)
21
15
 
22
- _this = this
16
+ // Create a transaction details cache, to reduce the number of API calls.
17
+ this.txCache = {}
23
18
  }
24
19
 
25
- async getBchData (addr) {
20
+ // Given a transaction ID, retrieve the transaction details. If the details
21
+ // have already been downloaded, then they will be stored in a local cache.
22
+ // The cache speeds things up and reduces the number of API calls.
23
+ async getTxData (txid) {
26
24
  try {
27
- // Validate Input
28
- if (typeof addr !== 'string') throw new Error('Address must be a string')
25
+ if (!txid) {
26
+ throw new Error('txid (transactoin ID) required')
27
+ }
29
28
 
30
- const balance = await _this.bchjs.Electrumx.balance(addr)
29
+ // Try to get the transaction details from the cache
30
+ let txDetails = this.txCache[txid]
31
31
 
32
- const utxos = await _this.bchjs.Electrumx.utxo(addr)
32
+ // Download from the full node if details are not in the cache.
33
+ if (!txDetails) {
34
+ // Get the transaction details from the full node
35
+ txDetails = await this.wallet.getTxData([txid])
36
+ txDetails = txDetails[0]
33
37
 
34
- const bchData = {
35
- balance: balance.balance,
36
- utxos: utxos.utxos
38
+ // Add the details to the cache
39
+ this.txCache[txid] = txDetails
37
40
  }
38
- // console.log(`bchData: ${JSON.stringify(bchData, null, 2)}`)
39
41
 
40
- return bchData
42
+ return txDetails
41
43
  } catch (err) {
42
- // Optional log to indicate the source of the error. This would normally
43
- // be written with a logging app like Winston.
44
- console.log('Error in util.js/getBalance()')
44
+ console.error('Error in getTxData()')
45
45
  throw err
46
46
  }
47
47
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "psf-multisig-approval",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Utility library implementing the PS009 specification",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "start": "node index.js",
8
8
  "test": "npm run lint && TEST=unit nyc mocha test/unit/",
9
- "test:integration": "mocha --timeout 25000 test/integration/",
9
+ "test:integration": "mocha --timeout 120000 test/integration/",
10
10
  "lint": "standard --env mocha --fix",
11
11
  "docs": "./node_modules/.bin/apidoc -i src/ -o docs",
12
12
  "coverage:report": "nyc --reporter=html mocha test/unit/ --exit"
@@ -32,13 +32,15 @@
32
32
  },
33
33
  "repository": "Permissionless-Software-Foundation/psf-multisig-approval",
34
34
  "dependencies": {
35
- "@psf/bch-js": "6.7.1"
35
+ "axios": "1.3.5",
36
+ "bitcore-lib-cash": "10.0.2"
36
37
  },
37
38
  "devDependencies": {
38
39
  "apidoc": "0.54.0",
39
40
  "chai": "4.3.7",
40
41
  "husky": "8.0.3",
41
42
  "lodash.clonedeep": "4.5.0",
43
+ "minimal-slp-wallet": "5.8.8",
42
44
  "mocha": "10.2.0",
43
45
  "nyc": "15.1.0",
44
46
  "semantic-release": "19.0.5",
@@ -0,0 +1,116 @@
1
+ /*
2
+ Integration tests for the main library.
3
+ */
4
+
5
+ // Global npm libraries
6
+ const SlpWallet = require('minimal-slp-wallet')
7
+ const assert = require('chai').assert
8
+
9
+ // Local libraries
10
+ const MultisigApproval = require('../../index')
11
+
12
+ describe('#psf-multisig-approval', () => {
13
+ let uut
14
+
15
+ before(async () => {
16
+ const wallet = new SlpWallet(undefined, {
17
+ interface: 'consumer-api',
18
+ restURL: 'https://bch-consumer-anacortes-wa-usa.fullstackcash.nl'
19
+ })
20
+ await wallet.walletInfoPromise
21
+
22
+ uut = new MultisigApproval({ wallet })
23
+ })
24
+
25
+ describe('#getNftHolderInfo', () => {
26
+ it('should get address and pubkeys for Minting Council NFT holders', async () => {
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)
114
+ })
115
+ })
116
+ })
@@ -0,0 +1,372 @@
1
+ /*
2
+ Unit tests for the main index.js library file.
3
+ */
4
+
5
+ // npm libraries
6
+ const assert = require('chai').assert
7
+ const sinon = require('sinon')
8
+ const cloneDeep = require('lodash.clonedeep')
9
+ const SlpWallet = require('minimal-slp-wallet')
10
+
11
+ // Mocking data libraries.
12
+
13
+ // Local libraries
14
+ const MultisigApproval = require('../../index')
15
+ const mockDataLib = require('./mocks/main-index-mocks')
16
+
17
+ describe('#MultisigApproval.js', () => {
18
+ let sandbox
19
+ let mockData
20
+ let uut
21
+ let wallet
22
+
23
+ before(async () => {
24
+ wallet = new SlpWallet(undefined, { interface: 'consumer-api' })
25
+ await wallet.walletInfoPromise
26
+ })
27
+
28
+ beforeEach(() => {
29
+ // Restore the sandbox before each test.
30
+ sandbox = sinon.createSandbox()
31
+
32
+ // Clone the mock data.
33
+ mockData = cloneDeep(mockDataLib)
34
+
35
+ uut = new MultisigApproval({ wallet })
36
+ })
37
+
38
+ afterEach(() => sandbox.restore())
39
+
40
+ describe('#constructor', () => {
41
+ it('should throw an error if wallet is not passed', () => {
42
+ try {
43
+ uut = new MultisigApproval()
44
+
45
+ assert.fail('Unexpected result')
46
+ } catch (err) {
47
+ assert.include(err.message, 'Instance of minimal-slp-wallet must be passed in as a property called \'wallet\', when initializing the psf-multisig-approval library.')
48
+ }
49
+ })
50
+
51
+ it('should have encapsulated dependencies', () => {
52
+ assert.property(uut, 'nfts')
53
+ })
54
+ })
55
+
56
+ describe('#getNftHolderInfo', () => {
57
+ it('should get info about NFTs associated with a group token', async () => {
58
+ // Mock dependencies and force desired code path
59
+ sandbox.stub(uut.nfts, 'getNftsFromGroup').resolves()
60
+ sandbox.stub(uut.nfts, 'getAddrsFromNfts').resolves()
61
+ sandbox.stub(uut.nfts, 'findKeys').resolves({ keys: [], keysNotFound: [] })
62
+
63
+ const result = await uut.getNftHolderInfo()
64
+
65
+ assert.property(result, 'keys')
66
+ assert.property(result, 'keysNotFound')
67
+ assert.isArray(result.keys)
68
+ assert.isArray(result.keysNotFound)
69
+ })
70
+
71
+ it('should catch, report, and throw errors', async () => {
72
+ try {
73
+ // Mock dependencies and force desired code path
74
+ sandbox.stub(uut.nfts, 'getNftsFromGroup').rejects(new Error('test error'))
75
+
76
+ await uut.getNftHolderInfo()
77
+
78
+ assert.fail('Unexpected code path')
79
+ } catch (err) {
80
+ assert.include(err.message, 'test error')
81
+ }
82
+ })
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
+ })
372
+ })