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,239 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 - Update Transaction (UT) and PS010 `PSP1` registry record codec.
|
|
3
|
+
|
|
4
|
+
Two on-chain records live here:
|
|
5
|
+
|
|
6
|
+
1. The PS010 section 4.1 registry announcement:
|
|
7
|
+
OP_RETURN
|
|
8
|
+
push 'PSP1' (50535031, 4 bytes)
|
|
9
|
+
push 0x01 (version, 1 byte)
|
|
10
|
+
push poolCashAddr (UTF-8, with 'bitcoincash:' prefix)
|
|
11
|
+
push validFromHeight (uint64, 8 bytes, little-endian)
|
|
12
|
+
push satsPerMb (uint64, 8 bytes, little-endian)
|
|
13
|
+
push prevRegistryTxid (32 raw bytes, internal byte order)
|
|
14
|
+
Detection prefix: 6a0450535031.
|
|
15
|
+
|
|
16
|
+
2. The PS009 v2 section 4 Update Transaction OP_RETURN:
|
|
17
|
+
OP_RETURN { "cid": ..., "endorses": <PSP1 announcement txid>, "ts": ... }
|
|
18
|
+
(section 4.1). A bare UTF-8 CID, as described in section 4 prose, is also
|
|
19
|
+
accepted by the parser.
|
|
20
|
+
|
|
21
|
+
Reference-first: nothing here talks to a node. `buildUpdateTransaction()`
|
|
22
|
+
needs injected UTXOs and (optionally) private keys.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { encodePsP1 } from './psp1-codec.js'
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
dataPushes,
|
|
29
|
+
extractVout0ScriptHex,
|
|
30
|
+
isOpReturn,
|
|
31
|
+
opReturnScript
|
|
32
|
+
} from './script.js'
|
|
33
|
+
|
|
34
|
+
import { buildTransaction } from './tx.js'
|
|
35
|
+
|
|
36
|
+
// The Update Transaction carries an IPFS CID as UTF-8. PS009 v2 section 4.1
|
|
37
|
+
// shows it wrapped in JSON, with the `endorses` field binding the UT to a
|
|
38
|
+
// specific on-chain PSP1 announcement.
|
|
39
|
+
function buildUpdateOpReturn (inObj = {}) {
|
|
40
|
+
const { cid, endorses, ts } = inObj
|
|
41
|
+
|
|
42
|
+
if (typeof cid !== 'string' || cid.trim().length === 0) {
|
|
43
|
+
throw new Error('cid is required (the IPFS CID of the council membership document)')
|
|
44
|
+
}
|
|
45
|
+
if (endorses !== undefined && endorses !== null) {
|
|
46
|
+
if (typeof endorses !== 'string' || !/^[0-9a-fA-F]{64}$/.test(endorses)) {
|
|
47
|
+
throw new Error('endorses must be a 64-character hex PSP1 announcement txid')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Preserve the property order from the spec example: cid, endorses, ts.
|
|
52
|
+
const doc = { cid }
|
|
53
|
+
if (endorses) doc.endorses = endorses.toLowerCase()
|
|
54
|
+
if (ts !== undefined && ts !== null) {
|
|
55
|
+
if (!Number.isInteger(ts)) throw new Error('ts must be an integer (milliseconds since epoch)')
|
|
56
|
+
doc.ts = ts
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const json = JSON.stringify(doc)
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
scriptHex: opReturnScript([Buffer.from(json, 'utf8')]),
|
|
63
|
+
json,
|
|
64
|
+
cid,
|
|
65
|
+
endorses: doc.endorses,
|
|
66
|
+
ts: doc.ts
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Parse an Update Transaction OP_RETURN. Returns null when the first output is
|
|
71
|
+
// not an OP_RETURN (or carries no payload). Throws when it is an OP_RETURN
|
|
72
|
+
// with a payload that is neither JSON nor a plausible CID.
|
|
73
|
+
function parseUpdateRecord (input) {
|
|
74
|
+
const scriptHex = extractVout0ScriptHex(input)
|
|
75
|
+
|
|
76
|
+
if (!isOpReturn(scriptHex)) return null
|
|
77
|
+
|
|
78
|
+
const pushes = dataPushes(scriptHex).filter(push => push.length > 0)
|
|
79
|
+
if (pushes.length === 0) return null
|
|
80
|
+
|
|
81
|
+
// v1 Update Transactions prefix the payload with an empty push
|
|
82
|
+
// (OP_RETURN OP_0 OP_PUSHDATA1 <json>). Taking the first non-empty push is
|
|
83
|
+
// tolerant of that and of a leading OP_RETURN-version push.
|
|
84
|
+
const payload = pushes[0]
|
|
85
|
+
|
|
86
|
+
let text
|
|
87
|
+
try {
|
|
88
|
+
text = payload.toString('utf8')
|
|
89
|
+
} catch (err) {
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const record = {
|
|
94
|
+
scriptHex,
|
|
95
|
+
text,
|
|
96
|
+
cid: null,
|
|
97
|
+
endorses: null,
|
|
98
|
+
ts: null,
|
|
99
|
+
mode: null,
|
|
100
|
+
json: null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (text.trim().startsWith('{')) {
|
|
104
|
+
let doc
|
|
105
|
+
try {
|
|
106
|
+
doc = JSON.parse(text)
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const parseErr = new Error(`Update Transaction OP_RETURN contains invalid JSON: ${err.message}`)
|
|
109
|
+
parseErr.code = 'ERR_UPDATE_RECORD_MALFORMED'
|
|
110
|
+
throw parseErr
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (typeof doc.cid !== 'string' || doc.cid.length === 0) {
|
|
114
|
+
const err = new Error('Update Transaction JSON is missing the required "cid" property')
|
|
115
|
+
err.code = 'ERR_UPDATE_RECORD_MALFORMED'
|
|
116
|
+
throw err
|
|
117
|
+
}
|
|
118
|
+
if (doc.endorses !== undefined && doc.endorses !== null) {
|
|
119
|
+
if (typeof doc.endorses !== 'string' || !/^[0-9a-fA-F]{64}$/.test(doc.endorses)) {
|
|
120
|
+
const err = new Error('Update Transaction JSON "endorses" must be a 64-character hex txid')
|
|
121
|
+
err.code = 'ERR_UPDATE_RECORD_MALFORMED'
|
|
122
|
+
throw err
|
|
123
|
+
}
|
|
124
|
+
record.endorses = doc.endorses.toLowerCase()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
record.mode = 'json'
|
|
128
|
+
record.json = doc
|
|
129
|
+
record.cid = doc.cid
|
|
130
|
+
record.ts = doc.ts === undefined ? null : doc.ts
|
|
131
|
+
|
|
132
|
+
return record
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Bare CID form (section 4 prose: "an OP_RETURN containing an IPFS CID").
|
|
136
|
+
if (!/^\S+$/.test(text)) {
|
|
137
|
+
const err = new Error('Update Transaction OP_RETURN is neither JSON nor a bare CID')
|
|
138
|
+
err.code = 'ERR_UPDATE_RECORD_MALFORMED'
|
|
139
|
+
throw err
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
record.mode = 'cid'
|
|
143
|
+
record.cid = text
|
|
144
|
+
|
|
145
|
+
return record
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/*
|
|
149
|
+
Build an Update Transaction (PS009 v2 section 4) - or, when given the PSP010
|
|
150
|
+
registry fields instead of a CID, the `PSP1` announcement transaction that
|
|
151
|
+
the UT endorses.
|
|
152
|
+
|
|
153
|
+
Update mode (section 4 / 4.1):
|
|
154
|
+
buildUpdateTransaction({ cid, endorses, ts, inputs, privKeys, changeAddress })
|
|
155
|
+
|
|
156
|
+
Announcement mode (PS010 section 4.1):
|
|
157
|
+
buildUpdateTransaction({
|
|
158
|
+
poolCashAddr, satsPerMb, validFromHeight, prevRegistryTxid,
|
|
159
|
+
inputs, // UTXOs, normally at the council P2SH address
|
|
160
|
+
council, // deriveCouncilAddress() output (for n-of-m signing)
|
|
161
|
+
privKeys // optional; n-of-m subset for partial signing
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
Both modes put the record in vout[0] and are pure/local.
|
|
165
|
+
*/
|
|
166
|
+
function buildUpdateTransaction (inObj = {}) {
|
|
167
|
+
const {
|
|
168
|
+
cid,
|
|
169
|
+
endorses,
|
|
170
|
+
ts,
|
|
171
|
+
poolCashAddr,
|
|
172
|
+
satsPerMb,
|
|
173
|
+
validFromHeight,
|
|
174
|
+
prevRegistryTxid,
|
|
175
|
+
inputs,
|
|
176
|
+
privKeys,
|
|
177
|
+
council,
|
|
178
|
+
changeAddress,
|
|
179
|
+
fee,
|
|
180
|
+
network
|
|
181
|
+
} = inObj
|
|
182
|
+
|
|
183
|
+
const hasPsP1Fields = poolCashAddr !== undefined ||
|
|
184
|
+
satsPerMb !== undefined ||
|
|
185
|
+
validFromHeight !== undefined
|
|
186
|
+
|
|
187
|
+
if (cid && hasPsP1Fields) {
|
|
188
|
+
throw new Error('Pass either { cid } (Update Transaction) or the PSP1 epoch fields (announcement), not both')
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
let mode
|
|
192
|
+
let opReturn
|
|
193
|
+
let record
|
|
194
|
+
|
|
195
|
+
if (hasPsP1Fields) {
|
|
196
|
+
mode = 'announcement'
|
|
197
|
+
record = encodePsP1({
|
|
198
|
+
poolCashAddr,
|
|
199
|
+
satsPerMb,
|
|
200
|
+
validFromHeight,
|
|
201
|
+
prevRegistryTxid
|
|
202
|
+
})
|
|
203
|
+
opReturn = record.scriptHex
|
|
204
|
+
} else {
|
|
205
|
+
mode = 'update'
|
|
206
|
+
record = buildUpdateOpReturn({ cid, endorses, ts })
|
|
207
|
+
opReturn = record.scriptHex
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const built = buildTransaction({
|
|
211
|
+
opReturnScriptHex: opReturn,
|
|
212
|
+
inputs,
|
|
213
|
+
privKeys,
|
|
214
|
+
council,
|
|
215
|
+
changeAddress,
|
|
216
|
+
fee,
|
|
217
|
+
network
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
const out = Object.assign({}, built, {
|
|
221
|
+
mode,
|
|
222
|
+
opReturnHex: opReturn,
|
|
223
|
+
record
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
return out
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export {
|
|
230
|
+
buildUpdateOpReturn,
|
|
231
|
+
parseUpdateRecord,
|
|
232
|
+
buildUpdateTransaction
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export default {
|
|
236
|
+
buildUpdateOpReturn,
|
|
237
|
+
parseUpdateRecord,
|
|
238
|
+
buildUpdateTransaction
|
|
239
|
+
}
|