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/README.md +143 -0
- package/index.js +32 -5
- package/lib/nfts.js +1 -1
- package/lib/ps009v2/approval.js +261 -0
- package/lib/ps009v2/council.js +237 -0
- package/lib/ps009v2/index.js +111 -0
- package/lib/ps009v2/psp1-codec.js +213 -0
- package/lib/ps009v2/script.js +352 -0
- package/lib/ps009v2/signatures.js +125 -0
- package/lib/ps009v2/tx.js +153 -0
- package/lib/ps009v2/update.js +239 -0
- package/lib/ps009v2/validate.js +834 -0
- package/lib/util.js +1 -1
- package/package.json +3 -2
- package/test/integration/main-index-integration.js +3 -3
- package/test/unit/main-index-unit.js +6 -6
- package/test/unit/mocks/main-index-mocks.js +13 -1
- package/test/unit/mocks/ps009v2-mocks.js +304 -0
- package/test/unit/mocks/util-mocks.js +1 -1
- package/test/unit/nfts-unit.js +6 -9
- package/test/unit/ps009v2-flow-unit.js +805 -0
- package/test/unit/ps009v2-vectors-unit.js +407 -0
- package/test/unit/util-unit.js +6 -6
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 - Council membership and P2SH multisig address derivation.
|
|
3
|
+
|
|
4
|
+
Implements PS009 v2 section 2 ("Multisig construction"):
|
|
5
|
+
|
|
6
|
+
- The council's P2SH multisig address is built from the current NFT-holder
|
|
7
|
+
public keys.
|
|
8
|
+
- The n-of-m threshold is chosen by the council and published in the
|
|
9
|
+
registry policy (e.g. 3-of-5).
|
|
10
|
+
- Public keys are compressed and **sorted lexicographically** before address
|
|
11
|
+
derivation, so that any party recomputes the same address.
|
|
12
|
+
|
|
13
|
+
This module is pure cryptography/encoding: it does not talk to a node and
|
|
14
|
+
does not read the blockchain.
|
|
15
|
+
|
|
16
|
+
https://github.com/Permissionless-Software-Foundation/specifications/blob/master/ps009-multisig-approval-v2.md
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import bitcore from '@chris.troutner/bitcore-lib-cash'
|
|
20
|
+
|
|
21
|
+
// Cashaddr prefixes used by BCH (mainnet and chipnet/testnet).
|
|
22
|
+
const CASHADDR_PREFIX = 'bitcoincash:'
|
|
23
|
+
const TESTNET_CASHADDR_PREFIX = 'bchtest:'
|
|
24
|
+
const TOKEN_AWARE_PREFIX = 'simpleledger:'
|
|
25
|
+
|
|
26
|
+
// Default network for address derivation. Overridable per call.
|
|
27
|
+
const NETWORK_NAMES = ['mainnet', 'testnet', 'chipnet']
|
|
28
|
+
|
|
29
|
+
// Resolve a network input (name or bitcore network object) to a bitcore
|
|
30
|
+
// network object, plus its cashaddr prefix.
|
|
31
|
+
function resolveNetwork (network) {
|
|
32
|
+
if (!network) return { networkObj: bitcore.Networks.livenet, name: 'mainnet' }
|
|
33
|
+
|
|
34
|
+
if (typeof network === 'string') {
|
|
35
|
+
const name = network.toLowerCase()
|
|
36
|
+
if (name === 'mainnet' || name === 'livenet') {
|
|
37
|
+
return { networkObj: bitcore.Networks.livenet, name: 'mainnet' }
|
|
38
|
+
}
|
|
39
|
+
if (name === 'testnet' || name === 'chipnet' || name === 'test') {
|
|
40
|
+
return { networkObj: bitcore.Networks.testnet, name: 'testnet' }
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`Unsupported network: ${network}. Expected one of: ${NETWORK_NAMES.join(', ')}`)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (network === bitcore.Networks.testnet) {
|
|
46
|
+
return { networkObj: bitcore.Networks.testnet, name: 'testnet' }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (network === bitcore.Networks.livenet) {
|
|
50
|
+
return { networkObj: bitcore.Networks.livenet, name: 'mainnet' }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
throw new Error('Unsupported network object. Use \'mainnet\', \'testnet\', or a bitcore Networks member.')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Return the cashaddr prefix for a resolved network name.
|
|
57
|
+
function cashaddrPrefix (networkName) {
|
|
58
|
+
return networkName === 'testnet' ? TESTNET_CASHADDR_PREFIX : CASHADDR_PREFIX
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Compressed public keys are 33 bytes: 0x02/0x03 followed by 32 bytes of X.
|
|
62
|
+
function isCompressedPubkeyHex (pubkey) {
|
|
63
|
+
return typeof pubkey === 'string' && /^0[23][0-9a-fA-F]{64}$/.test(pubkey)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Validate a public key and return its canonical (lowercase) hex form.
|
|
67
|
+
function validatePubkey (pubkey, index) {
|
|
68
|
+
const where = `public key at index ${index}`
|
|
69
|
+
|
|
70
|
+
if (typeof pubkey !== 'string') {
|
|
71
|
+
throw new Error(`${where} must be a hex string`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!isCompressedPubkeyHex(pubkey)) {
|
|
75
|
+
throw new Error(`${where} must be a 33-byte compressed public key (02/03 + 64 hex chars), got: ${pubkey}`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const canonical = pubkey.toLowerCase()
|
|
79
|
+
|
|
80
|
+
// Let bitcore validate the curve point as well as the encoding.
|
|
81
|
+
try {
|
|
82
|
+
// eslint-disable-next-line no-new
|
|
83
|
+
new bitcore.PublicKey(canonical)
|
|
84
|
+
} catch (err) {
|
|
85
|
+
throw new Error(`${where} is not a valid public key: ${pubkey}`)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return canonical
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
Derive the council's P2SH multisig address from operator public keys.
|
|
93
|
+
|
|
94
|
+
Input:
|
|
95
|
+
pubkeys - array of compressed public key hex strings (any
|
|
96
|
+
order; they are sorted lexicographically here).
|
|
97
|
+
requiredSignatures - n in the n-of-m threshold. Defaults to
|
|
98
|
+
floor(m / 2) + 1 (the PS009 v1 "50% + 1" default),
|
|
99
|
+
but the council is expected to publish the real
|
|
100
|
+
threshold in the registry policy.
|
|
101
|
+
network - 'mainnet' (default) or 'testnet'.
|
|
102
|
+
|
|
103
|
+
Output:
|
|
104
|
+
{
|
|
105
|
+
address, // cashaddr P2SH address
|
|
106
|
+
scriptHex, // P2SH scriptPubKey hex (OP_HASH160 <hash> OP_EQUAL)
|
|
107
|
+
redeemScript, // n-of-m multisig redeem script hex
|
|
108
|
+
publicKeys, // sorted, canonical compressed public keys
|
|
109
|
+
requiredSignatures,
|
|
110
|
+
threshold, // alias of requiredSignatures (spec wording)
|
|
111
|
+
m, n, // threshold / number of keys
|
|
112
|
+
network
|
|
113
|
+
}
|
|
114
|
+
*/
|
|
115
|
+
function deriveCouncilAddress (inObj = {}) {
|
|
116
|
+
const { pubkeys } = inObj
|
|
117
|
+
let { requiredSignatures } = inObj
|
|
118
|
+
|
|
119
|
+
if (!Array.isArray(pubkeys) || pubkeys.length === 0) {
|
|
120
|
+
throw new Error('pubkeys must be a non-empty array of compressed public key hex strings')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const { networkObj, name } = resolveNetwork(inObj.network)
|
|
124
|
+
|
|
125
|
+
// Validate + canonicalize every key.
|
|
126
|
+
const keys = pubkeys.map((pubkey, i) => validatePubkey(pubkey, i))
|
|
127
|
+
|
|
128
|
+
// Reject duplicates: the same key twice would silently lower the effective
|
|
129
|
+
// threshold and corrupt the m-of-n accounting.
|
|
130
|
+
const seen = {}
|
|
131
|
+
for (const key of keys) {
|
|
132
|
+
if (seen[key]) {
|
|
133
|
+
throw new Error(`Duplicate public key in council: ${key}`)
|
|
134
|
+
}
|
|
135
|
+
seen[key] = true
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// PS009 v2 section 2: public keys are sorted lexicographically before
|
|
139
|
+
// address derivation, so that every party recomputes the same address.
|
|
140
|
+
// (Lowercase hex sorts identically to raw byte order.)
|
|
141
|
+
const sortedKeys = keys.slice().sort()
|
|
142
|
+
|
|
143
|
+
if (requiredSignatures === undefined || requiredSignatures === null) {
|
|
144
|
+
requiredSignatures = Math.floor(sortedKeys.length / 2) + 1
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!Number.isInteger(requiredSignatures)) {
|
|
148
|
+
throw new Error('requiredSignatures must be an integer')
|
|
149
|
+
}
|
|
150
|
+
if (requiredSignatures < 1) {
|
|
151
|
+
throw new Error('requiredSignatures must be at least 1')
|
|
152
|
+
}
|
|
153
|
+
if (requiredSignatures > sortedKeys.length) {
|
|
154
|
+
throw new Error(`requiredSignatures (${requiredSignatures}) can not exceed the number of public keys (${sortedKeys.length})`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// The multisig redeem script (sorted keys, in order).
|
|
158
|
+
const redeemScript = bitcore.Script.buildMultisigOut(sortedKeys, requiredSignatures).toHex()
|
|
159
|
+
|
|
160
|
+
// The P2SH address + locking script.
|
|
161
|
+
const address = new bitcore.Address(sortedKeys, requiredSignatures, networkObj)
|
|
162
|
+
const scriptHex = new bitcore.Script(address).toHex()
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
address: address.toString(),
|
|
166
|
+
scriptHex,
|
|
167
|
+
redeemScript,
|
|
168
|
+
publicKeys: sortedKeys,
|
|
169
|
+
requiredSignatures,
|
|
170
|
+
threshold: requiredSignatures,
|
|
171
|
+
m: requiredSignatures,
|
|
172
|
+
n: sortedKeys.length,
|
|
173
|
+
network: name,
|
|
174
|
+
cashaddrPrefix: cashaddrPrefix(name)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Derive the P2PKH cashaddr that corresponds to a compressed public key.
|
|
179
|
+
// Used by PS009 v2 section 6 step 7 ("each address listed in the JSON is
|
|
180
|
+
// generated from the given public key").
|
|
181
|
+
function deriveAddressFromPubkey (pubkey, inObj = {}) {
|
|
182
|
+
const canonical = validatePubkey(pubkey, 0)
|
|
183
|
+
const { networkObj } = resolveNetwork(inObj.network)
|
|
184
|
+
const publicKey = new bitcore.PublicKey(canonical)
|
|
185
|
+
const address = bitcore.Address.fromPublicKey(publicKey, networkObj)
|
|
186
|
+
return address.toString()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Normalize a BCH address so that two representations of the same address
|
|
190
|
+
// compare equal. Handles token-aware (`simpleledger:`) prefixes and case.
|
|
191
|
+
// Falls back to the trimmed, lowercased input when bitcore can not parse it
|
|
192
|
+
// (e.g. P2SH32 cashaddrs, which bitcore-lib-cash 8.25.x does not know about).
|
|
193
|
+
function normalizeAddress (address) {
|
|
194
|
+
if (typeof address !== 'string' || address.length === 0) {
|
|
195
|
+
throw new Error('address must be a non-empty string')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let candidate = address.trim()
|
|
199
|
+
|
|
200
|
+
if (candidate.toLowerCase().startsWith(TOKEN_AWARE_PREFIX)) {
|
|
201
|
+
candidate = CASHADDR_PREFIX + candidate.slice(TOKEN_AWARE_PREFIX.length)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
return bitcore.Address.fromString(candidate).toString()
|
|
206
|
+
} catch (err) {
|
|
207
|
+
// P2SH32 addresses and other forms bitcore can not decode: compare the
|
|
208
|
+
// lowercased string form with any known prefix.
|
|
209
|
+
return candidate.toLowerCase()
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Compare two BCH addresses after normalization.
|
|
214
|
+
function addressesEqual (a, b) {
|
|
215
|
+
return normalizeAddress(a) === normalizeAddress(b)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export {
|
|
219
|
+
CASHADDR_PREFIX,
|
|
220
|
+
TESTNET_CASHADDR_PREFIX,
|
|
221
|
+
TOKEN_AWARE_PREFIX,
|
|
222
|
+
deriveCouncilAddress,
|
|
223
|
+
deriveAddressFromPubkey,
|
|
224
|
+
isCompressedPubkeyHex,
|
|
225
|
+
validatePubkey,
|
|
226
|
+
normalizeAddress,
|
|
227
|
+
addressesEqual,
|
|
228
|
+
resolveNetwork,
|
|
229
|
+
cashaddrPrefix
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export default {
|
|
233
|
+
deriveCouncilAddress,
|
|
234
|
+
deriveAddressFromPubkey,
|
|
235
|
+
normalizeAddress,
|
|
236
|
+
addressesEqual
|
|
237
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 (Multisig Rate Approval, revision 2) - reference implementation.
|
|
3
|
+
|
|
4
|
+
Spec: https://github.com/Permissionless-Software-Foundation/specifications/blob/master/ps009-multisig-approval-v2.md
|
|
5
|
+
|
|
6
|
+
This namespace is the PS009 v2 surface:
|
|
7
|
+
|
|
8
|
+
deriveCouncilAddress() - section 2: sorted compressed pubkeys -> P2SH n-of-m
|
|
9
|
+
encodePsP1() / parsePsP1Record() - PS010 section 4.1 PSP1 announcement codec
|
|
10
|
+
buildUpdateTransaction() - section 4: Update Transaction (or PSP1 announcement)
|
|
11
|
+
buildApprovalTransaction() - section 5: Approval Transaction (APPROVAL + UT txid)
|
|
12
|
+
parseApprovalRecord() - section 5: read an Approval Transaction
|
|
13
|
+
validateApproval() - section 6: the ten-step validation checklist
|
|
14
|
+
|
|
15
|
+
The old (PS009 v1) `MultisigApproval` class remains the package default
|
|
16
|
+
export; this object is exported alongside it as `ps009v2`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
deriveCouncilAddress,
|
|
21
|
+
deriveAddressFromPubkey,
|
|
22
|
+
normalizeAddress,
|
|
23
|
+
addressesEqual
|
|
24
|
+
} from './council.js'
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
PSP1_LOKAD,
|
|
28
|
+
PSP1_DETECTION_PREFIX,
|
|
29
|
+
ZERO_TXID,
|
|
30
|
+
encodePsP1,
|
|
31
|
+
isPsP1Record,
|
|
32
|
+
parsePsP1Record
|
|
33
|
+
} from './psp1-codec.js'
|
|
34
|
+
|
|
35
|
+
import {
|
|
36
|
+
buildUpdateOpReturn,
|
|
37
|
+
parseUpdateRecord,
|
|
38
|
+
buildUpdateTransaction
|
|
39
|
+
} from './update.js'
|
|
40
|
+
|
|
41
|
+
import {
|
|
42
|
+
APPROVAL_WORD,
|
|
43
|
+
DUST_SATOSHIS,
|
|
44
|
+
buildApprovalOpReturn,
|
|
45
|
+
parseApprovalRecord,
|
|
46
|
+
buildApprovalTransaction
|
|
47
|
+
} from './approval.js'
|
|
48
|
+
|
|
49
|
+
import { validateApproval, normalizeMembership } from './validate.js'
|
|
50
|
+
import { countMultisigSignatures, verifyMultisigSpend } from './signatures.js'
|
|
51
|
+
|
|
52
|
+
const ps009v2 = {
|
|
53
|
+
// Section 2 - council membership.
|
|
54
|
+
deriveCouncilAddress,
|
|
55
|
+
deriveAddressFromPubkey,
|
|
56
|
+
normalizeAddress,
|
|
57
|
+
addressesEqual,
|
|
58
|
+
|
|
59
|
+
// PS010 section 4.1 - PSP1 registry record.
|
|
60
|
+
PSP1_LOKAD,
|
|
61
|
+
PSP1_DETECTION_PREFIX,
|
|
62
|
+
ZERO_TXID,
|
|
63
|
+
encodePsP1,
|
|
64
|
+
isPsP1Record,
|
|
65
|
+
parsePsP1Record,
|
|
66
|
+
|
|
67
|
+
// Section 4 - Update Transaction.
|
|
68
|
+
buildUpdateOpReturn,
|
|
69
|
+
parseUpdateRecord,
|
|
70
|
+
buildUpdateTransaction,
|
|
71
|
+
|
|
72
|
+
// Section 5 - Approval Transaction.
|
|
73
|
+
APPROVAL_WORD,
|
|
74
|
+
DUST_SATOSHIS,
|
|
75
|
+
buildApprovalOpReturn,
|
|
76
|
+
parseApprovalRecord,
|
|
77
|
+
buildApprovalTransaction,
|
|
78
|
+
|
|
79
|
+
// Section 6 - validation.
|
|
80
|
+
validateApproval,
|
|
81
|
+
normalizeMembership,
|
|
82
|
+
countMultisigSignatures,
|
|
83
|
+
verifyMultisigSpend
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
deriveCouncilAddress,
|
|
88
|
+
deriveAddressFromPubkey,
|
|
89
|
+
normalizeAddress,
|
|
90
|
+
addressesEqual,
|
|
91
|
+
PSP1_LOKAD,
|
|
92
|
+
PSP1_DETECTION_PREFIX,
|
|
93
|
+
ZERO_TXID,
|
|
94
|
+
encodePsP1,
|
|
95
|
+
isPsP1Record,
|
|
96
|
+
parsePsP1Record,
|
|
97
|
+
buildUpdateOpReturn,
|
|
98
|
+
parseUpdateRecord,
|
|
99
|
+
buildUpdateTransaction,
|
|
100
|
+
APPROVAL_WORD,
|
|
101
|
+
DUST_SATOSHIS,
|
|
102
|
+
buildApprovalOpReturn,
|
|
103
|
+
parseApprovalRecord,
|
|
104
|
+
buildApprovalTransaction,
|
|
105
|
+
validateApproval,
|
|
106
|
+
normalizeMembership,
|
|
107
|
+
countMultisigSignatures,
|
|
108
|
+
verifyMultisigSpend
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export default ps009v2
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 / PS010 section 4.1 - `PSP1` pool-registry announcement codec.
|
|
3
|
+
|
|
4
|
+
Wire layout (normative, PS010 section 4.1):
|
|
5
|
+
|
|
6
|
+
scriptPubKey ASM (vout[0]):
|
|
7
|
+
OP_RETURN
|
|
8
|
+
OP_PUSHDATA 50535031 # "PSP1"
|
|
9
|
+
OP_PUSHDATA 01 # version = 1 (1 byte)
|
|
10
|
+
OP_PUSHDATA <poolCashAddrUtf8>
|
|
11
|
+
OP_PUSHDATA <validFromHeightLE8>
|
|
12
|
+
OP_PUSHDATA <satsPerMbLE8>
|
|
13
|
+
OP_PUSHDATA <prevRegistryTxidBin32>
|
|
14
|
+
|
|
15
|
+
Detection: script hex contains 6a0450535031.
|
|
16
|
+
|
|
17
|
+
Field rules:
|
|
18
|
+
- version must be 0x01; unknown versions are rejected.
|
|
19
|
+
- poolCashAddr is UTF-8 cashaddr *with* the network prefix. P2SH32 pool
|
|
20
|
+
addresses are accepted (bitcore-lib-cash 8.25.x can not decode them, so
|
|
21
|
+
only the prefix/charset are validated here).
|
|
22
|
+
- validFromHeight and satsPerMb are uint64 little-endian, always 8 bytes.
|
|
23
|
+
- prevRegistryTxid is 32 raw bytes in Bitcoin internal byte order. This
|
|
24
|
+
codec takes/returns the *displayed* txid hex (the form explorers show)
|
|
25
|
+
and reverses to/from internal order, per PS010's implementation note.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
dataPushes,
|
|
30
|
+
decodeTxidInternal,
|
|
31
|
+
decodeUint64LE,
|
|
32
|
+
encodeTxidInternal,
|
|
33
|
+
encodeUint64LE,
|
|
34
|
+
extractVout0ScriptHex,
|
|
35
|
+
isOpReturn,
|
|
36
|
+
opReturnScript
|
|
37
|
+
} from './script.js'
|
|
38
|
+
|
|
39
|
+
const PSP1_LOKAD = 'PSP1'
|
|
40
|
+
const PSP1_LOKAD_HEX = '50535031'
|
|
41
|
+
const PSP1_VERSION = 1
|
|
42
|
+
const PSP1_DETECTION_PREFIX = '6a0450535031'
|
|
43
|
+
const ZERO_TXID = '0'.repeat(64)
|
|
44
|
+
|
|
45
|
+
// Cashaddr prefixes accepted in the poolCashAddr field. PS010 says
|
|
46
|
+
// 'bitcoincash:' (mainnet); testnet/chipnet use 'bchtest:'.
|
|
47
|
+
const POOL_ADDR_PREFIXES = ['bitcoincash:', 'bchtest:']
|
|
48
|
+
|
|
49
|
+
function psp1Error (code, message) {
|
|
50
|
+
const err = new Error(message)
|
|
51
|
+
err.code = code
|
|
52
|
+
return err
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Encode a PSP1 registry announcement into a vout[0] scriptPubKey hex string.
|
|
56
|
+
function encodePsP1 (inObj = {}) {
|
|
57
|
+
const {
|
|
58
|
+
poolCashAddr,
|
|
59
|
+
satsPerMb,
|
|
60
|
+
validFromHeight,
|
|
61
|
+
prevRegistryTxid = ZERO_TXID,
|
|
62
|
+
version = PSP1_VERSION
|
|
63
|
+
} = inObj
|
|
64
|
+
|
|
65
|
+
if (typeof poolCashAddr !== 'string') {
|
|
66
|
+
throw new Error('poolCashAddr is required (UTF-8 cashaddr, including the network prefix)')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const prefixOk = POOL_ADDR_PREFIXES.some(prefix => poolCashAddr.startsWith(prefix))
|
|
70
|
+
if (!prefixOk) {
|
|
71
|
+
throw new Error(`poolCashAddr must start with one of: ${POOL_ADDR_PREFIXES.join(', ')}`)
|
|
72
|
+
}
|
|
73
|
+
if (!/^[a-z0-9:]+$/.test(poolCashAddr)) {
|
|
74
|
+
throw new Error('poolCashAddr must be a lowercase cashaddr string')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (version !== PSP1_VERSION) {
|
|
78
|
+
throw new Error(`Unsupported PSP1 version: ${version} (only ${PSP1_VERSION} is defined)`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (validFromHeight === undefined || validFromHeight === null) {
|
|
82
|
+
throw new Error('validFromHeight is required')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const pushes = [
|
|
86
|
+
Buffer.from(PSP1_LOKAD_HEX, 'hex'),
|
|
87
|
+
Buffer.from([version]),
|
|
88
|
+
Buffer.from(poolCashAddr, 'utf8'),
|
|
89
|
+
encodeUint64LE(validFromHeight, 'validFromHeight'),
|
|
90
|
+
encodeUint64LE(satsPerMb, 'satsPerMb'),
|
|
91
|
+
encodeTxidInternal(prevRegistryTxid, 'prevRegistryTxid')
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
const scriptHex = opReturnScript(pushes)
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
lokad: PSP1_LOKAD,
|
|
98
|
+
version,
|
|
99
|
+
poolCashAddr,
|
|
100
|
+
validFromHeight: Number(validFromHeight),
|
|
101
|
+
satsPerMb: Number(satsPerMb),
|
|
102
|
+
prevRegistryTxid: prevRegistryTxid.toLowerCase(),
|
|
103
|
+
scriptHex
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Cheap detection check, per PS010: 'OP_RETURN hex contains 6a0450535031'.
|
|
108
|
+
function isPsP1Record (input) {
|
|
109
|
+
try {
|
|
110
|
+
const scriptHex = extractVout0ScriptHex(input)
|
|
111
|
+
return scriptHex.toLowerCase().startsWith(PSP1_DETECTION_PREFIX)
|
|
112
|
+
} catch (err) {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
Parse a PSP1 announcement.
|
|
119
|
+
|
|
120
|
+
Returns the decoded record, or null when vout[0] is not a PSP1 record.
|
|
121
|
+
Throws (with err.code set) when vout[0] *is* a PSP1 record but is malformed:
|
|
122
|
+
wrong number of pushes, unknown version, bad field lengths, bad UTF-8.
|
|
123
|
+
*/
|
|
124
|
+
function parsePsP1Record (input) {
|
|
125
|
+
const scriptHex = extractVout0ScriptHex(input)
|
|
126
|
+
|
|
127
|
+
if (!isOpReturn(scriptHex)) return null
|
|
128
|
+
|
|
129
|
+
let pushes
|
|
130
|
+
try {
|
|
131
|
+
pushes = dataPushes(scriptHex)
|
|
132
|
+
} catch (err) {
|
|
133
|
+
// Only report a malformed record if it looks like PSP1; otherwise it is
|
|
134
|
+
// simply not a PSP1 record.
|
|
135
|
+
if (scriptHex.toLowerCase().includes(PSP1_LOKAD_HEX)) {
|
|
136
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `Malformed PSP1 pushdata: ${err.message}`)
|
|
137
|
+
}
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (pushes.length === 0) return null
|
|
142
|
+
if (pushes[0].toString('hex') !== PSP1_LOKAD_HEX) return null
|
|
143
|
+
|
|
144
|
+
if (pushes.length !== 6) {
|
|
145
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 record must contain exactly 6 pushes (lokad, version, poolCashAddr, validFromHeight, satsPerMb, prevRegistryTxid); found ${pushes.length}`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const [, versionBuf, addrBuf, heightBuf, satsBuf, prevTxidBuf] = pushes
|
|
149
|
+
|
|
150
|
+
if (versionBuf.length !== 1) {
|
|
151
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 version push must be exactly 1 byte; found ${versionBuf.length}`)
|
|
152
|
+
}
|
|
153
|
+
if (versionBuf[0] !== PSP1_VERSION) {
|
|
154
|
+
throw psp1Error('ERR_PSP1_UNKNOWN_VERSION', `Unknown PSP1 version: ${versionBuf[0]}`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let poolCashAddr
|
|
158
|
+
try {
|
|
159
|
+
poolCashAddr = addrBuf.toString('utf8')
|
|
160
|
+
} catch (err) {
|
|
161
|
+
throw psp1Error('ERR_PSP1_MALFORMED', errorMessage('PSP1 poolCashAddr is not valid UTF-8', err))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!POOL_ADDR_PREFIXES.some(prefix => poolCashAddr.startsWith(prefix))) {
|
|
165
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 poolCashAddr must start with one of: ${POOL_ADDR_PREFIXES.join(', ')}`)
|
|
166
|
+
}
|
|
167
|
+
if (!/^[a-z0-9:]+$/.test(poolCashAddr)) {
|
|
168
|
+
throw psp1Error('ERR_PSP1_MALFORMED', 'PSP1 poolCashAddr must be a lowercase cashaddr string')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (heightBuf.length !== 8) {
|
|
172
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 validFromHeight must be 8 bytes; found ${heightBuf.length}`)
|
|
173
|
+
}
|
|
174
|
+
if (satsBuf.length !== 8) {
|
|
175
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 satsPerMb must be 8 bytes; found ${satsBuf.length}`)
|
|
176
|
+
}
|
|
177
|
+
if (prevTxidBuf.length !== 32) {
|
|
178
|
+
throw psp1Error('ERR_PSP1_MALFORMED', `PSP1 prevRegistryTxid must be 32 bytes; found ${prevTxidBuf.length}`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
txid: null, // filled in by the validator/providers; not encoded on chain
|
|
183
|
+
lokad: PSP1_LOKAD,
|
|
184
|
+
version: versionBuf[0],
|
|
185
|
+
poolCashAddr,
|
|
186
|
+
validFromHeight: decodeUint64LE(heightBuf, 'validFromHeight'),
|
|
187
|
+
satsPerMb: decodeUint64LE(satsBuf, 'satsPerMb'),
|
|
188
|
+
prevRegistryTxid: decodeTxidInternal(prevTxidBuf, 'prevRegistryTxid'),
|
|
189
|
+
scriptHex
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function errorMessage (prefix, err) {
|
|
194
|
+
return `${prefix}: ${err && err.message ? err.message : err}`
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export {
|
|
198
|
+
PSP1_LOKAD,
|
|
199
|
+
PSP1_LOKAD_HEX,
|
|
200
|
+
PSP1_VERSION,
|
|
201
|
+
PSP1_DETECTION_PREFIX,
|
|
202
|
+
ZERO_TXID,
|
|
203
|
+
POOL_ADDR_PREFIXES,
|
|
204
|
+
encodePsP1,
|
|
205
|
+
isPsP1Record,
|
|
206
|
+
parsePsP1Record
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export default {
|
|
210
|
+
encodePsP1,
|
|
211
|
+
isPsP1Record,
|
|
212
|
+
parsePsP1Record
|
|
213
|
+
}
|