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,352 @@
1
+ /*
2
+ PS009 v2 - low-level Bitcoin script helpers.
3
+
4
+ No network access. Strict parsing: malformed pushdata is rejected rather
5
+ than truncated (the same rule PS010 section 5.3 applies to Pin Claims).
6
+ */
7
+
8
+ import bitcore from '@chris.troutner/bitcore-lib-cash'
9
+
10
+ // Bitcoin script opcodes used by this library.
11
+ const OP_RETURN = 0x6a
12
+ const OP_PUSHDATA1 = 0x4c
13
+ const OP_PUSHDATA2 = 0x4d
14
+ const OP_PUSHDATA4 = 0x4e
15
+
16
+ const MAX_UINT64 = BigInt('18446744073709551615')
17
+ const B256 = BigInt(256)
18
+
19
+ // Encode a Buffer as a Bitcoin script push (data push only).
20
+ function encodePushData (data) {
21
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data)
22
+ const len = buf.length
23
+
24
+ if (len < OP_PUSHDATA1) {
25
+ return Buffer.concat([Buffer.from([len]), buf])
26
+ }
27
+ if (len <= 0xff) {
28
+ return Buffer.concat([Buffer.from([OP_PUSHDATA1, len]), buf])
29
+ }
30
+ if (len <= 0xffff) {
31
+ const header = Buffer.alloc(3)
32
+ header[0] = OP_PUSHDATA2
33
+ header.writeUInt16LE(len, 1)
34
+ return Buffer.concat([header, buf])
35
+ }
36
+
37
+ const header = Buffer.alloc(5)
38
+ header[0] = OP_PUSHDATA4
39
+ header.writeUInt32LE(len, 1)
40
+ return Buffer.concat([header, buf])
41
+ }
42
+
43
+ // Read every push in a script hex string. Throws on truncated pushes.
44
+ function readScriptPushes (scriptHex) {
45
+ if (typeof scriptHex !== 'string' || !/^[0-9a-fA-F]*$/.test(scriptHex) || scriptHex.length % 2 !== 0) {
46
+ throw new Error('script must be an even-length hex string')
47
+ }
48
+
49
+ const buf = Buffer.from(scriptHex, 'hex')
50
+ const pushes = []
51
+ let i = 0
52
+
53
+ while (i < buf.length) {
54
+ const opcode = buf[i]
55
+ i++
56
+
57
+ let len = null
58
+ if (opcode < OP_PUSHDATA1) {
59
+ len = opcode
60
+ } else if (opcode === OP_PUSHDATA1) {
61
+ if (i + 1 > buf.length) throw new Error('truncated OP_PUSHDATA1 length')
62
+ len = buf[i]
63
+ i += 1
64
+ } else if (opcode === OP_PUSHDATA2) {
65
+ if (i + 2 > buf.length) throw new Error('truncated OP_PUSHDATA2 length')
66
+ len = buf.readUInt16LE(i)
67
+ i += 2
68
+ } else if (opcode === OP_PUSHDATA4) {
69
+ if (i + 4 > buf.length) throw new Error('truncated OP_PUSHDATA4 length')
70
+ len = buf.readUInt32LE(i)
71
+ i += 4
72
+ } else {
73
+ // Non-push opcode: keep it as a marker so callers can reject scripts
74
+ // that are not pure data pushes (OP_RETURN outputs must be push-only).
75
+ pushes.push({ opcode })
76
+ continue
77
+ }
78
+
79
+ if (i + len > buf.length) {
80
+ throw new Error('truncated script push data')
81
+ }
82
+
83
+ pushes.push({ opcode, data: buf.slice(i, i + len) })
84
+ i += len
85
+ }
86
+
87
+ return pushes
88
+ }
89
+
90
+ // Return only the data pushes of a script, ignoring non-push opcodes.
91
+ function dataPushes (scriptHex) {
92
+ const pushes = readScriptPushes(scriptHex)
93
+ const out = []
94
+ for (const push of pushes) {
95
+ if (push.data) out.push(push.data)
96
+ }
97
+ return out
98
+ }
99
+
100
+ // Build a script hex string from an array of data buffers (each becomes one
101
+ // data push).
102
+ function scriptFromPushes (pushes) {
103
+ return Buffer.concat(pushes.map(push => encodePushData(push))).toString('hex')
104
+ }
105
+
106
+ // Build an OP_RETURN script from an array of data buffers.
107
+ function opReturnScript (pushes) {
108
+ return '6a' + scriptFromPushes(pushes)
109
+ }
110
+
111
+ // Parse an ASM string (as produced by bitcore's Script#toASM) into a script.
112
+ // bitcore 8.25's own fromString() can not round-trip OP_RETURN data pushes, so
113
+ // accepted tokens are: a numeric length (ignored, the following token's own
114
+ // length is authoritative), `0x<hex>` data pushes, and bare hex data pushes.
115
+ function asmToScriptHex (asm) {
116
+ if (typeof asm !== 'string' || asm.length === 0) {
117
+ throw new Error('ASM string is required')
118
+ }
119
+
120
+ const tokens = asm.trim().split(/\s+/)
121
+
122
+ if (tokens[0] !== 'OP_RETURN') {
123
+ throw new Error('only OP_RETURN scripts can be parsed from ASM')
124
+ }
125
+
126
+ const pushes = []
127
+ for (let i = 1; i < tokens.length; i++) {
128
+ const token = tokens[i]
129
+
130
+ // Some tools print '<len> 0x<data>' for each push. Only treat a numeric
131
+ // token as a length when the following token is an explicit 0x push: a
132
+ // pushed payload can itself be all digits (e.g. ASCII-hex data).
133
+ if (/^[0-9]+$/.test(token) && tokens[i + 1] && tokens[i + 1].startsWith('0x')) {
134
+ continue
135
+ }
136
+
137
+ if (token.startsWith('0x') && isHexString(token.slice(2))) {
138
+ pushes.push(Buffer.from(token.slice(2), 'hex'))
139
+ continue
140
+ }
141
+
142
+ if (isHexString(token)) {
143
+ pushes.push(Buffer.from(token, 'hex'))
144
+ continue
145
+ }
146
+
147
+ throw new Error(`unsupported ASM token: ${token}`)
148
+ }
149
+
150
+ return opReturnScript(pushes)
151
+ }
152
+
153
+ function isHexString (str) {
154
+ return typeof str === 'string' && str.length % 2 === 0 && /^[0-9a-fA-F]*$/.test(str)
155
+ }
156
+
157
+ // Normalize any of the accepted "script or transaction" inputs into a
158
+ // vout[0] scriptPubKey hex string:
159
+ // - script hex
160
+ // - script ASM (e.g. 'OP_RETURN 50535031 ...')
161
+ // - a transaction details object (bch-api/Fulcrum shape or bitcore shape)
162
+ // - a raw transaction hex wrapped in { hex }
163
+ // - the output of buildUpdateTransaction()/buildApprovalTransaction()
164
+ function extractVout0ScriptHex (input) {
165
+ if (input === null || input === undefined) {
166
+ throw new Error('transaction or script is required')
167
+ }
168
+
169
+ if (typeof input === 'string') {
170
+ const str = input.trim()
171
+
172
+ if (str.startsWith('OP_')) {
173
+ return asmToScriptHex(str)
174
+ }
175
+
176
+ if (isHexString(str)) {
177
+ // A raw transaction hex (version byte first) is not a script; a vout[0]
178
+ // OP_RETURN script starts with 6a. Prefer the script interpretation only
179
+ // when it starts with OP_RETURN, otherwise try the transaction.
180
+ if (str.slice(0, 2).toLowerCase() === '6a') return str.toLowerCase()
181
+
182
+ try {
183
+ const tx = new bitcore.Transaction(str)
184
+ if (tx.outputs[0]) return tx.outputs[0].script.toHex()
185
+ } catch (err) {
186
+ // Fall through and treat it as a script.
187
+ }
188
+
189
+ return str.toLowerCase()
190
+ }
191
+
192
+ throw new Error('string input must be a script hex, transaction hex, or script ASM string')
193
+ }
194
+
195
+ if (typeof input !== 'object') {
196
+ throw new Error('transaction or script input must be a string or object')
197
+ }
198
+
199
+ // Unwrap builder output ({ tx, hex, ... }) and { txDetails } wrappers.
200
+ if (input.tx && typeof input.hex === 'string' && !input.vout && !input.outputs) {
201
+ return extractVout0ScriptHex(input.hex)
202
+ }
203
+ if (input.hex && !input.vout && !input.outputs) {
204
+ return extractVout0ScriptHex(input.hex)
205
+ }
206
+ if (input.tx && input.tx.vout) {
207
+ return extractVout0ScriptHex(input.tx)
208
+ }
209
+ if (input.txDetails && input.txDetails.vout) {
210
+ return extractVout0ScriptHex(input.txDetails)
211
+ }
212
+
213
+ if (Array.isArray(input.vout)) {
214
+ if (!input.vout[0]) throw new Error('transaction has no vout[0]')
215
+ return scriptHexFromVout(input.vout[0])
216
+ }
217
+
218
+ // bitcore Transaction#toObject() shape.
219
+ if (Array.isArray(input.outputs)) {
220
+ if (!input.outputs[0]) throw new Error('transaction has no outputs[0]')
221
+ return scriptHexFromVout(input.outputs[0])
222
+ }
223
+
224
+ throw new Error('could not find a vout[0] scriptPubKey in the supplied transaction object')
225
+ }
226
+
227
+ function scriptHexFromVout (vout) {
228
+ if (typeof vout.script === 'string') return vout.script.toLowerCase()
229
+ if (typeof vout.scriptPubKey === 'string') return vout.scriptPubKey.toLowerCase()
230
+ if (vout.scriptPubKey && typeof vout.scriptPubKey.hex === 'string') {
231
+ return vout.scriptPubKey.hex.toLowerCase()
232
+ }
233
+ if (vout.script && typeof vout.script.toHex === 'function') {
234
+ return vout.script.toHex()
235
+ }
236
+ throw new Error('vout entry has no scriptPubKey hex')
237
+ }
238
+
239
+ function isOpReturn (scriptHex) {
240
+ return typeof scriptHex === 'string' && scriptHex.slice(0, 2).toLowerCase() === '6a'
241
+ }
242
+
243
+ // ----- uint64 little-endian helpers (PS010 section 4.1) -----
244
+
245
+ // Encode a non-negative integer (Number, BigInt, or decimal string) as 8 bytes
246
+ // little-endian, per PS010 section 4.1 ("use 8 bytes even if height fits in 4").
247
+ function encodeUint64LE (value, fieldName) {
248
+ const name = fieldName || 'value'
249
+ let big
250
+
251
+ if (typeof value === 'bigint') {
252
+ big = value
253
+ } else if (typeof value === 'number' || typeof value === 'string') {
254
+ const str = String(value)
255
+ if (!/^[0-9]+$/.test(str)) {
256
+ throw new Error(`${name} must be a non-negative integer`)
257
+ }
258
+ if (typeof value === 'number' && !Number.isSafeInteger(value)) {
259
+ throw new Error(`${name} must be a safe integer (or pass a decimal string / BigInt)`)
260
+ }
261
+ big = BigInt(str)
262
+ } else {
263
+ throw new Error(`${name} must be a non-negative integer`)
264
+ }
265
+
266
+ if (big < BigInt(0) || big > MAX_UINT64) {
267
+ throw new Error(`${name} does not fit in a uint64`)
268
+ }
269
+
270
+ const buf = Buffer.alloc(8)
271
+ for (let i = 0; i < 8; i++) {
272
+ buf[i] = Number(big % B256)
273
+ big = big / B256
274
+ }
275
+
276
+ return buf
277
+ }
278
+
279
+ // Decode 8 little-endian bytes. Returns a Number when the value is a safe
280
+ // integer (all real heights and rates), otherwise a BigInt so no precision is
281
+ // silently lost.
282
+ function decodeUint64LE (buf, fieldName) {
283
+ const name = fieldName || 'value'
284
+ if (!Buffer.isBuffer(buf) || buf.length !== 8) {
285
+ throw new Error(`${name} must be an 8-byte buffer`)
286
+ }
287
+
288
+ let big = BigInt(0)
289
+ for (let i = 7; i >= 0; i--) {
290
+ big = big * B256 + BigInt(buf[i])
291
+ }
292
+
293
+ if (big <= BigInt(Number.MAX_SAFE_INTEGER)) return Number(big)
294
+ return big
295
+ }
296
+
297
+ // Encode a displayed txid (big-endian hex, the form explorers show) into the
298
+ // 32 raw bytes in Bitcoin internal byte order, as PS010 section 4.1 requires
299
+ // for prevRegistryTxid.
300
+ function encodeTxidInternal (txidHex, fieldName) {
301
+ const name = fieldName || 'txid'
302
+ if (typeof txidHex !== 'string' || !/^[0-9a-fA-F]{64}$/.test(txidHex)) {
303
+ throw new Error(`${name} must be a 64-character hex txid`)
304
+ }
305
+ return Buffer.from(txidHex, 'hex').reverse()
306
+ }
307
+
308
+ // Inverse of encodeTxidInternal(): raw internal bytes -> displayed txid hex.
309
+ function decodeTxidInternal (buf, fieldName) {
310
+ const name = fieldName || 'txid'
311
+ if (!Buffer.isBuffer(buf) || buf.length !== 32) {
312
+ throw new Error(`${name} must be a 32-byte buffer`)
313
+ }
314
+ return Buffer.from(buf).reverse().toString('hex')
315
+ }
316
+
317
+ export {
318
+ OP_RETURN,
319
+ OP_PUSHDATA1,
320
+ OP_PUSHDATA2,
321
+ OP_PUSHDATA4,
322
+ MAX_UINT64,
323
+ encodePushData,
324
+ readScriptPushes,
325
+ dataPushes,
326
+ scriptFromPushes,
327
+ opReturnScript,
328
+ asmToScriptHex,
329
+ extractVout0ScriptHex,
330
+ scriptHexFromVout,
331
+ isOpReturn,
332
+ isHexString,
333
+ encodeUint64LE,
334
+ decodeUint64LE,
335
+ encodeTxidInternal,
336
+ decodeTxidInternal
337
+ }
338
+
339
+ export default {
340
+ encodePushData,
341
+ readScriptPushes,
342
+ dataPushes,
343
+ scriptFromPushes,
344
+ opReturnScript,
345
+ asmToScriptHex,
346
+ extractVout0ScriptHex,
347
+ isOpReturn,
348
+ encodeUint64LE,
349
+ decodeUint64LE,
350
+ encodeTxidInternal,
351
+ decodeTxidInternal
352
+ }
@@ -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 }