@bsv/templates 1.3.0 → 1.4.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/dist/cjs/mod.js +3 -3
- package/dist/cjs/mod.js.map +1 -1
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/{MultiSigPubkeyHash.js → P2MSKH.js} +35 -40
- package/dist/cjs/src/P2MSKH.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/mod.js +1 -1
- package/dist/esm/mod.js.map +1 -1
- package/dist/esm/src/{MultiSigPubkeyHash.js → P2MSKH.js} +34 -39
- package/dist/esm/src/P2MSKH.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +1 -1
- package/dist/types/mod.d.ts.map +1 -1
- package/dist/types/src/{MultiSigPubkeyHash.d.ts → P2MSKH.d.ts} +6 -6
- package/dist/types/src/P2MSKH.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/mod.ts +1 -1
- package/package.json +1 -1
- package/src/MultiPushDrop.ts +1 -1
- package/src/P2MSKH.ts +218 -0
- package/dist/cjs/src/Metanet.js +0 -45
- package/dist/cjs/src/Metanet.js.map +0 -1
- package/dist/cjs/src/MultiSigPubkeyHash.js.map +0 -1
- package/dist/esm/src/Metanet.js +0 -42
- package/dist/esm/src/Metanet.js.map +0 -1
- package/dist/esm/src/MultiSigPubkeyHash.js.map +0 -1
- package/dist/types/src/Metanet.d.ts +0 -29
- package/dist/types/src/Metanet.d.ts.map +0 -1
- package/dist/types/src/MultiSigPubkeyHash.d.ts.map +0 -1
- package/src/MultiSigPubkeyHash.ts +0 -224
package/src/P2MSKH.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { ScriptTemplate, LockingScript, UnlockingScript, OP, Hash, PublicKey, TransactionSignature, Signature, Utils, WalletInterface, Transaction, ScriptChunk } from '@bsv/sdk'
|
|
2
|
+
|
|
3
|
+
export interface MultiSigInstructions {
|
|
4
|
+
keyID: string
|
|
5
|
+
counterparty: string
|
|
6
|
+
pubkeys: string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function concatPubkeys (pubkeys: PublicKey[]): number[] {
|
|
10
|
+
return pubkeys.map((p) => p.toDER() as number[]).reduce((a, b) => a.concat(b), [])
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function numberFromScriptChunk (chunk: ScriptChunk): number {
|
|
14
|
+
let returnNum: number
|
|
15
|
+
if (chunk.data == null) {
|
|
16
|
+
returnNum = 1 + (chunk.op) - OP.OP_1
|
|
17
|
+
} else {
|
|
18
|
+
const reader = new Utils.Reader(chunk.data)
|
|
19
|
+
const threshold = reader.readInt64LEBn()
|
|
20
|
+
returnNum = threshold.toNumber()
|
|
21
|
+
}
|
|
22
|
+
return returnNum
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class P2MSKH implements ScriptTemplate {
|
|
26
|
+
static address (pubkeys: PublicKey[], threshold: number): string {
|
|
27
|
+
if (threshold < 1 || threshold > pubkeys.length) throw new Error('threshold must be between 1 and the number of pubkeys')
|
|
28
|
+
if (!pubkeys || pubkeys.length < 2 || pubkeys.length < threshold) throw new Error(`at least ${threshold || 2} pubkeys are required`)
|
|
29
|
+
const concat = concatPubkeys(pubkeys)
|
|
30
|
+
const hash = Hash.hash160(concat)
|
|
31
|
+
const writer = new Utils.Writer()
|
|
32
|
+
writer.write(hash)
|
|
33
|
+
writer.writeVarIntNum(threshold)
|
|
34
|
+
writer.writeVarIntNum(pubkeys.length)
|
|
35
|
+
const data = writer.toArray()
|
|
36
|
+
return Utils.toBase58Check(data, [0x98])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static async addressBRC29 (wallet: WalletInterface, counterparties: string[], keyID: string, threshold: number): Promise<{ pubkeys: string[], address: string }> {
|
|
40
|
+
const pubkeys = await Promise.all(counterparties.map(async (counterparty) => {
|
|
41
|
+
const { publicKey } = await wallet.getPublicKey({
|
|
42
|
+
protocolID: [1, 'multi sig brc29'],
|
|
43
|
+
keyID,
|
|
44
|
+
counterparty
|
|
45
|
+
})
|
|
46
|
+
return PublicKey.fromString(publicKey)
|
|
47
|
+
}))
|
|
48
|
+
return { pubkeys: pubkeys.map(p => p.toString()), address: this.address(pubkeys, threshold) }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static thresholdAndTotalFromAddress (address: string): { hash: number[], threshold: number, total: number } {
|
|
52
|
+
const h = Utils.fromBase58Check(address)
|
|
53
|
+
if (h.prefix[0] !== 0x98) {
|
|
54
|
+
throw new Error('only P2MSH is supported, set your prefix byte to 0x98')
|
|
55
|
+
}
|
|
56
|
+
const reader = new Utils.Reader(h.data as number[])
|
|
57
|
+
const hash = reader.read(20)
|
|
58
|
+
const threshold = reader.readVarIntNum()
|
|
59
|
+
const total = reader.readVarIntNum()
|
|
60
|
+
return { hash, threshold, total }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
lock (
|
|
64
|
+
address?: string,
|
|
65
|
+
pubkeys?: PublicKey[],
|
|
66
|
+
threshold: number = 1
|
|
67
|
+
): LockingScript {
|
|
68
|
+
let hash: number[]
|
|
69
|
+
let total: number = pubkeys?.length || 0
|
|
70
|
+
if (address) {
|
|
71
|
+
if (typeof address !== 'string') throw new Error('address must be a string')
|
|
72
|
+
const result = P2MSKH.thresholdAndTotalFromAddress(address)
|
|
73
|
+
hash = result.hash
|
|
74
|
+
total = result.total
|
|
75
|
+
threshold = result.threshold
|
|
76
|
+
} else {
|
|
77
|
+
if ((pubkeys == null) || total < 2) throw new Error('at least 2 pubkeys are required')
|
|
78
|
+
const concat = concatPubkeys(pubkeys)
|
|
79
|
+
hash = Hash.hash160(concat)
|
|
80
|
+
}
|
|
81
|
+
if (!threshold || threshold < 1 || threshold > total) throw new Error('threshold must be between 1 and the number of pubkeys')
|
|
82
|
+
if (total > 10) throw new Error('total must be less than or equal to 10')
|
|
83
|
+
|
|
84
|
+
const script = new LockingScript()
|
|
85
|
+
script
|
|
86
|
+
.writeOpCode(OP.OP_DUP)
|
|
87
|
+
.writeOpCode(OP.OP_HASH160)
|
|
88
|
+
.writeBin(hash)
|
|
89
|
+
.writeOpCode(OP.OP_EQUALVERIFY)
|
|
90
|
+
.writeNumber(threshold)
|
|
91
|
+
.writeOpCode(OP.OP_SWAP)
|
|
92
|
+
for (let i = 0; i < total - 1; i++) {
|
|
93
|
+
script
|
|
94
|
+
.writeNumber(33)
|
|
95
|
+
.writeOpCode(OP.OP_SPLIT)
|
|
96
|
+
}
|
|
97
|
+
script.writeNumber(total)
|
|
98
|
+
script.writeOpCode(OP.OP_CHECKMULTISIG)
|
|
99
|
+
|
|
100
|
+
return script
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
unlock (
|
|
104
|
+
wallet: WalletInterface,
|
|
105
|
+
customInstructions: MultiSigInstructions,
|
|
106
|
+
workingUnlockingScript?: UnlockingScript,
|
|
107
|
+
signOutputs: 'all' | 'none' | 'single' = 'all',
|
|
108
|
+
anyoneCanPay = false,
|
|
109
|
+
sourceSatoshis?: number,
|
|
110
|
+
lockingScript?: LockingScript
|
|
111
|
+
): {
|
|
112
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>
|
|
113
|
+
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
|
|
114
|
+
} {
|
|
115
|
+
return {
|
|
116
|
+
sign: async (tx: Transaction, inputIndex: number) => {
|
|
117
|
+
if (workingUnlockingScript == null) {
|
|
118
|
+
workingUnlockingScript = new UnlockingScript()
|
|
119
|
+
workingUnlockingScript.writeOpCode(OP.OP_0)
|
|
120
|
+
const pubkeys = concatPubkeys(customInstructions.pubkeys.map(p => PublicKey.fromString(p)))
|
|
121
|
+
workingUnlockingScript.writeBin(pubkeys)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let signatureScope = TransactionSignature.SIGHASH_FORKID
|
|
125
|
+
if (signOutputs === 'all') {
|
|
126
|
+
signatureScope |= TransactionSignature.SIGHASH_ALL
|
|
127
|
+
}
|
|
128
|
+
if (signOutputs === 'none') {
|
|
129
|
+
signatureScope |= TransactionSignature.SIGHASH_NONE
|
|
130
|
+
}
|
|
131
|
+
if (signOutputs === 'single') {
|
|
132
|
+
signatureScope |= TransactionSignature.SIGHASH_SINGLE
|
|
133
|
+
}
|
|
134
|
+
if (anyoneCanPay) {
|
|
135
|
+
signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY
|
|
136
|
+
}
|
|
137
|
+
const input = tx.inputs[inputIndex]
|
|
138
|
+
|
|
139
|
+
const otherInputs = tx.inputs.filter(
|
|
140
|
+
(_, index) => index !== inputIndex
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
const sourceTXID = input.sourceTXID
|
|
144
|
+
? input.sourceTXID
|
|
145
|
+
: input.sourceTransaction?.id('hex')
|
|
146
|
+
|
|
147
|
+
if (!sourceTXID) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
'The input sourceTXID or sourceTransaction is required for transaction signing.'
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
sourceSatoshis ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis
|
|
153
|
+
if (!sourceSatoshis) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
'The sourceSatoshis or input sourceTransaction is required for transaction signing.'
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
lockingScript ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript
|
|
159
|
+
if (lockingScript == null) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
'The lockingScript or input sourceTransaction is required for transaction signing.'
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const preimage = TransactionSignature.format({
|
|
166
|
+
sourceTXID,
|
|
167
|
+
sourceOutputIndex: input.sourceOutputIndex,
|
|
168
|
+
sourceSatoshis,
|
|
169
|
+
transactionVersion: tx.version,
|
|
170
|
+
otherInputs,
|
|
171
|
+
inputIndex,
|
|
172
|
+
outputs: tx.outputs,
|
|
173
|
+
inputSequence: input.sequence || 0xffffffff,
|
|
174
|
+
subscript: lockingScript,
|
|
175
|
+
lockTime: tx.lockTime,
|
|
176
|
+
scope: signatureScope
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
const hashToDirectlySign = Hash.hash256(preimage)
|
|
180
|
+
|
|
181
|
+
const { signature } = await wallet.createSignature({
|
|
182
|
+
hashToDirectlySign,
|
|
183
|
+
protocolID: [1, 'multi sig brc29'],
|
|
184
|
+
counterparty: customInstructions.counterparty,
|
|
185
|
+
keyID: customInstructions.keyID
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const s = Signature.fromDER(signature)
|
|
189
|
+
const sig = new TransactionSignature(s.r, s.s, signatureScope)
|
|
190
|
+
const sigForScript = sig.toChecksigFormat()
|
|
191
|
+
|
|
192
|
+
workingUnlockingScript.writeBin(sigForScript)
|
|
193
|
+
const chunkforSig = workingUnlockingScript.chunks.pop() as ScriptChunk
|
|
194
|
+
// add it to the array before the pubkeys, pushing the other content to the right
|
|
195
|
+
workingUnlockingScript.chunks.splice(workingUnlockingScript.chunks.length - 1, 0, chunkforSig)
|
|
196
|
+
return workingUnlockingScript
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
estimateLength: async (tx: Transaction, inputIndex: number) => {
|
|
200
|
+
let numberOfPubkeys
|
|
201
|
+
let numberOfSignatures
|
|
202
|
+
const staticLength = 8
|
|
203
|
+
const input = tx.inputs[inputIndex]
|
|
204
|
+
const lockingScript = input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript
|
|
205
|
+
if (lockingScript == null) {
|
|
206
|
+
return await Promise.resolve(1000) // guess
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const totalChunk = lockingScript.chunks.at(-2) as { op: number, data: number[] }
|
|
210
|
+
numberOfPubkeys = numberFromScriptChunk(totalChunk)
|
|
211
|
+
|
|
212
|
+
const thresholdChunk = lockingScript.chunks[4] as { op: number, data: number[] }
|
|
213
|
+
numberOfSignatures = numberFromScriptChunk(thresholdChunk)
|
|
214
|
+
return await Promise.resolve(staticLength + (numberOfSignatures * 74) + (numberOfPubkeys * 34))
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
package/dist/cjs/src/Metanet.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const sdk_1 = require("@bsv/sdk");
|
|
4
|
-
/**
|
|
5
|
-
* Metanet class implementing ScriptTemplate.
|
|
6
|
-
*
|
|
7
|
-
* This class provides methods to create Metanet outputs from data. Only lock script is available.
|
|
8
|
-
*/
|
|
9
|
-
class Metanet {
|
|
10
|
-
/**
|
|
11
|
-
* Creates a Metanet output script
|
|
12
|
-
*
|
|
13
|
-
* @param {PublicKey} pubkey the public key responsible for the metanet node
|
|
14
|
-
* @param {string} parentTXID the TXID of the parent metanet transaction or null for root node
|
|
15
|
-
* @param {string[]} data the output data, an array of metadata ending in data payload
|
|
16
|
-
* @returns {LockingScript} - A Metanet locking script.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* // creates a root metanet output with 'subprotocol' and 'filename' metadata followed by data
|
|
20
|
-
* lock(pubkey, null, txid, ['subprotocol', 'filename', data ])
|
|
21
|
-
*/
|
|
22
|
-
lock(pubkey, parentTXID, data = []) {
|
|
23
|
-
const script = [
|
|
24
|
-
{ op: sdk_1.OP.OP_FALSE },
|
|
25
|
-
{ op: sdk_1.OP.OP_RETURN }
|
|
26
|
-
];
|
|
27
|
-
const fields = [
|
|
28
|
-
'meta',
|
|
29
|
-
pubkey.toString(),
|
|
30
|
-
parentTXID !== null && parentTXID !== void 0 ? parentTXID : 'null'
|
|
31
|
-
].concat(data);
|
|
32
|
-
for (const field of fields.filter(Boolean)) {
|
|
33
|
-
script.push({ op: field.length, data: sdk_1.Utils.toArray(field) });
|
|
34
|
-
}
|
|
35
|
-
return new sdk_1.LockingScript(script);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Unlock method is not available for Metanet scripts, throws exception.
|
|
39
|
-
*/
|
|
40
|
-
unlock() {
|
|
41
|
-
throw new Error('Unlock is not supported for Metanet scripts');
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.default = Metanet;
|
|
45
|
-
//# sourceMappingURL=Metanet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Metanet.js","sourceRoot":"","sources":["../../../src/Metanet.ts"],"names":[],"mappings":";;AAAA,kCAA4G;AAE5G;;;;GAIG;AACH,MAAqB,OAAO;IAC1B;;;;;;;;;;;KAWC;IACD,IAAI,CAAE,MAAiB,EAAE,UAAyB,EAAE,OAA0B,EAAE;QAC9E,MAAM,MAAM,GAA2C;YACrD,EAAE,EAAE,EAAE,QAAE,CAAC,QAAQ,EAAE;YACnB,EAAE,EAAE,EAAE,QAAE,CAAC,SAAS,EAAE;SACrB,CAAA;QAED,MAAM,MAAM,GAAG;YACb,MAAM;YACN,MAAM,CAAC,QAAQ,EAAE;YACjB,UAAU,aAAV,UAAU,cAAV,UAAU,GAAI,MAAM;SACrB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAEd,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3C,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,WAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,IAAI,mBAAa,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;IAED;;QAEI;IACJ,MAAM;QAIJ,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;IAChE,CAAC;CACF;AAzCD,0BAyCC"}
|
|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,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;gBACxG,MAAM,cAAc,GAAG,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,MAAM,CAAC,YAAY,CAAmC,CAAA;gBAC5F,kBAAkB,GAAG,qBAAqB,CAAC,cAAc,CAAC,CAAA;gBAC1D,OAAO,OAAO,CAAC,OAAO,CAAC,YAAY,GAAG,CAAC,kBAAkB,GAAG,EAAE,CAAC,GAAK,CAAC,eAAe,GAAG,EAAE,CAAC,CAAC,CAAA;YAC/F,CAAC;SACJ,CAAA;IACL,CAAC;CACJ;AAvMD,gDAuMC"}
|
package/dist/esm/src/Metanet.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { OP, LockingScript, Utils } from '@bsv/sdk';
|
|
2
|
-
/**
|
|
3
|
-
* Metanet class implementing ScriptTemplate.
|
|
4
|
-
*
|
|
5
|
-
* This class provides methods to create Metanet outputs from data. Only lock script is available.
|
|
6
|
-
*/
|
|
7
|
-
export default class Metanet {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a Metanet output script
|
|
10
|
-
*
|
|
11
|
-
* @param {PublicKey} pubkey the public key responsible for the metanet node
|
|
12
|
-
* @param {string} parentTXID the TXID of the parent metanet transaction or null for root node
|
|
13
|
-
* @param {string[]} data the output data, an array of metadata ending in data payload
|
|
14
|
-
* @returns {LockingScript} - A Metanet locking script.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // creates a root metanet output with 'subprotocol' and 'filename' metadata followed by data
|
|
18
|
-
* lock(pubkey, null, txid, ['subprotocol', 'filename', data ])
|
|
19
|
-
*/
|
|
20
|
-
lock(pubkey, parentTXID, data = []) {
|
|
21
|
-
const script = [
|
|
22
|
-
{ op: OP.OP_FALSE },
|
|
23
|
-
{ op: OP.OP_RETURN }
|
|
24
|
-
];
|
|
25
|
-
const fields = [
|
|
26
|
-
'meta',
|
|
27
|
-
pubkey.toString(),
|
|
28
|
-
parentTXID ?? 'null'
|
|
29
|
-
].concat(data);
|
|
30
|
-
for (const field of fields.filter(Boolean)) {
|
|
31
|
-
script.push({ op: field.length, data: Utils.toArray(field) });
|
|
32
|
-
}
|
|
33
|
-
return new LockingScript(script);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Unlock method is not available for Metanet scripts, throws exception.
|
|
37
|
-
*/
|
|
38
|
-
unlock() {
|
|
39
|
-
throw new Error('Unlock is not supported for Metanet scripts');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
//# sourceMappingURL=Metanet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Metanet.js","sourceRoot":"","sources":["../../../src/Metanet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAA6B,aAAa,EAAgC,KAAK,EAAE,MAAM,UAAU,CAAA;AAE5G;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B;;;;;;;;;;;KAWC;IACD,IAAI,CAAE,MAAiB,EAAE,UAAyB,EAAE,OAA0B,EAAE;QAC9E,MAAM,MAAM,GAA2C;YACrD,EAAE,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE;YACnB,EAAE,EAAE,EAAE,EAAE,CAAC,SAAS,EAAE;SACrB,CAAA;QAED,MAAM,MAAM,GAAG;YACb,MAAM;YACN,MAAM,CAAC,QAAQ,EAAE;YACjB,UAAU,IAAI,MAAM;SACrB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAEd,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3C,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,IAAI,aAAa,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;IAED;;QAEI;IACJ,MAAM;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AM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;gBACxG,MAAM,cAAc,GAAG,aAAa,EAAE,MAAM,CAAC,YAAY,CAAmC,CAAA;gBAC5F,kBAAkB,GAAG,qBAAqB,CAAC,cAAc,CAAC,CAAA;gBAC1D,OAAO,OAAO,CAAC,OAAO,CAAC,YAAY,GAAG,CAAC,kBAAkB,GAAG,EAAE,CAAC,GAAK,CAAC,eAAe,GAAG,EAAE,CAAC,CAAC,CAAA;YAC/F,CAAC;SACJ,CAAA;IACL,CAAC;CACJ"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { PublicKey, ScriptTemplate, LockingScript, UnlockingScript, Transaction } from '@bsv/sdk';
|
|
2
|
-
/**
|
|
3
|
-
* Metanet class implementing ScriptTemplate.
|
|
4
|
-
*
|
|
5
|
-
* This class provides methods to create Metanet outputs from data. Only lock script is available.
|
|
6
|
-
*/
|
|
7
|
-
export default class Metanet implements ScriptTemplate {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a Metanet output script
|
|
10
|
-
*
|
|
11
|
-
* @param {PublicKey} pubkey the public key responsible for the metanet node
|
|
12
|
-
* @param {string} parentTXID the TXID of the parent metanet transaction or null for root node
|
|
13
|
-
* @param {string[]} data the output data, an array of metadata ending in data payload
|
|
14
|
-
* @returns {LockingScript} - A Metanet locking script.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // creates a root metanet output with 'subprotocol' and 'filename' metadata followed by data
|
|
18
|
-
* lock(pubkey, null, txid, ['subprotocol', 'filename', data ])
|
|
19
|
-
*/
|
|
20
|
-
lock(pubkey: PublicKey, parentTXID: string | null, data?: string[] | string): LockingScript;
|
|
21
|
-
/**
|
|
22
|
-
* Unlock method is not available for Metanet scripts, throws exception.
|
|
23
|
-
*/
|
|
24
|
-
unlock(): {
|
|
25
|
-
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
26
|
-
estimateLength: () => Promise<number>;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
//# sourceMappingURL=Metanet.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Metanet.d.ts","sourceRoot":"","sources":["../../../src/Metanet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,SAAS,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,WAAW,EAAS,MAAM,UAAU,CAAA;AAE5G;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,OAAQ,YAAW,cAAc;IACpD;;;;;;;;;;;KAWC;IACD,IAAI,CAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,GAAE,MAAM,EAAE,GAAG,MAAW,GAAG,aAAa;IAmBhG;;QAEI;IACJ,MAAM,IAAK;QACT,IAAI,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;QACvE,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;KACtC;CAGF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MultiSigPubkeyHash.d.ts","sourceRoot":"","sources":["../../../src/MultiSigPubkeyHash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAY,SAAS,EAA0C,eAAe,EAAE,WAAW,EAAe,MAAM,UAAU,CAAA;AAEjL,MAAM,MAAM,oBAAoB,GAAG;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE,MAAM,EAAE,CAAA;CACpB,CAAA;AAkBD,qBAAa,kBAAmB,YAAW,cAAc;IAErD,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;WAalD,YAAY,CAAC,MAAM,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAY/J,MAAM,CAAC,4BAA4B,CAAC,OAAO,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IAY1G,IAAI,CACA,OAAO,CAAC,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,SAAS,EAAE,EACrB,SAAS,GAAE,MAAU,GACtB,aAAa;IAuChB,MAAM,CACF,MAAM,EAAE,eAAe,EACvB,kBAAkB,EAAE,oBAAoB,EACxC,sBAAsB,CAAC,EAAE,eAAe,EACxC,WAAW,GAAE,KAAK,GAAG,MAAM,GAAG,QAAgB,EAC9C,YAAY,UAAQ,EACpB,cAAc,CAAC,EAAE,MAAM,EACvB,aAAa,CAAC,EAAE,aAAa,GAC9B;QACC,IAAI,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;QACxE,cAAc,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;KAC1E;CA0GN"}
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { ScriptTemplate, LockingScript, UnlockingScript, OP, Hash, PublicKey, TransactionSignature, Signature, Utils, WalletInterface, Transaction, ScriptChunk } from "@bsv/sdk"
|
|
2
|
-
|
|
3
|
-
export type MultiSigInstructions = {
|
|
4
|
-
keyID: string
|
|
5
|
-
counterparty: string
|
|
6
|
-
pubkeys: string[]
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function concatPubkeys(pubkeys: PublicKey[]): number[] {
|
|
10
|
-
return pubkeys.map((p) => p.toDER() as number[]).reduce((a, b) => a.concat(b), [])
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function numberFromScriptChunk(chunk: ScriptChunk): number {
|
|
14
|
-
let returnNum: number
|
|
15
|
-
if (!chunk.data) {
|
|
16
|
-
returnNum = 1 + (chunk.op as number) - OP.OP_1
|
|
17
|
-
} else {
|
|
18
|
-
const reader = new Utils.Reader(chunk.data)
|
|
19
|
-
const threshold = reader.readInt64LEBn()
|
|
20
|
-
returnNum = threshold.toNumber()
|
|
21
|
-
}
|
|
22
|
-
return returnNum
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class MultiSigPubkeyHash implements ScriptTemplate {
|
|
26
|
-
|
|
27
|
-
static address(pubkeys: PublicKey[], threshold: number): string {
|
|
28
|
-
if (threshold < 1 || threshold > pubkeys.length) throw new Error('threshold must be between 1 and the number of pubkeys')
|
|
29
|
-
if (!pubkeys || pubkeys.length < 2 || pubkeys.length < threshold) throw new Error(`at least ${threshold || 2} pubkeys are required`)
|
|
30
|
-
const concat = concatPubkeys(pubkeys)
|
|
31
|
-
const hash = Hash.hash160(concat)
|
|
32
|
-
const writer = new Utils.Writer()
|
|
33
|
-
writer.write(hash)
|
|
34
|
-
writer.writeVarIntNum(threshold)
|
|
35
|
-
writer.writeVarIntNum(pubkeys.length)
|
|
36
|
-
const data = writer.toArray()
|
|
37
|
-
return Utils.toBase58Check(data, [0x98])
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static async addressBRC29(wallet: WalletInterface, counterparties: string[], keyID: string, threshold: number): Promise<{ pubkeys: string[], address: string }> {
|
|
41
|
-
const pubkeys = await Promise.all(counterparties.map(async (counterparty) => {
|
|
42
|
-
const { publicKey } = await wallet.getPublicKey({
|
|
43
|
-
protocolID: [1, "multi sig brc29"],
|
|
44
|
-
keyID,
|
|
45
|
-
counterparty
|
|
46
|
-
})
|
|
47
|
-
return PublicKey.fromString(publicKey)
|
|
48
|
-
}))
|
|
49
|
-
return { pubkeys: pubkeys.map(p => p.toString()), address: this.address(pubkeys, threshold) }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static thresholdAndTotalFromAddress(address: string): { hash: number[], threshold: number, total: number } {
|
|
53
|
-
const h = Utils.fromBase58Check(address)
|
|
54
|
-
if (h.prefix[0] !== 0x98) {
|
|
55
|
-
throw new Error('only P2MSH is supported, set your prefix byte to 0x98')
|
|
56
|
-
}
|
|
57
|
-
const reader = new Utils.Reader(h.data as number[])
|
|
58
|
-
const hash = reader.read(20)
|
|
59
|
-
const threshold = reader.readVarIntNum()
|
|
60
|
-
const total = reader.readVarIntNum()
|
|
61
|
-
return { hash, threshold, total }
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
lock(
|
|
65
|
-
address?: string,
|
|
66
|
-
pubkeys?: PublicKey[],
|
|
67
|
-
threshold: number = 1,
|
|
68
|
-
): LockingScript {
|
|
69
|
-
let hash: number[]
|
|
70
|
-
let total: number = pubkeys?.length || 0
|
|
71
|
-
if (address) {
|
|
72
|
-
if (typeof address !== 'string') throw new Error('address must be a string')
|
|
73
|
-
const result = MultiSigPubkeyHash.thresholdAndTotalFromAddress(address)
|
|
74
|
-
hash = result.hash
|
|
75
|
-
total = result.total
|
|
76
|
-
threshold = result.threshold
|
|
77
|
-
} else {
|
|
78
|
-
if (!pubkeys || total < 2) throw new Error(`at least 2 pubkeys are required`)
|
|
79
|
-
const concat = concatPubkeys(pubkeys)
|
|
80
|
-
hash = Hash.hash160(concat)
|
|
81
|
-
}
|
|
82
|
-
if (!threshold || threshold < 1 || threshold > total) throw new Error('threshold must be between 1 and the number of pubkeys')
|
|
83
|
-
if (total > 10) throw new Error('total must be less than or equal to 10')
|
|
84
|
-
|
|
85
|
-
const script = new LockingScript();
|
|
86
|
-
for (let i = 0; i < total - 1; i++) {
|
|
87
|
-
script.writeOpCode(OP.OP_CAT)
|
|
88
|
-
}
|
|
89
|
-
script
|
|
90
|
-
.writeOpCode(OP.OP_DUP)
|
|
91
|
-
.writeOpCode(OP.OP_HASH160)
|
|
92
|
-
.writeBin(hash)
|
|
93
|
-
.writeOpCode(OP.OP_EQUALVERIFY)
|
|
94
|
-
.writeNumber(threshold)
|
|
95
|
-
.writeOpCode(OP.OP_SWAP);
|
|
96
|
-
for (let i = 0; i < total - 1; i++) {
|
|
97
|
-
script
|
|
98
|
-
.writeNumber(33)
|
|
99
|
-
.writeOpCode(OP.OP_SPLIT)
|
|
100
|
-
}
|
|
101
|
-
script.writeNumber(total)
|
|
102
|
-
script.writeOpCode(OP.OP_CHECKMULTISIG);
|
|
103
|
-
|
|
104
|
-
return script
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
unlock(
|
|
108
|
-
wallet: WalletInterface,
|
|
109
|
-
customInstructions: MultiSigInstructions,
|
|
110
|
-
workingUnlockingScript?: UnlockingScript,
|
|
111
|
-
signOutputs: "all" | "none" | "single" = "all",
|
|
112
|
-
anyoneCanPay = false,
|
|
113
|
-
sourceSatoshis?: number,
|
|
114
|
-
lockingScript?: LockingScript
|
|
115
|
-
): {
|
|
116
|
-
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
117
|
-
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>;
|
|
118
|
-
} {
|
|
119
|
-
return {
|
|
120
|
-
sign: async (tx: Transaction, inputIndex: number) => {
|
|
121
|
-
if (!workingUnlockingScript) {
|
|
122
|
-
workingUnlockingScript = new UnlockingScript()
|
|
123
|
-
workingUnlockingScript.writeOpCode(OP.OP_0)
|
|
124
|
-
customInstructions.pubkeys.forEach((pubkey) => {
|
|
125
|
-
workingUnlockingScript!.writeBin(PublicKey.fromString(pubkey).toDER() as number[])
|
|
126
|
-
})
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let signatureScope = TransactionSignature.SIGHASH_FORKID;
|
|
130
|
-
if (signOutputs === "all") {
|
|
131
|
-
signatureScope |= TransactionSignature.SIGHASH_ALL;
|
|
132
|
-
}
|
|
133
|
-
if (signOutputs === "none") {
|
|
134
|
-
signatureScope |= TransactionSignature.SIGHASH_NONE;
|
|
135
|
-
}
|
|
136
|
-
if (signOutputs === "single") {
|
|
137
|
-
signatureScope |= TransactionSignature.SIGHASH_SINGLE;
|
|
138
|
-
}
|
|
139
|
-
if (anyoneCanPay) {
|
|
140
|
-
signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY;
|
|
141
|
-
}
|
|
142
|
-
const input = tx.inputs[inputIndex];
|
|
143
|
-
|
|
144
|
-
const otherInputs = tx.inputs.filter(
|
|
145
|
-
(_, index) => index !== inputIndex
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
const sourceTXID = input.sourceTXID
|
|
149
|
-
? input.sourceTXID
|
|
150
|
-
: input.sourceTransaction?.id("hex")
|
|
151
|
-
|
|
152
|
-
if (!sourceTXID) {
|
|
153
|
-
throw new Error(
|
|
154
|
-
"The input sourceTXID or sourceTransaction is required for transaction signing."
|
|
155
|
-
)
|
|
156
|
-
}
|
|
157
|
-
sourceSatoshis ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis;
|
|
158
|
-
if (!sourceSatoshis) {
|
|
159
|
-
throw new Error(
|
|
160
|
-
"The sourceSatoshis or input sourceTransaction is required for transaction signing."
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
lockingScript ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript;
|
|
164
|
-
if (!lockingScript) {
|
|
165
|
-
throw new Error(
|
|
166
|
-
"The lockingScript or input sourceTransaction is required for transaction signing."
|
|
167
|
-
)
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const preimage = TransactionSignature.format({
|
|
171
|
-
sourceTXID,
|
|
172
|
-
sourceOutputIndex: input.sourceOutputIndex,
|
|
173
|
-
sourceSatoshis,
|
|
174
|
-
transactionVersion: tx.version,
|
|
175
|
-
otherInputs,
|
|
176
|
-
inputIndex,
|
|
177
|
-
outputs: tx.outputs,
|
|
178
|
-
inputSequence: input.sequence || 0xffffffff,
|
|
179
|
-
subscript: lockingScript,
|
|
180
|
-
lockTime: tx.lockTime,
|
|
181
|
-
scope: signatureScope,
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
const hashToDirectlySign = Hash.hash256(preimage)
|
|
185
|
-
|
|
186
|
-
const { signature } = await wallet.createSignature({
|
|
187
|
-
hashToDirectlySign,
|
|
188
|
-
protocolID: [1, "multi sig brc29"],
|
|
189
|
-
counterparty: customInstructions.counterparty,
|
|
190
|
-
keyID: customInstructions.keyID,
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
const s = Signature.fromDER(signature)
|
|
194
|
-
const sig = new TransactionSignature(s.r, s.s, signatureScope)
|
|
195
|
-
const sigForScript = sig.toChecksigFormat()
|
|
196
|
-
|
|
197
|
-
workingUnlockingScript.writeBin(sigForScript)
|
|
198
|
-
const chunkforSig = workingUnlockingScript.chunks.pop() as ScriptChunk
|
|
199
|
-
// add it to the array before the pubkeys, pushing the other content to the right
|
|
200
|
-
workingUnlockingScript.chunks.splice(workingUnlockingScript.chunks.length - customInstructions.pubkeys.length, 0, chunkforSig)
|
|
201
|
-
return workingUnlockingScript
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
estimateLength: (tx: Transaction, inputIndex: number) => {
|
|
205
|
-
let numberOfPubkeys = 2
|
|
206
|
-
let numberOfSignatures = 1
|
|
207
|
-
const staticLength = 1
|
|
208
|
-
const input = tx.inputs[inputIndex];
|
|
209
|
-
const lockingScript = input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript;
|
|
210
|
-
if (!lockingScript) {
|
|
211
|
-
return Promise.resolve(1000) // guess
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const totalChunk = lockingScript?.chunks[lockingScript.chunks.length - 2] as { op: number, data: number[] }
|
|
215
|
-
numberOfPubkeys = numberFromScriptChunk(totalChunk)
|
|
216
|
-
|
|
217
|
-
const thresholdPos = lockingScript.chunks.map(chunk => chunk.op === OP.OP_EQUALVERIFY).indexOf(true) + 1
|
|
218
|
-
const thresholdChunk = lockingScript?.chunks[thresholdPos] as { op: number, data: number[] }
|
|
219
|
-
numberOfSignatures = numberFromScriptChunk(thresholdChunk)
|
|
220
|
-
return Promise.resolve(staticLength + (numberOfSignatures * 74) + (numberOfPubkeys * 34))
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|