psf-multisig-approval 2.2.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,407 @@
1
+ /*
2
+ PS009 v2 - spec byte-layout vectors.
3
+
4
+ Every expected value here is either lifted from the PS009 v2 / PS010 specs or
5
+ is a fixed output of this reference implementation, asserted byte-for-byte so
6
+ that an accidental wire-format change fails loudly.
7
+ */
8
+
9
+ // npm libraries
10
+ import { assert } from 'chai'
11
+ import bitcore from '@chris.troutner/bitcore-lib-cash'
12
+
13
+ // Local libraries
14
+ import {
15
+ deriveCouncilAddress,
16
+ deriveAddressFromPubkey,
17
+ normalizeAddress,
18
+ addressesEqual,
19
+ encodePsP1,
20
+ parsePsP1Record,
21
+ isPsP1Record,
22
+ buildApprovalOpReturn,
23
+ parseApprovalRecord,
24
+ buildUpdateOpReturn,
25
+ parseUpdateRecord
26
+ } from '../../lib/ps009v2/index.js'
27
+ import { opReturnScript } from '../../lib/ps009v2/script.js'
28
+ import * as mockData from './mocks/ps009v2-mocks.js'
29
+
30
+ // PS010 section 4.1 fixture: genesis announcement.
31
+ const PSP1_GENESIS_HEX = '6a0450535031010136626974636f696e636173683a707a7970646633777638397a7937356d766a6a346165746e36336e71387363336375666d6e6b7a7a6a7108c0090e000000000008d007000000000000200000000000000000000000000000000000000000000000000000000000000000'
32
+
33
+ // PS010 section 4.1 fixture with a non-genesis prevRegistryTxid. The display
34
+ // txid f8ea1fcd...4565f must be reversed into internal byte order on the wire.
35
+ const PSP1_CHAINED_HEX = '6a0450535031010136626974636f696e636173683a707a7970646633777638397a7937356d766a6a346165746e36336e71387363336375666d6e6b7a7a6a7108c0090e000000000008d007000000000000205ff64d25931d320d358bc3dc5f3dacd5f3634f6a1c25c662fdad8144cd1feaf8'
36
+ const CHAINED_PREV_TXID = 'f8ea1fcd4481adfd62c6251c6a4f63f3d5ac3d5fdcc38b350d321d93254df65f'
37
+
38
+ // PS009 v2 section 5.1 example: OP_RETURN APPROVAL 6eaa12d2...730a
39
+ const APPROVAL_TXID = '6eaa12d2749cf15b00873551088975dd6748d9a6f867426db914735c58ad730a'
40
+ const APPROVAL_OP_RETURN_HEX = '6a08415050524f56414c4036656161313264323734396366313562303038373335353130383839373564643637343864396136663836373432366462393134373335633538616437333061'
41
+
42
+ // PS009 v2 section 4.1 example Update Transaction OP_RETURN.
43
+ const UPDATE_CID = 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi'
44
+ const UPDATE_JSON = `{"cid":"${UPDATE_CID}","endorses":"${APPROVAL_TXID}","ts":1770000000000}`
45
+ const UPDATE_OP_RETURN_HEX = '6a4ca67b22636964223a2262616679626569676479727a74357366703775646d37687537367568377932366e6633656675796c71616266336f636c67747179353566627a6469222c22656e646f72736573223a2236656161313264323734396366313562303038373335353130383839373564643637343864396136663836373432366462393134373335633538616437333061222c227473223a313737303030303030303030307d'
46
+
47
+ // A real PS009 v1 Update Transaction OP_RETURN (from the repo's own fixtures):
48
+ // OP_RETURN OP_0 OP_PUSHDATA1 <88-byte JSON>.
49
+ const V1_UPDATE_OP_RETURN_HEX = '6a004c587b22636964223a226261667962656962356436733674337471346c687770326f63767a377932777334637a676b726d686c687635793561657968366271726d73787869222c227473223a313637363536303234373136387d'
50
+ const V1_UPDATE_CID = 'bafybeib5d6s6t3tq4lhwp2ocvz7y2ws4czgkrmhlhv5y5aeyh6bqrmsxxi'
51
+
52
+ describe('#ps009v2 spec vectors', () => {
53
+ describe('#deriveCouncilAddress', () => {
54
+ it('should reproduce the README 3-of-5 council address and script byte-for-byte', () => {
55
+ const result = mockData.README_COUNCIL
56
+
57
+ assert.equal(result.address, 'bitcoincash:pqntzt6wcp38h8ud68wjnwh437uek76lhvhlwcm4fj')
58
+ assert.equal(result.scriptHex, 'a91426b12f4ec0627b9f8dd1dd29baf58fb99b7b5fbb87')
59
+ assert.equal(result.redeemScript, '5321021ca211a04a1d489ae77e01c28c97b02e733893890fda00a359ca8956c2e0d25921024be54accec310c2636140336ae548d610ba9b7ce300b3f42494b4a6f2963731f21033c930d4cff4ba68a70f7a21443e20ad4176173380d21f8c3ce27f7ce947f3246210361fd21512b9072e8f6b984d9b04c57e5779867c2ad002999372268770fcb26742103b9bdc40c478ab0536be29c368b26c48a5e8d6867fc34b77c727ab690365aae9155ae')
60
+ assert.equal(result.requiredSignatures, 3)
61
+ assert.equal(result.n, 5)
62
+ })
63
+
64
+ it('should sort public keys lexicographically before derivation', () => {
65
+ const result = mockData.README_COUNCIL
66
+
67
+ assert.deepEqual(result.publicKeys, mockData.README_PUBKEYS.slice().sort())
68
+
69
+ // Same address regardless of the order the operator keys are supplied in.
70
+ const reversed = deriveCouncilAddress({
71
+ pubkeys: mockData.README_PUBKEYS.slice().reverse(),
72
+ requiredSignatures: 3
73
+ })
74
+ assert.equal(reversed.address, result.address)
75
+ assert.equal(reversed.scriptHex, result.scriptHex)
76
+ assert.equal(reversed.redeemScript, result.redeemScript)
77
+ })
78
+
79
+ it('should default the threshold to 50% + 1 like PS009 v1', () => {
80
+ const result = deriveCouncilAddress({ pubkeys: mockData.README_PUBKEYS })
81
+ assert.equal(result.requiredSignatures, 3)
82
+ assert.equal(result.n, 5)
83
+ })
84
+
85
+ it('should derive a deterministic 2-of-3 council fixture', () => {
86
+ const result = mockData.council
87
+
88
+ assert.equal(result.address, 'bitcoincash:pze4t4mxj0k66hxnn664p3m4ak9hmhdlmgw787kmw9')
89
+ assert.equal(result.scriptHex, 'a914b355d76693edad5cd39eb550c775ed8b7dddbfda87')
90
+ assert.equal(result.requiredSignatures, 2)
91
+ assert.equal(result.redeemScript.slice(0, 2), '52')
92
+ assert.equal(result.redeemScript.slice(-2), 'ae')
93
+ })
94
+
95
+ it('should derive a testnet council address', () => {
96
+ const result = deriveCouncilAddress({
97
+ pubkeys: mockData.councilPubKeys,
98
+ requiredSignatures: 2,
99
+ network: 'testnet'
100
+ })
101
+
102
+ assert.match(result.address, /^bchtest:p/)
103
+ assert.equal(result.network, 'testnet')
104
+ })
105
+
106
+ it('should throw on missing, invalid, duplicate, or uncompressed pubkeys', () => {
107
+ assert.throws(() => deriveCouncilAddress({}), /pubkeys must be a non-empty array/)
108
+ assert.throws(() => deriveCouncilAddress({ pubkeys: [] }), /pubkeys must be a non-empty array/)
109
+ assert.throws(() => deriveCouncilAddress({ pubkeys: ['zz'] }), /compressed public key/)
110
+ assert.throws(() => deriveCouncilAddress({ pubkeys: [mockData.councilPubKeys[0], mockData.councilPubKeys[0]] }), /Duplicate public key/)
111
+ const uncompressed = '04' + '11'.repeat(64)
112
+ assert.throws(() => deriveCouncilAddress({ pubkeys: [uncompressed] }), /compressed public key/)
113
+ })
114
+
115
+ it('should throw on an invalid threshold', () => {
116
+ assert.throws(() => deriveCouncilAddress({ pubkeys: mockData.councilPubKeys, requiredSignatures: 0 }), /at least 1/)
117
+ assert.throws(() => deriveCouncilAddress({ pubkeys: mockData.councilPubKeys, requiredSignatures: 4 }), /can not exceed/)
118
+ assert.throws(() => deriveCouncilAddress({ pubkeys: mockData.councilPubKeys, requiredSignatures: 1.5 }), /must be an integer/)
119
+ })
120
+
121
+ it('should derive a P2PKH address from a public key (README vector)', () => {
122
+ assert.equal(
123
+ deriveAddressFromPubkey('021ca211a04a1d489ae77e01c28c97b02e733893890fda00a359ca8956c2e0d259'),
124
+ 'bitcoincash:qz4zsa22glal5c4jqm748xge657gwuw2d5t9nvg64p'
125
+ )
126
+ })
127
+
128
+ it('should normalize token-aware and mixed-case addresses', () => {
129
+ const address = 'bitcoincash:qrwe6kxhvu47ve6jvgrf2d93w0q38av7s5xm9xfehr'
130
+ assert.equal(normalizeAddress('simpleledger:qrwe6kxhvu47ve6jvgrf2d93w0q38av7s5xm9xfehr'), address)
131
+ assert.isTrue(addressesEqual(address.toUpperCase(), address))
132
+ assert.isFalse(addressesEqual(address, mockData.council.address))
133
+ })
134
+ })
135
+
136
+ describe('#PSP1 codec (PS010 section 4.1)', () => {
137
+ it('should encode the genesis PSP1 record byte-for-byte', () => {
138
+ const record = encodePsP1({
139
+ poolCashAddr: mockData.POOL_CASH_ADDR,
140
+ validFromHeight: mockData.VALID_FROM_HEIGHT,
141
+ satsPerMb: mockData.SATS_PER_MB,
142
+ prevRegistryTxid: mockData.PREV_REGISTRY_TXID
143
+ })
144
+
145
+ assert.equal(record.scriptHex, PSP1_GENESIS_HEX)
146
+ })
147
+
148
+ it('should encode the chained record with prevRegistryTxid in internal byte order', () => {
149
+ const record = encodePsP1({
150
+ poolCashAddr: mockData.POOL_CASH_ADDR,
151
+ validFromHeight: mockData.VALID_FROM_HEIGHT,
152
+ satsPerMb: mockData.SATS_PER_MB,
153
+ prevRegistryTxid: CHAINED_PREV_TXID
154
+ })
155
+
156
+ assert.equal(record.scriptHex, PSP1_CHAINED_HEX)
157
+ // The wire bytes are the reverse of the displayed txid.
158
+ assert.equal(
159
+ record.scriptHex.slice(-64),
160
+ Buffer.from(CHAINED_PREV_TXID, 'hex').reverse().toString('hex')
161
+ )
162
+ })
163
+
164
+ it('should parse both fixtures back to the same fields', () => {
165
+ for (const fixture of [
166
+ { hex: PSP1_GENESIS_HEX, prev: mockData.PREV_REGISTRY_TXID },
167
+ { hex: PSP1_CHAINED_HEX, prev: CHAINED_PREV_TXID }
168
+ ]) {
169
+ const record = parsePsP1Record(fixture.hex)
170
+
171
+ assert.equal(record.lokad, 'PSP1')
172
+ assert.equal(record.version, 1)
173
+ assert.equal(record.poolCashAddr, mockData.POOL_CASH_ADDR)
174
+ assert.equal(record.validFromHeight, mockData.VALID_FROM_HEIGHT)
175
+ assert.equal(record.satsPerMb, mockData.SATS_PER_MB)
176
+ assert.equal(record.prevRegistryTxid, fixture.prev)
177
+ }
178
+ })
179
+
180
+ it('should detect PSP1 with the 6a0450535031 prefix', () => {
181
+ assert.isTrue(isPsP1Record(PSP1_GENESIS_HEX))
182
+ assert.isFalse(isPsP1Record('6a0400510000'))
183
+ assert.isFalse(isPsP1Record('76a914' + '00'.repeat(20) + '88ac'))
184
+ })
185
+
186
+ it('should parse a PSP1 record from a transaction object and from ASM', () => {
187
+ const fromTx = parsePsP1Record({
188
+ txid: mockData.announcement.txid,
189
+ vout: [{ scriptPubKey: { hex: PSP1_GENESIS_HEX } }]
190
+ })
191
+ assert.equal(fromTx.validFromHeight, mockData.VALID_FROM_HEIGHT)
192
+
193
+ const asm = new bitcore.Script(PSP1_GENESIS_HEX).toASM()
194
+ const fromAsm = parsePsP1Record(asm)
195
+ assert.equal(fromAsm.poolCashAddr, mockData.POOL_CASH_ADDR)
196
+ })
197
+
198
+ it('should return null for non-PSP1 outputs', () => {
199
+ assert.isNull(parsePsP1Record('6a0400510000'))
200
+ assert.isNull(parsePsP1Record('76a914' + '00'.repeat(20) + '88ac'))
201
+ assert.isNull(parsePsP1Record('6a'))
202
+ })
203
+
204
+ it('should reject an unknown PSP1 version', () => {
205
+ const version2 = PSP1_GENESIS_HEX.replace('505350310101', '505350310102')
206
+
207
+ try {
208
+ parsePsP1Record(version2)
209
+ assert.fail('Unexpected code path')
210
+ } catch (err) {
211
+ assert.equal(err.code, 'ERR_PSP1_UNKNOWN_VERSION')
212
+ }
213
+ })
214
+
215
+ it('should reject a truncated validFromHeight (PS010 requires 8 bytes)', () => {
216
+ const bad = opReturnScript([
217
+ Buffer.from('50535031', 'hex'),
218
+ Buffer.from([1]),
219
+ Buffer.from(mockData.POOL_CASH_ADDR, 'utf8'),
220
+ Buffer.from('c0090e00', 'hex'),
221
+ Buffer.from('d007000000000000', 'hex'),
222
+ Buffer.alloc(32)
223
+ ])
224
+
225
+ try {
226
+ parsePsP1Record(bad)
227
+ assert.fail('Unexpected code path')
228
+ } catch (err) {
229
+ assert.equal(err.code, 'ERR_PSP1_MALFORMED')
230
+ assert.include(err.message, 'validFromHeight')
231
+ }
232
+ })
233
+
234
+ it('should reject a truncated push instead of truncating it', () => {
235
+ try {
236
+ parsePsP1Record(PSP1_GENESIS_HEX.slice(0, 200))
237
+ assert.fail('Unexpected code path')
238
+ } catch (err) {
239
+ assert.equal(err.code, 'ERR_PSP1_MALFORMED')
240
+ }
241
+ })
242
+
243
+ it('should reject a poolCashAddr without the network prefix', () => {
244
+ const bad = opReturnScript([
245
+ Buffer.from('50535031', 'hex'),
246
+ Buffer.from([1]),
247
+ Buffer.from('pzypdf3wv89zy75mvjj4aetn63nq8sc3cufmnkzzjq', 'utf8'),
248
+ Buffer.from('c0090e0000000000', 'hex'),
249
+ Buffer.from('d007000000000000', 'hex'),
250
+ Buffer.alloc(32)
251
+ ])
252
+
253
+ try {
254
+ parsePsP1Record(bad)
255
+ assert.fail('Unexpected code path')
256
+ } catch (err) {
257
+ assert.equal(err.code, 'ERR_PSP1_MALFORMED')
258
+ }
259
+ })
260
+
261
+ it('should throw on encode with a missing prefix or bad uint64', () => {
262
+ assert.throws(() => encodePsP1({
263
+ poolCashAddr: 'pzypdf3wv89zy75mvjj4aetn63nq8sc3cufmnkzzjq',
264
+ validFromHeight: 1,
265
+ satsPerMb: 1
266
+ }), /must start with/)
267
+
268
+ assert.throws(() => encodePsP1({
269
+ poolCashAddr: mockData.POOL_CASH_ADDR,
270
+ validFromHeight: 1.5,
271
+ satsPerMb: 1
272
+ }), /validFromHeight/)
273
+
274
+ assert.throws(() => encodePsP1({
275
+ poolCashAddr: mockData.POOL_CASH_ADDR,
276
+ validFromHeight: 1,
277
+ satsPerMb: -1
278
+ }), /satsPerMb/)
279
+ })
280
+ })
281
+
282
+ describe('#Approval Transaction codec (section 5.1)', () => {
283
+ it('should encode the section 5.1 example byte-for-byte', () => {
284
+ const record = buildApprovalOpReturn(APPROVAL_TXID)
285
+
286
+ assert.equal(record.scriptHex, APPROVAL_OP_RETURN_HEX)
287
+ assert.equal(record.updateTxid, APPROVAL_TXID)
288
+ assert.equal(record.opReturn, `OP_RETURN APPROVAL ${APPROVAL_TXID}`)
289
+ })
290
+
291
+ it('should parse the section 5.1 example', () => {
292
+ const record = parseApprovalRecord(APPROVAL_OP_RETURN_HEX)
293
+
294
+ assert.equal(record.word, 'APPROVAL')
295
+ assert.equal(record.updateTxid, APPROVAL_TXID)
296
+ assert.equal(record.encoding, 'push-word+ascii-txid')
297
+ const asm = new bitcore.Script(APPROVAL_OP_RETURN_HEX).toASM()
298
+ assert.equal(parseApprovalRecord(asm).updateTxid, APPROVAL_TXID)
299
+ })
300
+
301
+ it('should parse a PS009 v1 APPROVE record (two pushes, ASCII txid)', () => {
302
+ const legacy = opReturnScript([
303
+ Buffer.from('APPROVE', 'utf8'),
304
+ Buffer.from(APPROVAL_TXID, 'utf8')
305
+ ])
306
+ const record = parseApprovalRecord(legacy)
307
+
308
+ assert.equal(record.word, 'APPROVE')
309
+ assert.equal(record.updateTxid, APPROVAL_TXID)
310
+ })
311
+
312
+ it('should parse a single-push legacy record and a raw-txid record', () => {
313
+ const singlePush = opReturnScript([Buffer.from(`APPROVAL ${APPROVAL_TXID}`, 'utf8')])
314
+ assert.equal(parseApprovalRecord(singlePush).updateTxid, APPROVAL_TXID)
315
+
316
+ const rawTxid = opReturnScript([
317
+ Buffer.from('APPROVAL', 'utf8'),
318
+ Buffer.from(APPROVAL_TXID, 'hex').reverse()
319
+ ])
320
+ assert.equal(parseApprovalRecord(rawTxid).updateTxid, APPROVAL_TXID)
321
+ })
322
+
323
+ it('should return null for non-approval outputs', () => {
324
+ assert.isNull(parseApprovalRecord('6a0400510000'))
325
+ assert.isNull(parseApprovalRecord('6a'))
326
+ })
327
+
328
+ it('should throw on a malformed approval record', () => {
329
+ const missingTxid = opReturnScript([Buffer.from('APPROVAL', 'utf8')])
330
+ const shortTxid = opReturnScript([
331
+ Buffer.from('APPROVAL', 'utf8'),
332
+ Buffer.from('abcd', 'utf8')
333
+ ])
334
+
335
+ for (const script of [missingTxid, shortTxid]) {
336
+ try {
337
+ parseApprovalRecord(script)
338
+ assert.fail('Unexpected code path')
339
+ } catch (err) {
340
+ assert.equal(err.code, 'ERR_APPROVAL_RECORD_MALFORMED')
341
+ }
342
+ }
343
+ })
344
+ })
345
+
346
+ describe('#Update Transaction codec (section 4.1)', () => {
347
+ it('should encode the section 4.1 JSON example byte-for-byte', () => {
348
+ const record = buildUpdateOpReturn({
349
+ cid: UPDATE_CID,
350
+ endorses: APPROVAL_TXID,
351
+ ts: 1770000000000
352
+ })
353
+
354
+ assert.equal(record.json, UPDATE_JSON)
355
+ assert.equal(record.scriptHex, UPDATE_OP_RETURN_HEX)
356
+ })
357
+
358
+ it('should parse the section 4.1 JSON example', () => {
359
+ const record = parseUpdateRecord(UPDATE_OP_RETURN_HEX)
360
+
361
+ assert.equal(record.mode, 'json')
362
+ assert.equal(record.cid, UPDATE_CID)
363
+ assert.equal(record.endorses, APPROVAL_TXID)
364
+ assert.equal(record.ts, 1770000000000)
365
+ })
366
+
367
+ it('should parse the PS009 v1 Update Transaction fixture (OP_0 + OP_PUSHDATA1 JSON)', () => {
368
+ const record = parseUpdateRecord(V1_UPDATE_OP_RETURN_HEX)
369
+
370
+ assert.equal(record.mode, 'json')
371
+ assert.equal(record.cid, V1_UPDATE_CID)
372
+ assert.equal(record.ts, 1676560247168)
373
+ assert.isNull(record.endorses)
374
+ })
375
+
376
+ it('should parse a bare-CID Update Transaction', () => {
377
+ const record = parseUpdateRecord(opReturnScript([Buffer.from(V1_UPDATE_CID, 'utf8')]))
378
+
379
+ assert.equal(record.mode, 'cid')
380
+ assert.equal(record.cid, V1_UPDATE_CID)
381
+ })
382
+
383
+ it('should return null for non-OP_RETURN / empty outputs', () => {
384
+ assert.isNull(parseUpdateRecord('76a914' + '00'.repeat(20) + '88ac'))
385
+ assert.isNull(parseUpdateRecord('6a'))
386
+ })
387
+
388
+ it('should throw on malformed JSON or a missing cid', () => {
389
+ const badJson = opReturnScript([Buffer.from('{not json', 'utf8')])
390
+ const noCid = opReturnScript([Buffer.from(JSON.stringify({ ts: 1 }), 'utf8')])
391
+
392
+ for (const script of [badJson, noCid]) {
393
+ try {
394
+ parseUpdateRecord(script)
395
+ assert.fail('Unexpected code path')
396
+ } catch (err) {
397
+ assert.equal(err.code, 'ERR_UPDATE_RECORD_MALFORMED')
398
+ }
399
+ }
400
+ })
401
+
402
+ it('should reject an invalid endorses txid', () => {
403
+ assert.throws(() => buildUpdateOpReturn({ cid: UPDATE_CID, endorses: 'abc' }), /endorses/)
404
+ assert.throws(() => buildUpdateOpReturn({}), /cid is required/)
405
+ })
406
+ })
407
+ })