psf-multisig-approval 2.1.0 → 2.3.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
@@ -47,4 +47,4 @@ class UtilLib {
47
47
  }
48
48
  }
49
49
 
50
- module.exports = UtilLib
50
+ export default UtilLib
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "psf-multisig-approval",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Utility library implementing the PS009 specification",
5
+ "type": "module",
5
6
  "main": "index.js",
6
7
  "scripts": {
7
8
  "start": "node index.js",
@@ -40,7 +41,7 @@
40
41
  "chai": "4.3.7",
41
42
  "husky": "8.0.3",
42
43
  "lodash.clonedeep": "4.5.0",
43
- "minimal-slp-wallet": "5.11.2",
44
+ "minimal-slp-wallet": "7.1.3",
44
45
  "mocha": "10.2.0",
45
46
  "nyc": "15.1.0",
46
47
  "semantic-release": "19.0.5",
@@ -3,11 +3,11 @@
3
3
  */
4
4
 
5
5
  // Global npm libraries
6
- const SlpWallet = require('minimal-slp-wallet')
7
- const assert = require('chai').assert
6
+ import SlpWallet from 'minimal-slp-wallet'
7
+ import { assert } from 'chai'
8
8
 
9
9
  // Local libraries
10
- const MultisigApproval = require('../../index')
10
+ import MultisigApproval from '../../index.js'
11
11
 
12
12
  describe('#psf-multisig-approval', () => {
13
13
  let uut
@@ -3,16 +3,16 @@
3
3
  */
4
4
 
5
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')
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ import cloneDeep from 'lodash.clonedeep'
9
+ import SlpWallet from 'minimal-slp-wallet'
10
10
 
11
11
  // Mocking data libraries.
12
12
 
13
13
  // Local libraries
14
- const MultisigApproval = require('../../index')
15
- const mockDataLib = require('./mocks/main-index-mocks')
14
+ import MultisigApproval from '../../index.js'
15
+ import mockDataLib from './mocks/main-index-mocks.js'
16
16
 
17
17
  describe('#MultisigApproval.js', () => {
18
18
  let sandbox
@@ -277,7 +277,19 @@ const updateObj01 = {
277
277
  txDetails: updateTxDetails01
278
278
  }
279
279
 
280
- module.exports = {
280
+ export {
281
+ pubkeys01,
282
+ txHistory01,
283
+ txHistory02,
284
+ approvalTxDetails01,
285
+ updateTxDetails01,
286
+ tokenHolderInfo01,
287
+ updateData01,
288
+ approvalObj01,
289
+ updateObj01
290
+ }
291
+
292
+ export default {
281
293
  pubkeys01,
282
294
  txHistory01,
283
295
  txHistory02,
@@ -0,0 +1,304 @@
1
+ /*
2
+ Deterministic fixtures for the PS009 v2 unit tests.
3
+
4
+ Nothing here touches a node or a wallet: keys are derived from clearly
5
+ test-only hex patterns, and every UTXO is fake. The fixtures build real,
6
+ signed transactions locally so the tests can assert exact bytes, and act as
7
+ the injected "providers" that validateApproval() expects.
8
+ */
9
+
10
+ import bitcore from '@chris.troutner/bitcore-lib-cash'
11
+
12
+ import {
13
+ buildApprovalTransaction,
14
+ buildUpdateTransaction,
15
+ deriveAddressFromPubkey,
16
+ deriveCouncilAddress,
17
+ encodePsP1
18
+ } from '../../../lib/ps009v2/index.js'
19
+
20
+ // Test-only private keys (hex scalars, clearly not real funds).
21
+ const COUNCIL_KEY_HEX = [
22
+ 'a1'.repeat(32),
23
+ 'b2'.repeat(32),
24
+ 'c3'.repeat(32)
25
+ ]
26
+ const UPDATE_KEY_HEX = 'd4'.repeat(32)
27
+
28
+ const councilKeys = COUNCIL_KEY_HEX.map(hex => new bitcore.PrivateKey(hex))
29
+ const updateKey = new bitcore.PrivateKey(UPDATE_KEY_HEX)
30
+
31
+ const councilWifs = councilKeys.map(key => key.toWIF())
32
+ const councilPubKeys = councilKeys.map(key => key.publicKey.toString())
33
+ const updateAddress = updateKey.toAddress().toString()
34
+
35
+ // 2-of-3 deterministic council.
36
+ const council = deriveCouncilAddress({
37
+ pubkeys: councilPubKeys,
38
+ requiredSignatures: 2
39
+ })
40
+
41
+ // The five-key 3-of-5 council from the PS009 v1 README (also the PSF Minting
42
+ // Council fixture). Used as a spec/README cross-check vector.
43
+ const README_PUBKEYS = [
44
+ '021ca211a04a1d489ae77e01c28c97b02e733893890fda00a359ca8956c2e0d259',
45
+ '0361fd21512b9072e8f6b984d9b04c57e5779867c2ad002999372268770fcb2674',
46
+ '03b9bdc40c478ab0536be29c368b26c48a5e8d6867fc34b77c727ab690365aae91',
47
+ '033c930d4cff4ba68a70f7a21443e20ad4176173380d21f8c3ce27f7ce947f3246',
48
+ '024be54accec310c2636140336ae548d610ba9b7ce300b3f42494b4a6f2963731f'
49
+ ]
50
+ const README_COUNCIL = deriveCouncilAddress({
51
+ pubkeys: README_PUBKEYS,
52
+ requiredSignatures: 3
53
+ })
54
+
55
+ // PS010 section 4.1 registry epoch values.
56
+ const GROUP_TOKEN_ID = '8e8d90ebdb1791d58eba7acd428ff3b1e21c47fb7aba2ba3b5b815aa0fe7d6d5'
57
+ const NFT_IDS = [
58
+ 'e86164aaa06efac1d6453951f67beafe042f0bceb9312845a95355b1e93aa846',
59
+ 'da7ccbd5e24e468c9e7402489ca9148b5e76e588b73cc4aa4bbf1ca41d5808ab',
60
+ '0ec318bede8c2f229db840a24cb63d662ad91e8e6c840e46e6a8ff2d173049ce'
61
+ ]
62
+ const POOL_CASH_ADDR = 'bitcoincash:pzypdf3wv89zy75mvjj4aetn63nq8sc3cufmnkzzjq'
63
+ const VALID_FROM_HEIGHT = 920000
64
+ const SATS_PER_MB = 2000
65
+ const PREV_REGISTRY_TXID = '0'.repeat(64)
66
+ const REFERENCE_ADDRESS = 'bitcoincash:qrwe6kxhvu47ve6jvgrf2d93w0q38av7s5xm9xfehr'
67
+ const CID = 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi'
68
+ const UPDATE_TS = 1770000000000
69
+
70
+ // Fake funding UTXOs.
71
+ const COUNCIL_FUNDING_TXID = 'a'.repeat(64)
72
+ const UPDATE_FUNDING_TXID = 'b'.repeat(64)
73
+ const AT_FUNDING_TXID = 'c'.repeat(64)
74
+ const OTHER_TXID = 'd'.repeat(64)
75
+ const FAKE_APPROVAL_TXID = 'e'.repeat(64)
76
+
77
+ // ----- build the on-chain objects used by the flow tests -----
78
+
79
+ // 1. The PS010 registry announcement (PSP1), signed by the council.
80
+ const announcement = buildUpdateTransaction({
81
+ poolCashAddr: POOL_CASH_ADDR,
82
+ satsPerMb: SATS_PER_MB,
83
+ validFromHeight: VALID_FROM_HEIGHT,
84
+ prevRegistryTxid: PREV_REGISTRY_TXID,
85
+ inputs: [{
86
+ txid: COUNCIL_FUNDING_TXID,
87
+ vout: 0,
88
+ satoshis: 100000,
89
+ script: council.scriptHex,
90
+ address: council.address
91
+ }],
92
+ council,
93
+ privKeys: [councilWifs[0], councilWifs[1]]
94
+ })
95
+
96
+ // 2. The Update Transaction (P2PKH), carrying the CID + `endorses`.
97
+ const updateTx = buildUpdateTransaction({
98
+ cid: CID,
99
+ endorses: announcement.txid,
100
+ ts: UPDATE_TS,
101
+ inputs: [{
102
+ txid: UPDATE_FUNDING_TXID,
103
+ vout: 1,
104
+ satoshis: 50000,
105
+ script: new bitcore.Script(updateKey.toAddress()).toHex(),
106
+ address: updateAddress
107
+ }],
108
+ privKeys: [updateKey],
109
+ changeAddress: updateAddress
110
+ })
111
+
112
+ // 3. The Approval Transaction (2-of-3 council signatures).
113
+ const approvalTx = buildApprovalTransaction({
114
+ utxid: updateTx.txid,
115
+ utxos: [{
116
+ txid: AT_FUNDING_TXID,
117
+ vout: 0,
118
+ satoshis: 200000,
119
+ script: council.scriptHex,
120
+ address: council.address
121
+ }],
122
+ council,
123
+ privKeys: [councilWifs[0], councilWifs[1]],
124
+ referenceAddress: REFERENCE_ADDRESS
125
+ })
126
+
127
+ // The membership JSON that the Update Transaction CID resolves to.
128
+ const memberAddresses = councilPubKeys.map(pubKey => deriveAddressFromPubkey(pubKey))
129
+
130
+ const membership = {
131
+ groupTokenId: GROUP_TOKEN_ID,
132
+ nfts: NFT_IDS.map((tokenId, i) => ({
133
+ tokenId,
134
+ address: memberAddresses[i],
135
+ pubKey: councilPubKeys[i]
136
+ })),
137
+ requiredSignatures: 2,
138
+ multisigAddr: council.address
139
+ }
140
+
141
+ const nftData = {}
142
+ NFT_IDS.forEach((tokenId, i) => {
143
+ nftData[tokenId] = {
144
+ tokenId,
145
+ groupTokenId: GROUP_TOKEN_ID,
146
+ holder: memberAddresses[i]
147
+ }
148
+ })
149
+
150
+ // ----- tx-details shaped helpers (bch-api / Fulcrum style) -----
151
+
152
+ function addressOfScript (scriptHex) {
153
+ try {
154
+ return new bitcore.Address(new bitcore.Script(scriptHex)).toString()
155
+ } catch (err) {
156
+ return null
157
+ }
158
+ }
159
+
160
+ function txDetails (built, inObj = {}) {
161
+ const tx = built.tx
162
+ return {
163
+ txid: built.txid,
164
+ hex: built.hex,
165
+ height: inObj.height || 100,
166
+ vin: [{
167
+ txid: inObj.prevTxid || OTHER_TXID,
168
+ vout: 0,
169
+ address: inObj.prevAddress || council.address,
170
+ satoshis: inObj.prevSatoshis || 100000,
171
+ scriptSig: { hex: tx.inputs[0].script.toHex() }
172
+ }],
173
+ vout: tx.outputs.map((output, n) => {
174
+ const address = addressOfScript(output.script.toHex())
175
+ return {
176
+ n,
177
+ satoshis: output.satoshis,
178
+ scriptPubKey: {
179
+ hex: output.script.toHex(),
180
+ addresses: address ? [address] : []
181
+ }
182
+ }
183
+ })
184
+ }
185
+ }
186
+
187
+ const announcementDetails = txDetails(announcement, {
188
+ prevTxid: COUNCIL_FUNDING_TXID,
189
+ prevAddress: council.address,
190
+ prevSatoshis: 100000
191
+ })
192
+ const updateDetails = txDetails(updateTx, {
193
+ prevTxid: UPDATE_FUNDING_TXID,
194
+ prevAddress: updateAddress,
195
+ prevSatoshis: 50000
196
+ })
197
+ const approvalDetails = txDetails(approvalTx, {
198
+ prevTxid: AT_FUNDING_TXID,
199
+ prevAddress: council.address,
200
+ prevSatoshis: 200000
201
+ })
202
+
203
+ // ----- injected providers -----
204
+
205
+ // Build the provider set validateApproval() expects. Every provider can be
206
+ // overridden per test to force a specific section 6 failure.
207
+ function makeProviders (overrides = {}) {
208
+ const txs = {
209
+ [announcement.txid]: announcementDetails,
210
+ [updateTx.txid]: updateDetails,
211
+ [approvalTx.txid]: approvalDetails
212
+ }
213
+
214
+ const providers = {
215
+ psp1FromChain: async (txid) => txs[txid] || null,
216
+ getTx: async (txid) => txs[txid] || null,
217
+ getTxHistory: async () => [
218
+ { tx_hash: OTHER_TXID, height: 101 },
219
+ { tx_hash: approvalTx.txid, height: 100 }
220
+ ],
221
+ getCidData: async () => membership,
222
+ getNft: async (tokenId) => nftData[tokenId] || null,
223
+ referenceAddress: REFERENCE_ADDRESS
224
+ }
225
+
226
+ return Object.assign(providers, overrides)
227
+ }
228
+
229
+ // The base validateApproval() arguments for the happy path.
230
+ function makeValidationArgs (overrides = {}) {
231
+ const args = {
232
+ atxid: approvalTx.txid,
233
+ utxid: updateTx.txid,
234
+ psp1Txid: announcement.txid,
235
+ council: {
236
+ groupTokenId: GROUP_TOKEN_ID,
237
+ requiredSignatures: 2,
238
+ address: council.address
239
+ },
240
+ referenceAddress: REFERENCE_ADDRESS
241
+ }
242
+
243
+ return Object.assign(args, makeProviders(), overrides)
244
+ }
245
+
246
+ const psp1Record = encodePsP1({
247
+ poolCashAddr: POOL_CASH_ADDR,
248
+ satsPerMb: SATS_PER_MB,
249
+ validFromHeight: VALID_FROM_HEIGHT,
250
+ prevRegistryTxid: PREV_REGISTRY_TXID
251
+ })
252
+
253
+ export {
254
+ COUNCIL_KEY_HEX,
255
+ UPDATE_KEY_HEX,
256
+ councilKeys,
257
+ councilWifs,
258
+ councilPubKeys,
259
+ updateKey,
260
+ updateAddress,
261
+ council,
262
+ README_PUBKEYS,
263
+ README_COUNCIL,
264
+ GROUP_TOKEN_ID,
265
+ NFT_IDS,
266
+ POOL_CASH_ADDR,
267
+ VALID_FROM_HEIGHT,
268
+ SATS_PER_MB,
269
+ PREV_REGISTRY_TXID,
270
+ REFERENCE_ADDRESS,
271
+ CID,
272
+ UPDATE_TS,
273
+ COUNCIL_FUNDING_TXID,
274
+ UPDATE_FUNDING_TXID,
275
+ AT_FUNDING_TXID,
276
+ OTHER_TXID,
277
+ FAKE_APPROVAL_TXID,
278
+ announcement,
279
+ updateTx,
280
+ approvalTx,
281
+ memberAddresses,
282
+ membership,
283
+ nftData,
284
+ announcementDetails,
285
+ updateDetails,
286
+ approvalDetails,
287
+ txDetails,
288
+ psp1Record,
289
+ makeProviders,
290
+ makeValidationArgs
291
+ }
292
+
293
+ export default {
294
+ council,
295
+ README_COUNCIL,
296
+ announcement,
297
+ updateTx,
298
+ approvalTx,
299
+ membership,
300
+ nftData,
301
+ psp1Record,
302
+ makeProviders,
303
+ makeValidationArgs
304
+ }
@@ -26,7 +26,7 @@ const mockUtxos = {
26
26
  ]
27
27
  }
28
28
 
29
- module.exports = {
29
+ export {
30
30
  mockBalance,
31
31
  mockUtxos
32
32
  }
@@ -3,19 +3,16 @@
3
3
  */
4
4
 
5
5
  // npm libraries
6
- const chai = require('chai')
7
- const sinon = require('sinon')
8
- // const cloneDeep = require('lodash.clonedeep')
9
- const SlpWallet = require('minimal-slp-wallet')
10
-
11
- // Locally global variables.
12
- const assert = chai.assert
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ // import cloneDeep from 'lodash.clonedeep'
9
+ import SlpWallet from 'minimal-slp-wallet'
13
10
 
14
11
  // Mocking data libraries.
15
- // const mockDataLib = require('./mocks/util-mocks')
12
+ // import mockDataLib from './mocks/util-mocks.js'
16
13
 
17
14
  // Unit under test
18
- const NftsLib = require('../../lib/nfts')
15
+ import NftsLib from '../../lib/nfts.js'
19
16
 
20
17
  describe('#NFTs', () => {
21
18
  let sandbox