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.
@@ -0,0 +1,805 @@
1
+ /*
2
+ PS009 v2 - builder + full Update -> Approval -> validate flow tests.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import bitcore from '@chris.troutner/bitcore-lib-cash'
8
+
9
+ // Local libraries
10
+ import {
11
+ buildUpdateTransaction,
12
+ buildApprovalTransaction,
13
+ parseUpdateRecord,
14
+ parsePsP1Record,
15
+ parseApprovalRecord,
16
+ deriveCouncilAddress,
17
+ validateApproval,
18
+ countMultisigSignatures,
19
+ verifyMultisigSpend
20
+ } from '../../lib/ps009v2/index.js'
21
+ import { opReturnScript } from '../../lib/ps009v2/script.js'
22
+ import * as mockData from './mocks/ps009v2-mocks.js'
23
+
24
+ function clone (obj) {
25
+ return JSON.parse(JSON.stringify(obj))
26
+ }
27
+
28
+ // Wrap the fixture providers so a single test can override one provider
29
+ // without losing the happy-path defaults.
30
+ function withProviders (overrides) {
31
+ return mockData.makeValidationArgs(overrides)
32
+ }
33
+
34
+ describe('#ps009v2 flow', () => {
35
+ describe('#buildUpdateTransaction (announcement mode)', () => {
36
+ it('should build and sign a PSP1 announcement from a council UTXO', () => {
37
+ const announcement = mockData.announcement
38
+ const record = parsePsP1Record(announcement)
39
+
40
+ assert.equal(record.lokad, 'PSP1')
41
+ assert.equal(record.poolCashAddr, mockData.POOL_CASH_ADDR)
42
+ assert.equal(record.validFromHeight, mockData.VALID_FROM_HEIGHT)
43
+ assert.equal(record.satsPerMb, mockData.SATS_PER_MB)
44
+ assert.equal(announcement.mode, 'announcement')
45
+ assert.isTrue(announcement.signed)
46
+ assert.equal(announcement.txid, '3f1be5e7cfb8b07166d1155f69f4e399da95c54d0b7886574831ece12f71c87c')
47
+
48
+ // vout[0] is the PSP1 OP_RETURN, vout[1] is change back to the council.
49
+ assert.equal(announcement.tx.outputs[0].satoshis, 0)
50
+ assert.equal(
51
+ new bitcore.Address(announcement.tx.outputs[1].script).toString(),
52
+ mockData.council.address
53
+ )
54
+ })
55
+
56
+ it('should reject mixing Update and announcement fields', () => {
57
+ assert.throws(() => buildUpdateTransaction({
58
+ cid: mockData.CID,
59
+ poolCashAddr: mockData.POOL_CASH_ADDR,
60
+ validFromHeight: 1,
61
+ satsPerMb: 1,
62
+ inputs: []
63
+ }), /not both/)
64
+ })
65
+
66
+ it('should reject a missing cid or missing inputs', () => {
67
+ assert.throws(() => buildUpdateTransaction({ inputs: [] }), /cid is required/)
68
+ assert.throws(() => buildUpdateTransaction({
69
+ cid: mockData.CID,
70
+ endorses: mockData.announcement.txid
71
+ }), /inputs must be a non-empty array/)
72
+ })
73
+ })
74
+
75
+ describe('#buildUpdateTransaction (Update mode)', () => {
76
+ it('should build the Update Transaction with cid + endorses in vout[0]', () => {
77
+ const update = mockData.updateTx
78
+ const record = parseUpdateRecord(update)
79
+
80
+ assert.equal(update.mode, 'update')
81
+ assert.equal(update.txid, '843bbea60f1792da7bd1afbbb046dc14f4cbe7f6255142e0809320157387f5cd')
82
+ assert.equal(record.cid, mockData.CID)
83
+ assert.equal(record.endorses, mockData.announcement.txid)
84
+ assert.equal(update.tx.outputs[0].satoshis, 0)
85
+ assert.equal(
86
+ new bitcore.Address(update.tx.outputs[1].script).toString(),
87
+ mockData.updateAddress
88
+ )
89
+ })
90
+ })
91
+
92
+ describe('#buildApprovalTransaction', () => {
93
+ it('should build a 2-of-3 Approval Transaction with the section 5 shape', () => {
94
+ const approval = mockData.approvalTx
95
+ const record = parseApprovalRecord(approval)
96
+
97
+ assert.equal(record.word, 'APPROVAL')
98
+ assert.equal(record.updateTxid, mockData.updateTx.txid)
99
+ assert.equal(approval.txid, '47529d0051a60db6843ad070c443421b7fa65110f0dc89b7af0dfa2571dcc88d')
100
+ assert.isTrue(approval.complete)
101
+ assert.equal(approval.signatures, 2)
102
+
103
+ // vout[0] OP_RETURN, vout[1] = 546 sats to the Reference Address,
104
+ // vout[2] = change to the council P2SH address.
105
+ assert.equal(approval.tx.outputs.length, 3)
106
+ assert.equal(approval.tx.outputs[1].satoshis, 546)
107
+ assert.equal(
108
+ new bitcore.Address(approval.tx.outputs[1].script).toString(),
109
+ mockData.REFERENCE_ADDRESS
110
+ )
111
+ assert.equal(
112
+ new bitcore.Address(approval.tx.outputs[2].script).toString(),
113
+ mockData.council.address
114
+ )
115
+ })
116
+
117
+ it('should produce a partially signed AT when fewer than n keys are supplied', () => {
118
+ const partial = buildApprovalTransaction({
119
+ utxid: mockData.updateTx.txid,
120
+ utxos: [{ txid: 'f'.repeat(64), vout: 0, satoshis: 200000 }],
121
+ council: mockData.council,
122
+ privKeys: [mockData.councilWifs[0]],
123
+ referenceAddress: mockData.REFERENCE_ADDRESS
124
+ })
125
+
126
+ assert.isFalse(partial.complete)
127
+ assert.isFalse(partial.signed)
128
+ assert.equal(partial.signatures, 1)
129
+ // A partially signed transaction must still serialize for the next signer.
130
+ assert.isString(partial.hex)
131
+ assert.isAbove(partial.hex.length, 0)
132
+ })
133
+
134
+ it('should derive the council from pubkeys when no council object is passed', () => {
135
+ const approval = buildApprovalTransaction({
136
+ utxid: mockData.updateTx.txid,
137
+ utxos: [{ txid: 'f'.repeat(64), vout: 0, satoshis: 200000 }],
138
+ pubkeys: mockData.councilPubKeys,
139
+ requiredSignatures: 2,
140
+ privKeys: [mockData.councilWifs[0], mockData.councilWifs[1]],
141
+ referenceAddress: mockData.REFERENCE_ADDRESS
142
+ })
143
+
144
+ assert.equal(approval.council.address, mockData.council.address)
145
+ assert.isTrue(approval.complete)
146
+ })
147
+
148
+ it('should reject a first UTXO that does not belong to the council', () => {
149
+ assert.throws(() => buildApprovalTransaction({
150
+ utxid: mockData.updateTx.txid,
151
+ utxos: [{ txid: 'f'.repeat(64), vout: 0, satoshis: 200000, script: 'a914' + '11'.repeat(20) + '87' }],
152
+ council: mockData.council,
153
+ referenceAddress: mockData.REFERENCE_ADDRESS
154
+ }), /does not lock to the council/)
155
+
156
+ assert.throws(() => buildApprovalTransaction({
157
+ utxid: mockData.updateTx.txid,
158
+ utxos: [{
159
+ txid: 'f'.repeat(64),
160
+ vout: 0,
161
+ satoshis: 200000,
162
+ script: mockData.council.scriptHex,
163
+ address: mockData.REFERENCE_ADDRESS
164
+ }],
165
+ council: mockData.council,
166
+ referenceAddress: mockData.REFERENCE_ADDRESS
167
+ }), /is not the council P2SH address/)
168
+ })
169
+
170
+ it('should reject missing required inputs', () => {
171
+ assert.throws(() => buildApprovalTransaction({ referenceAddress: mockData.REFERENCE_ADDRESS }), /utxid is required/)
172
+ assert.throws(() => buildApprovalTransaction({
173
+ utxid: mockData.updateTx.txid,
174
+ utxos: [{ txid: 'f'.repeat(64), vout: 0, satoshis: 200000 }],
175
+ council: mockData.council
176
+ }), /referenceAddress is required/)
177
+ assert.throws(() => buildApprovalTransaction({
178
+ utxid: mockData.updateTx.txid,
179
+ council: mockData.council,
180
+ referenceAddress: mockData.REFERENCE_ADDRESS
181
+ }), /utxos \(or utxo\) is required/)
182
+ })
183
+ })
184
+
185
+ describe('#multisig signatures', () => {
186
+ it('should count the n-of-m signatures in the AT scriptSig', () => {
187
+ const scriptSig = mockData.approvalTx.tx.inputs[0].script.toHex()
188
+ assert.equal(countMultisigSignatures(scriptSig), 2)
189
+ assert.equal(countMultisigSignatures('00'), 0)
190
+ assert.equal(countMultisigSignatures(''), 0)
191
+ })
192
+
193
+ it('should verify the n-of-m spend with the script interpreter', () => {
194
+ const result = verifyMultisigSpend({
195
+ txHex: mockData.approvalTx.hex,
196
+ inputIndex: 0,
197
+ scriptPubKeyHex: mockData.council.scriptHex,
198
+ satoshis: 200000
199
+ })
200
+
201
+ assert.isTrue(result.valid)
202
+ assert.isNull(result.reason)
203
+ })
204
+
205
+ it('should reject a tampered signature', () => {
206
+ // Flip one hex digit inside the first DER signature.
207
+ const hex = mockData.approvalTx.hex
208
+ const marker = '3044'
209
+ const index = hex.indexOf(marker)
210
+ const tampered = hex.slice(0, index + 10) + (hex[index + 10] === '0' ? '1' : '0') + hex.slice(index + 11)
211
+
212
+ const result = verifyMultisigSpend({
213
+ txHex: tampered,
214
+ inputIndex: 0,
215
+ scriptPubKeyHex: mockData.council.scriptHex,
216
+ satoshis: 200000
217
+ })
218
+
219
+ assert.isFalse(result.valid)
220
+ })
221
+
222
+ it('should require a prevout amount for FORKID verification', () => {
223
+ assert.throws(() => verifyMultisigSpend({
224
+ txHex: mockData.approvalTx.hex,
225
+ scriptPubKeyHex: mockData.council.scriptHex
226
+ }), /satoshis/)
227
+
228
+ const missingInput = verifyMultisigSpend({
229
+ txHex: mockData.approvalTx.hex,
230
+ inputIndex: 5,
231
+ scriptPubKeyHex: mockData.council.scriptHex,
232
+ satoshis: 200000
233
+ })
234
+ assert.isFalse(missingInput.valid)
235
+ assert.equal(missingInput.reason, 'INPUT_INDEX_OUT_OF_RANGE')
236
+ })
237
+ })
238
+
239
+ describe('#validateApproval - happy path', () => {
240
+ it('should validate the full PSP1 -> UT -> AT chain', async () => {
241
+ const result = await validateApproval(withProviders())
242
+
243
+ assert.isTrue(result.valid, JSON.stringify(result.errors))
244
+ assert.isEmpty(result.errors)
245
+ assert.equal(result.atxid, mockData.approvalTx.txid)
246
+ assert.equal(result.utxid, mockData.updateTx.txid)
247
+ assert.equal(result.psp1Txid, mockData.announcement.txid)
248
+
249
+ // All ten section 6 steps recorded.
250
+ assert.deepEqual(result.checks.map(c => c.step), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
251
+
252
+ // The epoch's operative values come from the PSP1 record on chain.
253
+ assert.equal(result.epoch.satsPerMb, mockData.SATS_PER_MB)
254
+ assert.equal(result.epoch.poolCashAddr, mockData.POOL_CASH_ADDR)
255
+ assert.equal(result.epoch.validFromHeight, mockData.VALID_FROM_HEIGHT)
256
+
257
+ // The recomputed council address is the AT's first-vin address.
258
+ assert.equal(result.council.address, mockData.council.address)
259
+ assert.equal(result.council.requiredSignatures, 2)
260
+ assert.equal(result.council.n, 3)
261
+ })
262
+
263
+ it('should accept a later previous epoch (monotonic height)', async () => {
264
+ const result = await validateApproval(withProviders({ prevEpochHeight: mockData.VALID_FROM_HEIGHT - 1 }))
265
+ assert.isTrue(result.valid)
266
+ })
267
+
268
+ it('should accept a membership JSON whose endorses matches the UT', async () => {
269
+ const updateData = clone(mockData.membership)
270
+ updateData.endorses = mockData.announcement.txid
271
+
272
+ const result = await validateApproval(withProviders({ updateData }))
273
+ assert.isTrue(result.valid)
274
+ })
275
+
276
+ it('should accept a historically-held NFT via wasNftHeldBy', async () => {
277
+ const moved = clone(mockData.nftData)
278
+ moved[mockData.NFT_IDS[0]].holder = mockData.REFERENCE_ADDRESS
279
+
280
+ const result = await validateApproval(withProviders({
281
+ getNft: async (tokenId) => moved[tokenId],
282
+ wasNftHeldBy: async ({ tokenId, address }) => tokenId === mockData.NFT_IDS[0] && address === mockData.memberAddresses[0]
283
+ }))
284
+
285
+ assert.isTrue(result.valid, JSON.stringify(result.errors))
286
+ })
287
+ })
288
+
289
+ describe('#validateApproval - section 6 negative steps', () => {
290
+ // ---- step 1: read the PSP1 record from the chain ----
291
+ it('step 1: should fail when the psp1FromChain provider is missing', async () => {
292
+ const args = withProviders()
293
+ delete args.psp1FromChain
294
+
295
+ const result = await validateApproval(args)
296
+ assert.isFalse(result.valid)
297
+ assert.equal(result.errors[0].step, 1)
298
+ assert.equal(result.errors[0].code, 'PSP1_PROVIDER_REQUIRED')
299
+ })
300
+
301
+ it('step 1: should fail when the chain read throws', async () => {
302
+ const result = await validateApproval(withProviders({
303
+ psp1FromChain: async () => { throw new Error('node unreachable') }
304
+ }))
305
+
306
+ assert.isFalse(result.valid)
307
+ assert.equal(result.errors[0].code, 'PSP1_READ_FAILED')
308
+ assert.include(result.errors[0].message, 'node unreachable')
309
+ })
310
+
311
+ it('step 1: should fail when the output is not a PSP1 record', async () => {
312
+ const result = await validateApproval(withProviders({
313
+ psp1FromChain: async () => mockData.approvalDetails
314
+ }))
315
+
316
+ assert.isFalse(result.valid)
317
+ assert.equal(result.errors[0].code, 'PSP1_MALFORMED')
318
+ })
319
+
320
+ it('step 1: should fail on a malformed PSP1 record (unknown version)', async () => {
321
+ const badHex = mockData.psp1Record.scriptHex.replace('505350310101', '505350310102')
322
+
323
+ const result = await validateApproval(withProviders({
324
+ psp1FromChain: async () => ({
325
+ txid: mockData.announcement.txid,
326
+ vin: [{ address: mockData.council.address }],
327
+ vout: [{ scriptPubKey: { hex: badHex } }]
328
+ })
329
+ }))
330
+
331
+ assert.isFalse(result.valid)
332
+ assert.equal(result.errors[0].code, 'PSP1_UNKNOWN_VERSION')
333
+ })
334
+
335
+ it('step 1: should reject decoded fields with an unknown version', async () => {
336
+ const decoded = Object.assign({}, mockData.psp1Record, {
337
+ version: 2,
338
+ vin: [{ address: mockData.council.address }]
339
+ })
340
+ delete decoded.scriptHex
341
+
342
+ const result = await validateApproval(withProviders({
343
+ psp1FromChain: async () => decoded
344
+ }))
345
+
346
+ assert.isFalse(result.valid)
347
+ assert.equal(result.errors[0].code, 'PSP1_UNKNOWN_VERSION')
348
+ })
349
+
350
+ // ---- step 2: height monotonicity ----
351
+ it('step 2: should reject a non-increasing validFromHeight', async () => {
352
+ const result = await validateApproval(withProviders({ prevEpochHeight: mockData.VALID_FROM_HEIGHT }))
353
+
354
+ assert.isFalse(result.valid)
355
+ assert.equal(result.errors[0].step, 2)
356
+ assert.equal(result.errors[0].code, 'HEIGHT_NOT_MONOTONIC')
357
+ })
358
+
359
+ // ---- step 3: the Approval Transaction ----
360
+ it('step 3: should fail when the AT can not be read', async () => {
361
+ const result = await validateApproval(withProviders({
362
+ getTx: async (txid) => txid === mockData.approvalTx.txid ? null : undefined
363
+ }))
364
+
365
+ assert.isFalse(result.valid)
366
+ assert.equal(result.errors[0].code, 'APPROVAL_TX_READ_FAILED')
367
+ })
368
+
369
+ it('step 3: should reject an AT that approves a different Update Transaction', async () => {
370
+ const result = await validateApproval(withProviders({ utxid: mockData.OTHER_TXID }))
371
+
372
+ assert.isFalse(result.valid)
373
+ assert.equal(result.errors[0].code, 'AT_REFERENCES_DIFFERENT_UPDATE')
374
+ })
375
+
376
+ it('step 3: should reject a transaction with no APPROVAL record', async () => {
377
+ const result = await validateApproval(withProviders({
378
+ getTx: async (txid) => {
379
+ if (txid === mockData.approvalTx.txid) return mockData.updateDetails
380
+ return mockData.announcementDetails.txid === txid ? mockData.announcementDetails : mockData.updateDetails
381
+ }
382
+ }))
383
+
384
+ assert.isFalse(result.valid)
385
+ assert.equal(result.errors[0].code, 'NOT_AN_APPROVAL_TX')
386
+ })
387
+
388
+ it('step 3: should reject an AT missing the 546-sat Reference Address dust output', async () => {
389
+ const result = await validateApproval(withProviders({ referenceAddress: mockData.OTHER_TXID }))
390
+
391
+ assert.isFalse(result.valid)
392
+ assert.equal(result.errors[0].code, 'APPROVAL_DUST_OUTPUT_INVALID')
393
+ })
394
+
395
+ it('step 3: should reject an AT that is not the latest APPROVAL tx', async () => {
396
+ const newer = buildApprovalTransaction({
397
+ utxid: mockData.updateTx.txid,
398
+ utxos: [{ txid: '9'.repeat(64), vout: 0, satoshis: 200000 }],
399
+ council: mockData.council,
400
+ privKeys: [mockData.councilWifs[0], mockData.councilWifs[1]],
401
+ referenceAddress: mockData.REFERENCE_ADDRESS
402
+ })
403
+ const newerDetails = mockData.txDetails(newer, { prevTxid: '9'.repeat(64), prevAddress: mockData.council.address, prevSatoshis: 200000 })
404
+
405
+ const result = await validateApproval(withProviders({
406
+ getTx: async (txid) => {
407
+ if (txid === newer.txid) return newerDetails
408
+ if (txid === mockData.approvalTx.txid) return mockData.approvalDetails
409
+ if (txid === mockData.updateTx.txid) return mockData.updateDetails
410
+ if (txid === mockData.announcement.txid) return mockData.announcementDetails
411
+ return null
412
+ },
413
+ getTxHistory: async () => [
414
+ { tx_hash: newer.txid, height: 200 },
415
+ { tx_hash: mockData.approvalTx.txid, height: 100 }
416
+ ]
417
+ }))
418
+
419
+ assert.isFalse(result.valid)
420
+ assert.equal(result.errors[0].code, 'NOT_LATEST_APPROVAL')
421
+ })
422
+
423
+ // ---- step 4: the Update Transaction and membership JSON ----
424
+ it('step 4: should fail when the Update Transaction can not be read', async () => {
425
+ const result = await validateApproval(withProviders({
426
+ getTx: async (txid) => {
427
+ if (txid === mockData.approvalTx.txid) return mockData.approvalDetails
428
+ return null
429
+ }
430
+ }))
431
+
432
+ assert.isFalse(result.valid)
433
+ assert.equal(result.errors[0].code, 'UPDATE_TX_READ_FAILED')
434
+ })
435
+
436
+ it('step 4: should fail closed without a CID provider', async () => {
437
+ const args = withProviders()
438
+ delete args.getCidData
439
+
440
+ const result = await validateApproval(args)
441
+ assert.isFalse(result.valid)
442
+ assert.equal(result.errors[0].code, 'CID_PROVIDER_REQUIRED')
443
+ })
444
+
445
+ it('step 4: should fail when the CID can not be fetched', async () => {
446
+ const result = await validateApproval(withProviders({
447
+ getCidData: async () => { throw new Error('gateway timeout') }
448
+ }))
449
+
450
+ assert.isFalse(result.valid)
451
+ assert.equal(result.errors[0].code, 'CID_FETCH_FAILED')
452
+ })
453
+
454
+ it('step 4: should reject a malformed Update Transaction record', async () => {
455
+ const malformed = {
456
+ txid: mockData.updateTx.txid,
457
+ hex: mockData.updateTx.hex,
458
+ vin: [{ address: mockData.updateAddress }],
459
+ vout: [{ scriptPubKey: { hex: opReturnScript([Buffer.from('{bad json', 'utf8')]) } }]
460
+ }
461
+
462
+ const result = await validateApproval(withProviders({
463
+ getTx: async (txid) => {
464
+ if (txid === mockData.updateTx.txid) return malformed
465
+ if (txid === mockData.approvalTx.txid) return mockData.approvalDetails
466
+ if (txid === mockData.announcement.txid) return mockData.announcementDetails
467
+ return null
468
+ }
469
+ }))
470
+
471
+ assert.isFalse(result.valid)
472
+ assert.equal(result.errors[0].code, 'UPDATE_RECORD_MALFORMED')
473
+ })
474
+
475
+ // ---- step 5: Group Token ----
476
+ it('step 5: should reject a different Group Token', async () => {
477
+ const result = await validateApproval(withProviders({
478
+ council: {
479
+ groupTokenId: 'f'.repeat(64),
480
+ requiredSignatures: 2
481
+ }
482
+ }))
483
+
484
+ assert.isFalse(result.valid)
485
+ assert.equal(result.errors[0].step, 5)
486
+ assert.equal(result.errors[0].code, 'GROUP_TOKEN_MISMATCH')
487
+ })
488
+
489
+ it('step 5: should reject a membership document with no Group Token', async () => {
490
+ const updateData = clone(mockData.membership)
491
+ delete updateData.groupTokenId
492
+
493
+ const result = await validateApproval(withProviders({ updateData }))
494
+ assert.equal(result.errors[0].code, 'GROUP_TOKEN_MISSING')
495
+ })
496
+
497
+ // ---- step 6: NFTs are children of the Group Token ----
498
+ it('step 6: should reject an NFT from a different group', async () => {
499
+ const wrongGroup = clone(mockData.nftData)
500
+ wrongGroup[mockData.NFT_IDS[1]].groupTokenId = 'f'.repeat(64)
501
+
502
+ const result = await validateApproval(withProviders({
503
+ getNft: async (tokenId) => wrongGroup[tokenId]
504
+ }))
505
+
506
+ assert.isFalse(result.valid)
507
+ assert.equal(result.errors[0].step, 6)
508
+ assert.equal(result.errors[0].code, 'NFT_NOT_IN_GROUP')
509
+ })
510
+
511
+ it('step 6: should fail closed when the NFT group can not be established', async () => {
512
+ const noGroup = clone(mockData.nftData)
513
+ delete noGroup[mockData.NFT_IDS[0]].groupTokenId
514
+
515
+ const result = await validateApproval(withProviders({
516
+ getNft: async (tokenId) => noGroup[tokenId]
517
+ }))
518
+
519
+ assert.isFalse(result.valid)
520
+ assert.equal(result.errors[0].code, 'NFT_GROUP_UNKNOWN')
521
+ })
522
+
523
+ it('step 6: should reject a membership document with no operators', async () => {
524
+ const updateData = clone(mockData.membership)
525
+ updateData.nfts = []
526
+
527
+ const result = await validateApproval(withProviders({ updateData }))
528
+ assert.equal(result.errors[0].code, 'NO_OPERATOR_NFTS')
529
+ })
530
+
531
+ // ---- step 7: address derived from public key ----
532
+ it('step 7: should reject an address that is not derived from the public key', async () => {
533
+ const updateData = clone(mockData.membership)
534
+ updateData.nfts[0].address = mockData.memberAddresses[1]
535
+
536
+ const result = await validateApproval(withProviders({ updateData }))
537
+ assert.equal(result.errors[0].step, 7)
538
+ assert.equal(result.errors[0].code, 'ADDRESS_PUBKEY_MISMATCH')
539
+ })
540
+
541
+ it('step 7: should reject an entry missing its public key', async () => {
542
+ const updateData = clone(mockData.membership)
543
+ delete updateData.nfts[2].pubKey
544
+
545
+ const result = await validateApproval(withProviders({ updateData }))
546
+ assert.equal(result.errors[0].code, 'ADDRESS_PUBKEY_MISSING')
547
+ })
548
+
549
+ // ---- step 8: address holds the NFT ----
550
+ it('step 8: should reject an address that does not hold the NFT', async () => {
551
+ const moved = clone(mockData.nftData)
552
+ moved[mockData.NFT_IDS[0]].holder = mockData.REFERENCE_ADDRESS
553
+
554
+ const result = await validateApproval(withProviders({
555
+ getNft: async (tokenId) => moved[tokenId]
556
+ }))
557
+
558
+ assert.isFalse(result.valid)
559
+ assert.equal(result.errors[0].step, 8)
560
+ assert.equal(result.errors[0].code, 'NFT_NOT_HELD')
561
+ })
562
+
563
+ it('step 8: should fail closed without an NFT provider', async () => {
564
+ // Group membership is supplied in the JSON, but current NFT ownership
565
+ // can only come from chain. Without a provider, step 8 must fail closed.
566
+ const updateData = clone(mockData.membership)
567
+ updateData.nfts.forEach(entry => { entry.groupTokenId = mockData.GROUP_TOKEN_ID })
568
+
569
+ const result = await validateApproval(withProviders({ updateData, getNft: undefined }))
570
+ assert.equal(result.errors[0].step, 8)
571
+ assert.equal(result.errors[0].code, 'NFT_PROVIDER_REQUIRED')
572
+ })
573
+
574
+ // ---- step 9: multisig address + n-of-m signatures ----
575
+ it('step 9: should reject insufficient council signatures', async () => {
576
+ const partial = buildApprovalTransaction({
577
+ utxid: mockData.updateTx.txid,
578
+ utxos: [{ txid: mockData.AT_FUNDING_TXID, vout: 0, satoshis: 200000 }],
579
+ council: mockData.council,
580
+ privKeys: [mockData.councilWifs[0]],
581
+ referenceAddress: mockData.REFERENCE_ADDRESS
582
+ })
583
+ const partialDetails = mockData.txDetails(partial, {
584
+ prevTxid: mockData.AT_FUNDING_TXID,
585
+ prevAddress: mockData.council.address,
586
+ prevSatoshis: 200000
587
+ })
588
+
589
+ const result = await validateApproval(withProviders({
590
+ atxid: partial.txid,
591
+ getTx: async (txid) => {
592
+ if (txid === partial.txid) return partialDetails
593
+ if (txid === mockData.updateTx.txid) return mockData.updateDetails
594
+ if (txid === mockData.announcement.txid) return mockData.announcementDetails
595
+ return null
596
+ }
597
+ }))
598
+
599
+ assert.isFalse(result.valid)
600
+ assert.equal(result.errors[0].step, 9)
601
+ assert.equal(result.errors[0].code, 'INSUFFICIENT_SIGNATURES')
602
+ })
603
+
604
+ it('step 9: should reject a published council address that does not match', async () => {
605
+ const result = await validateApproval(withProviders({
606
+ council: {
607
+ groupTokenId: mockData.GROUP_TOKEN_ID,
608
+ requiredSignatures: 2,
609
+ address: mockData.README_COUNCIL.address
610
+ }
611
+ }))
612
+
613
+ assert.equal(result.errors[0].code, 'COUNCIL_ADDRESS_MISMATCH')
614
+ })
615
+
616
+ it('step 9: should reject a threshold mismatch', async () => {
617
+ const updateData = clone(mockData.membership)
618
+ updateData.requiredSignatures = 1
619
+
620
+ const result = await validateApproval(withProviders({ updateData }))
621
+ assert.equal(result.errors[0].code, 'THRESHOLD_MISMATCH')
622
+ })
623
+
624
+ it('step 9: should require a threshold', async () => {
625
+ const updateData = clone(mockData.membership)
626
+ delete updateData.requiredSignatures
627
+
628
+ const result = await validateApproval(withProviders({
629
+ council: { groupTokenId: mockData.GROUP_TOKEN_ID },
630
+ updateData
631
+ }))
632
+
633
+ assert.equal(result.errors[0].code, 'THRESHOLD_MISSING')
634
+ })
635
+
636
+ it('step 9: should reject an AT whose first vin is not the council address', async () => {
637
+ // Membership lists only two of the three operators with a 2-of-2
638
+ // threshold, so the recomputed address differs from the AT's 2-of-3
639
+ // address. The AT is still fully signed.
640
+ const updateData = clone(mockData.membership)
641
+ delete updateData.multisigAddr
642
+ updateData.nfts = updateData.nfts.slice(0, 2)
643
+ updateData.requiredSignatures = 2
644
+
645
+ const result = await validateApproval(withProviders({
646
+ council: { groupTokenId: mockData.GROUP_TOKEN_ID },
647
+ updateData
648
+ }))
649
+
650
+ assert.isFalse(result.valid)
651
+ assert.equal(result.errors[0].code, 'AT_FIRST_VIN_NOT_COUNCIL')
652
+ })
653
+
654
+ // ---- step 10: `endorses` linkage + PS010 F9 ----
655
+ it('step 10: should reject an endorses that names another PSP1 announcement', async () => {
656
+ // The on-chain Update Transaction carries no `endorses`; the membership
657
+ // JSON names a different announcement. That must be a mismatch, not a
658
+ // conflict (nothing on chain disagrees).
659
+ const noEndorse = buildUpdateTransaction({
660
+ cid: mockData.CID,
661
+ ts: mockData.UPDATE_TS,
662
+ inputs: [{
663
+ txid: '7'.repeat(64),
664
+ vout: 0,
665
+ satoshis: 50000,
666
+ script: new bitcore.Script(mockData.updateKey.toAddress()).toHex(),
667
+ address: mockData.updateAddress
668
+ }],
669
+ privKeys: [mockData.updateKey],
670
+ changeAddress: mockData.updateAddress
671
+ })
672
+ const noEndorseDetails = mockData.txDetails(noEndorse, {
673
+ prevTxid: '7'.repeat(64),
674
+ prevAddress: mockData.updateAddress,
675
+ prevSatoshis: 50000
676
+ })
677
+ const updateData = clone(mockData.membership)
678
+ updateData.endorses = mockData.OTHER_TXID
679
+
680
+ const result = await validateApproval(withProviders({
681
+ updateData,
682
+ getTx: async (txid) => {
683
+ if (txid === mockData.updateTx.txid) return noEndorseDetails
684
+ if (txid === mockData.approvalTx.txid) return mockData.approvalDetails
685
+ if (txid === mockData.announcement.txid) return mockData.announcementDetails
686
+ return null
687
+ }
688
+ }))
689
+
690
+ assert.isFalse(result.valid)
691
+ assert.equal(result.errors[0].step, 10)
692
+ assert.equal(result.errors[0].code, 'ENDORSES_MISMATCH')
693
+ })
694
+
695
+ it('step 10: should reject conflicting on-chain and off-chain endorses fields', async () => {
696
+ const updateData = clone(mockData.membership)
697
+ updateData.endorses = mockData.OTHER_TXID
698
+
699
+ const result = await validateApproval(withProviders({ updateData }))
700
+ assert.isFalse(result.valid)
701
+
702
+ // The on-chain UT endorses the announcement, so the membership JSON's
703
+ // conflicting value must be reported distinctly.
704
+ assert.equal(result.errors[0].code, 'ENDORSES_CONFLICT')
705
+ })
706
+
707
+ it('step 10: should reject a missing endorses field', async () => {
708
+ const noEndorse = buildUpdateTransaction({
709
+ cid: mockData.CID,
710
+ ts: mockData.UPDATE_TS,
711
+ inputs: [{
712
+ txid: '8'.repeat(64),
713
+ vout: 0,
714
+ satoshis: 50000,
715
+ script: new bitcore.Script(mockData.updateKey.toAddress()).toHex(),
716
+ address: mockData.updateAddress
717
+ }],
718
+ privKeys: [mockData.updateKey],
719
+ changeAddress: mockData.updateAddress
720
+ })
721
+ const noEndorseDetails = mockData.txDetails(noEndorse, {
722
+ prevTxid: '8'.repeat(64),
723
+ prevAddress: mockData.updateAddress,
724
+ prevSatoshis: 50000
725
+ })
726
+ const updateData = clone(mockData.membership)
727
+ delete updateData.endorses
728
+
729
+ const result = await validateApproval(withProviders({
730
+ updateData,
731
+ getTx: async (txid) => {
732
+ if (txid === noEndorse.txid) return noEndorseDetails
733
+ if (txid === mockData.updateTx.txid) return noEndorseDetails
734
+ if (txid === mockData.approvalTx.txid) return mockData.approvalDetails
735
+ if (txid === mockData.announcement.txid) return mockData.announcementDetails
736
+ return null
737
+ }
738
+ }))
739
+
740
+ assert.equal(result.errors[0].code, 'ENDORSES_MISSING')
741
+ })
742
+
743
+ it('step 10: should reject a PSP1 announcement whose first vin is not the council (F9)', async () => {
744
+ const wrongVin = clone(mockData.announcementDetails)
745
+ wrongVin.vin[0].address = mockData.updateAddress
746
+
747
+ const result = await validateApproval(withProviders({
748
+ psp1FromChain: async () => wrongVin
749
+ }))
750
+
751
+ assert.isFalse(result.valid)
752
+ assert.equal(result.errors[0].step, 10)
753
+ assert.equal(result.errors[0].code, 'PSP1_FIRST_VIN_NOT_COUNCIL')
754
+ assert.include(result.errors[0].message, 'F9')
755
+ })
756
+
757
+ it('step 10: should fail closed when the PSP1 first vin is unknown', async () => {
758
+ const result = await validateApproval(withProviders({
759
+ psp1FromChain: async () => mockData.psp1Record
760
+ }))
761
+
762
+ assert.equal(result.errors[0].code, 'PSP1_FIRST_VIN_UNKNOWN')
763
+ })
764
+ })
765
+
766
+ describe('#validateApproval - argument guards', () => {
767
+ it('should throw without atxid, council.groupTokenId, or psp1Txid', async () => {
768
+ try {
769
+ await validateApproval({})
770
+ assert.fail('Unexpected code path')
771
+ } catch (err) {
772
+ assert.include(err.message, 'atxid is required')
773
+ }
774
+
775
+ try {
776
+ await validateApproval({ atxid: mockData.approvalTx.txid })
777
+ assert.fail('Unexpected code path')
778
+ } catch (err) {
779
+ assert.include(err.message, 'council.groupTokenId is required')
780
+ }
781
+
782
+ try {
783
+ await validateApproval({
784
+ atxid: mockData.approvalTx.txid,
785
+ council: { groupTokenId: mockData.GROUP_TOKEN_ID }
786
+ })
787
+ assert.fail('Unexpected code path')
788
+ } catch (err) {
789
+ assert.include(err.message, 'psp1Txid is required')
790
+ }
791
+ })
792
+
793
+ it('should derive the council address itself when council.address is omitted', async () => {
794
+ const result = await validateApproval(withProviders({
795
+ council: { groupTokenId: mockData.GROUP_TOKEN_ID, requiredSignatures: 2 }
796
+ }))
797
+
798
+ assert.isTrue(result.valid, JSON.stringify(result.errors))
799
+ assert.equal(result.council.address, deriveCouncilAddress({
800
+ pubkeys: mockData.councilPubKeys,
801
+ requiredSignatures: 2
802
+ }).address)
803
+ })
804
+ })
805
+ })