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.
- package/README.md +143 -0
- package/index.js +27 -0
- 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/package.json +1 -1
- package/test/unit/mocks/ps009v2-mocks.js +304 -0
- package/test/unit/ps009v2-flow-unit.js +805 -0
- package/test/unit/ps009v2-vectors-unit.js +407 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 - P2SH multisig signature inspection/verification.
|
|
3
|
+
|
|
4
|
+
Used by validateApproval() step 9 ("assert the Approval Transaction's first
|
|
5
|
+
input spends from that multisig address (i.e. it was signed by the n-of-m
|
|
6
|
+
council keys)").
|
|
7
|
+
|
|
8
|
+
No network access: the caller supplies the spending transaction and the
|
|
9
|
+
prevout scriptPubKey + amount.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import bitcore from '@chris.troutner/bitcore-lib-cash'
|
|
13
|
+
|
|
14
|
+
import { dataPushes } from './script.js'
|
|
15
|
+
|
|
16
|
+
// DER signatures end with the 1-byte sighash type. bitcore uses
|
|
17
|
+
// SIGHASH_ALL | SIGHASH_FORKID (0x41) for BCH.
|
|
18
|
+
const SIGHASH_ALL_FORKID = 0x41
|
|
19
|
+
|
|
20
|
+
// Count signature pushes in a P2SH multisig scriptSig. A multisig scriptSig is
|
|
21
|
+
// OP_0 <sig>... <redeemScript>; the redeem script is the final push and is
|
|
22
|
+
// not a signature.
|
|
23
|
+
function countMultisigSignatures (scriptSigHex) {
|
|
24
|
+
if (typeof scriptSigHex !== 'string' || scriptSigHex.length === 0) return 0
|
|
25
|
+
|
|
26
|
+
let pushes
|
|
27
|
+
try {
|
|
28
|
+
pushes = dataPushes(scriptSigHex)
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let count = 0
|
|
34
|
+
for (let i = 0; i < pushes.length; i++) {
|
|
35
|
+
const push = pushes[i]
|
|
36
|
+
// The final push is the redeem script (starts with a small-integer
|
|
37
|
+
// opcode for m, i.e. 0x51-0x60, and ends with OP_CHECKMULTISIG 0xae).
|
|
38
|
+
const isLast = i === pushes.length - 1
|
|
39
|
+
if (isLast && push.length > 1 && push[0] >= 0x51 && push[0] <= 0x60 && push[push.length - 1] === 0xae) {
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
if (push.length > 8 && push[push.length - 1] === SIGHASH_ALL_FORKID) {
|
|
43
|
+
count++
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return count
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
Verify that a transaction input is a valid spend of a P2SH multisig output.
|
|
52
|
+
|
|
53
|
+
The Bitcoin script interpreter enforces the redeem script's m-of-n
|
|
54
|
+
threshold, so `true` means at least `requiredSignatures` valid signatures
|
|
55
|
+
were present and the redeem script hashes to the supplied P2SH output.
|
|
56
|
+
|
|
57
|
+
Input:
|
|
58
|
+
txHex - full spending transaction hex
|
|
59
|
+
inputIndex - vin index (default 0)
|
|
60
|
+
scriptPubKeyHex - the prevout P2SH locking script hex (a914...87)
|
|
61
|
+
satoshis - the prevout amount (required for BCH FORKID sighash)
|
|
62
|
+
|
|
63
|
+
Output:
|
|
64
|
+
{ valid, reason, error }
|
|
65
|
+
*/
|
|
66
|
+
function verifyMultisigSpend (inObj = {}) {
|
|
67
|
+
const {
|
|
68
|
+
txHex,
|
|
69
|
+
inputIndex = 0,
|
|
70
|
+
scriptPubKeyHex,
|
|
71
|
+
satoshis
|
|
72
|
+
} = inObj
|
|
73
|
+
|
|
74
|
+
if (typeof txHex !== 'string' || txHex.length === 0) {
|
|
75
|
+
throw new Error('txHex is required')
|
|
76
|
+
}
|
|
77
|
+
if (typeof scriptPubKeyHex !== 'string' || !/^[0-9a-fA-F]+$/.test(scriptPubKeyHex)) {
|
|
78
|
+
throw new Error('scriptPubKeyHex is required')
|
|
79
|
+
}
|
|
80
|
+
if (!Number.isInteger(satoshis) || satoshis <= 0) {
|
|
81
|
+
throw new Error('satoshis (prevout amount) is required for BCH FORKID signature checks')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const tx = new bitcore.Transaction(txHex)
|
|
85
|
+
if (!tx.inputs[inputIndex]) {
|
|
86
|
+
return { valid: false, reason: 'INPUT_INDEX_OUT_OF_RANGE', error: null }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const scriptPubKey = new bitcore.Script(scriptPubKeyHex)
|
|
90
|
+
const Interpreter = bitcore.Script.Interpreter
|
|
91
|
+
|
|
92
|
+
const flags = Interpreter.SCRIPT_VERIFY_P2SH |
|
|
93
|
+
Interpreter.SCRIPT_VERIFY_STRICTENC |
|
|
94
|
+
Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID
|
|
95
|
+
|
|
96
|
+
const interpreter = new Interpreter()
|
|
97
|
+
|
|
98
|
+
let valid = false
|
|
99
|
+
try {
|
|
100
|
+
valid = interpreter.verify(
|
|
101
|
+
tx.inputs[inputIndex].script,
|
|
102
|
+
scriptPubKey,
|
|
103
|
+
tx,
|
|
104
|
+
inputIndex,
|
|
105
|
+
flags,
|
|
106
|
+
new bitcore.crypto.BN(satoshis)
|
|
107
|
+
)
|
|
108
|
+
} catch (err) {
|
|
109
|
+
return { valid: false, reason: 'SCRIPT_EVALUATION_ERROR', error: err.message }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
valid,
|
|
114
|
+
reason: valid ? null : (interpreter.errstr || 'SCRIPT_VERIFY_FAILED'),
|
|
115
|
+
error: null
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export {
|
|
120
|
+
SIGHASH_ALL_FORKID,
|
|
121
|
+
countMultisigSignatures,
|
|
122
|
+
verifyMultisigSpend
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export default { countMultisigSignatures, verifyMultisigSpend }
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/*
|
|
2
|
+
PS009 v2 - shared transaction building helpers.
|
|
3
|
+
|
|
4
|
+
These are thin, deterministic wrappers around bitcore-lib-cash. They are
|
|
5
|
+
deliberately network-free: no broadcasting, no wallet, no node RPC. Callers
|
|
6
|
+
inject UTXOs and (optionally) private keys; the helpers build and sign the
|
|
7
|
+
transaction locally.
|
|
8
|
+
|
|
9
|
+
Two signing modes are supported:
|
|
10
|
+
|
|
11
|
+
- P2PKH inputs (Update Transactions, PS009 v2 section 4): pass `privKeys`;
|
|
12
|
+
bitcore signs with the supplied keys.
|
|
13
|
+
- P2SH multisig inputs (Approval Transactions, section 5 / section 6 step 9):
|
|
14
|
+
pass `council` (the output of deriveCouncilAddress()) so bitcore creates
|
|
15
|
+
MultiSigScriptHashInput instances, then pass `privKeys`. Any subset of the
|
|
16
|
+
council keys may be supplied; a partially signed transaction is returned
|
|
17
|
+
when fewer than n keys are available, so multiple operators can sign in
|
|
18
|
+
turn (PS009's whole point).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import bitcore from '@chris.troutner/bitcore-lib-cash'
|
|
22
|
+
|
|
23
|
+
import { resolveNetwork } from './council.js'
|
|
24
|
+
|
|
25
|
+
// PS009 v2 section 5: the Approval Transaction must include a dust output
|
|
26
|
+
// (546 satoshis) in its second output (index 1).
|
|
27
|
+
const DUST_SATOSHIS = 546
|
|
28
|
+
|
|
29
|
+
// Default miner fee for reference transactions (one input, two/three outputs).
|
|
30
|
+
const DEFAULT_FEE = 1000
|
|
31
|
+
|
|
32
|
+
// Normalize a UTXO into the shape bitcore's Transaction#from() expects.
|
|
33
|
+
function normalizeUtxo (utxo, index) {
|
|
34
|
+
if (!utxo || typeof utxo !== 'object') {
|
|
35
|
+
throw new Error(`utxo at index ${index} must be an object`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const txId = utxo.txId || utxo.txid
|
|
39
|
+
const outputIndex = utxo.outputIndex !== undefined ? utxo.outputIndex : utxo.vout
|
|
40
|
+
|
|
41
|
+
if (typeof txId !== 'string' || !/^[0-9a-fA-F]{64}$/.test(txId)) {
|
|
42
|
+
throw new Error(`utxo at index ${index} must include a 64-character txid`)
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isInteger(outputIndex) || outputIndex < 0) {
|
|
45
|
+
throw new Error(`utxo at index ${index} must include a non-negative integer outputIndex`)
|
|
46
|
+
}
|
|
47
|
+
if (!Number.isInteger(utxo.satoshis) || utxo.satoshis <= 0) {
|
|
48
|
+
throw new Error(`utxo at index ${index} must include a positive integer satoshis value`)
|
|
49
|
+
}
|
|
50
|
+
if (typeof utxo.script !== 'string' || !/^[0-9a-fA-F]+$/.test(utxo.script)) {
|
|
51
|
+
throw new Error(`utxo at index ${index} must include the scriptPubKey hex as 'script'`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const out = {
|
|
55
|
+
txId: txId.toLowerCase(),
|
|
56
|
+
outputIndex,
|
|
57
|
+
satoshis: utxo.satoshis,
|
|
58
|
+
script: utxo.script.toLowerCase()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (utxo.address) out.address = utxo.address
|
|
62
|
+
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Build a transaction whose first output is an OP_RETURN, optionally followed
|
|
67
|
+
// by explicit outputs, a change output, and signatures.
|
|
68
|
+
function buildTransaction (inObj = {}) {
|
|
69
|
+
const {
|
|
70
|
+
opReturnScriptHex,
|
|
71
|
+
inputs,
|
|
72
|
+
outputs = [],
|
|
73
|
+
privKeys = [],
|
|
74
|
+
council,
|
|
75
|
+
changeAddress,
|
|
76
|
+
fee = DEFAULT_FEE
|
|
77
|
+
} = inObj
|
|
78
|
+
|
|
79
|
+
if (typeof opReturnScriptHex !== 'string' || !/^6a[0-9a-fA-F]*$/.test(opReturnScriptHex)) {
|
|
80
|
+
throw new Error('opReturnScriptHex must be an OP_RETURN script hex string (starting with 6a)')
|
|
81
|
+
}
|
|
82
|
+
if (!Array.isArray(inputs) || inputs.length === 0) {
|
|
83
|
+
throw new Error('inputs must be a non-empty array of UTXOs')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const utxos = inputs.map((utxo, i) => normalizeUtxo(utxo, i))
|
|
87
|
+
|
|
88
|
+
const { name } = resolveNetwork(inObj.network)
|
|
89
|
+
|
|
90
|
+
const tx = new bitcore.Transaction()
|
|
91
|
+
|
|
92
|
+
if (council) {
|
|
93
|
+
// P2SH multisig input(s): tell bitcore the redeem-script public keys and
|
|
94
|
+
// threshold, so it produces a MultiSigScriptHashInput and can assemble
|
|
95
|
+
// the scriptSig (including the redeem script).
|
|
96
|
+
tx.from(utxos, council.publicKeys, council.requiredSignatures)
|
|
97
|
+
} else {
|
|
98
|
+
tx.from(utxos)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// vout[0] is always the OP_RETURN, per PS009 v2 sections 4 and 5.
|
|
102
|
+
tx.addOutput(new bitcore.Transaction.Output({
|
|
103
|
+
script: new bitcore.Script(opReturnScriptHex),
|
|
104
|
+
satoshis: 0
|
|
105
|
+
}))
|
|
106
|
+
|
|
107
|
+
// Additional explicit outputs (e.g. the 546-sat dust output in an Approval
|
|
108
|
+
// Transaction). Callers pass satoshis + address.
|
|
109
|
+
for (const output of outputs) {
|
|
110
|
+
tx.addOutput(new bitcore.Transaction.Output({
|
|
111
|
+
script: new bitcore.Script(new bitcore.Address(output.address)),
|
|
112
|
+
satoshis: output.satoshis
|
|
113
|
+
}))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (changeAddress) {
|
|
117
|
+
tx.change(changeAddress)
|
|
118
|
+
} else if (council) {
|
|
119
|
+
tx.change(council.address)
|
|
120
|
+
} else {
|
|
121
|
+
throw new Error('changeAddress is required when building a transaction without a council')
|
|
122
|
+
}
|
|
123
|
+
tx.fee(fee)
|
|
124
|
+
|
|
125
|
+
let signed = false
|
|
126
|
+
if (Array.isArray(privKeys) && privKeys.length > 0) {
|
|
127
|
+
tx.sign(privKeys)
|
|
128
|
+
signed = tx.isFullySigned()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// `toString()` refuses to serialize an incomplete transaction, so use
|
|
132
|
+
// unchecked serialization when only some council members have signed.
|
|
133
|
+
const hex = signed ? tx.toString() : tx.uncheckedSerialize()
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
tx,
|
|
137
|
+
hex,
|
|
138
|
+
txid: tx.hash,
|
|
139
|
+
signed,
|
|
140
|
+
fee,
|
|
141
|
+
network: name,
|
|
142
|
+
inputs: utxos.length
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export {
|
|
147
|
+
DUST_SATOSHIS,
|
|
148
|
+
DEFAULT_FEE,
|
|
149
|
+
normalizeUtxo,
|
|
150
|
+
buildTransaction
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export default { buildTransaction, normalizeUtxo }
|
|
@@ -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
|
+
}
|