@bsv/templates 1.3.0 → 1.4.1

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.
Files changed (35) hide show
  1. package/dist/cjs/mod.js +3 -3
  2. package/dist/cjs/mod.js.map +1 -1
  3. package/dist/cjs/package.json +11 -10
  4. package/dist/cjs/src/MultiPushDrop.js +4 -9
  5. package/dist/cjs/src/MultiPushDrop.js.map +1 -1
  6. package/dist/cjs/src/{MultiSigPubkeyHash.js → P2MSKH.js} +36 -41
  7. package/dist/cjs/src/P2MSKH.js.map +1 -0
  8. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  9. package/dist/esm/mod.js +1 -1
  10. package/dist/esm/mod.js.map +1 -1
  11. package/dist/esm/src/MultiPushDrop.js +4 -9
  12. package/dist/esm/src/MultiPushDrop.js.map +1 -1
  13. package/dist/esm/src/{MultiSigPubkeyHash.js → P2MSKH.js} +35 -40
  14. package/dist/esm/src/P2MSKH.js.map +1 -0
  15. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  16. package/dist/types/mod.d.ts +1 -1
  17. package/dist/types/mod.d.ts.map +1 -1
  18. package/dist/types/src/MultiPushDrop.d.ts.map +1 -1
  19. package/dist/types/src/{MultiSigPubkeyHash.d.ts → P2MSKH.d.ts} +6 -6
  20. package/dist/types/src/P2MSKH.d.ts.map +1 -0
  21. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  22. package/mod.ts +1 -1
  23. package/package.json +20 -19
  24. package/src/MultiPushDrop.ts +5 -10
  25. package/src/P2MSKH.ts +218 -0
  26. package/dist/cjs/src/Metanet.js +0 -45
  27. package/dist/cjs/src/Metanet.js.map +0 -1
  28. package/dist/cjs/src/MultiSigPubkeyHash.js.map +0 -1
  29. package/dist/esm/src/Metanet.js +0 -42
  30. package/dist/esm/src/Metanet.js.map +0 -1
  31. package/dist/esm/src/MultiSigPubkeyHash.js.map +0 -1
  32. package/dist/types/src/Metanet.d.ts +0 -29
  33. package/dist/types/src/Metanet.d.ts.map +0 -1
  34. package/dist/types/src/MultiSigPubkeyHash.d.ts.map +0 -1
  35. package/src/MultiSigPubkeyHash.ts +0 -224
@@ -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
- }