@1sat/templates 0.0.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 (57) hide show
  1. package/dist/bitcom/aip.d.ts +88 -0
  2. package/dist/bitcom/aip.d.ts.map +1 -0
  3. package/dist/bitcom/aip.js +205 -0
  4. package/dist/bitcom/aip.js.map +1 -0
  5. package/dist/bitcom/b.d.ts +99 -0
  6. package/dist/bitcom/b.d.ts.map +1 -0
  7. package/dist/bitcom/b.js +206 -0
  8. package/dist/bitcom/b.js.map +1 -0
  9. package/dist/bitcom/bap.d.ts +126 -0
  10. package/dist/bitcom/bap.d.ts.map +1 -0
  11. package/dist/bitcom/bap.js +334 -0
  12. package/dist/bitcom/bap.js.map +1 -0
  13. package/dist/bitcom/bitcom.d.ts +75 -0
  14. package/dist/bitcom/bitcom.d.ts.map +1 -0
  15. package/dist/bitcom/bitcom.js +186 -0
  16. package/dist/bitcom/bitcom.js.map +1 -0
  17. package/dist/bitcom/map.d.ts +90 -0
  18. package/dist/bitcom/map.d.ts.map +1 -0
  19. package/dist/bitcom/map.js +215 -0
  20. package/dist/bitcom/map.js.map +1 -0
  21. package/dist/bitcom/sigma.d.ts +93 -0
  22. package/dist/bitcom/sigma.d.ts.map +1 -0
  23. package/dist/bitcom/sigma.js +175 -0
  24. package/dist/bitcom/sigma.js.map +1 -0
  25. package/dist/bsocial/bsocial.d.ts +152 -0
  26. package/dist/bsocial/bsocial.d.ts.map +1 -0
  27. package/dist/bsocial/bsocial.js +397 -0
  28. package/dist/bsocial/bsocial.js.map +1 -0
  29. package/dist/bsv20/bsv20.d.ts +277 -0
  30. package/dist/bsv20/bsv20.d.ts.map +1 -0
  31. package/dist/bsv20/bsv20.js +544 -0
  32. package/dist/bsv20/bsv20.js.map +1 -0
  33. package/dist/bsv21/bsv21.d.ts +231 -0
  34. package/dist/bsv21/bsv21.d.ts.map +1 -0
  35. package/dist/bsv21/bsv21.js +475 -0
  36. package/dist/bsv21/bsv21.js.map +1 -0
  37. package/dist/index.d.ts +35 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +31 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/inscription/inscription.d.ts +165 -0
  42. package/dist/inscription/inscription.d.ts.map +1 -0
  43. package/dist/inscription/inscription.js +401 -0
  44. package/dist/inscription/inscription.js.map +1 -0
  45. package/dist/lock/lock.d.ts +92 -0
  46. package/dist/lock/lock.d.ts.map +1 -0
  47. package/dist/lock/lock.js +283 -0
  48. package/dist/lock/lock.js.map +1 -0
  49. package/dist/ordlock/ordlock.d.ts +126 -0
  50. package/dist/ordlock/ordlock.d.ts.map +1 -0
  51. package/dist/ordlock/ordlock.js +316 -0
  52. package/dist/ordlock/ordlock.js.map +1 -0
  53. package/dist/signer.d.ts +39 -0
  54. package/dist/signer.d.ts.map +1 -0
  55. package/dist/signer.js +54 -0
  56. package/dist/signer.js.map +1 -0
  57. package/package.json +43 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lock.d.ts","sourceRoot":"","sources":["../../src/lock/lock.ts"],"names":[],"mappings":"AAIA,OAAO,EAGN,KAAK,UAAU,EACf,MAAM,EACN,KAAK,WAAW,EAEhB,KAAK,eAAe,EAEpB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,MAAM,UAAU,CAAA;AAEjB;;GAEG;AACH,eAAO,MAAM,WAAW,OAAwC,CAAA;AAEhE;;GAEG;AACH,eAAO,MAAM,WAAW,OAAwC,CAAA;AAEhE;;GAEG;AACH,MAAM,WAAW,QAAQ;IACxB,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAA;IACf,iEAAiE;IACjE,KAAK,EAAE,MAAM,CAAA;CACb;AA0BD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,OAAO,IAAI;IACxB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,UAAO,GAAG,QAAQ,GAAG,IAAI;IAoD9D;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAOtC;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IASnD;;;;;;;;;OASG;IACH,MAAM,CAAC,MAAM,CACZ,UAAU,EAAE,UAAU,EACtB,WAAW,GAAE,KAAK,GAAG,MAAM,GAAG,QAAgB,EAC9C,YAAY,UAAQ,EACpB,cAAc,CAAC,EAAE,MAAM,EACvB,aAAa,CAAC,EAAE,MAAM,GACpB;QACF,IAAI,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;QACvE,cAAc,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;KACxE;IA2ED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,gBAAgB,CACtB,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,cAAc,EAC1B,KAAK,EAAE,MAAM,EACb,YAAY,SAAS,EACrB,WAAW,GAAE,KAAK,GAAG,MAAM,GAAG,QAAgB,EAC9C,YAAY,UAAQ,GAClB;QACF,IAAI,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;QACvE,cAAc,EAAE,CAAC,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;KACxE;CAsFD"}
@@ -0,0 +1,283 @@
1
+ import { LOCK_PREFIX as LOCK_PREFIX_HEX, LOCK_SUFFIX as LOCK_SUFFIX_HEX, } from '@1sat/types';
2
+ import { Hash, P2PKH, Script, TransactionSignature, Utils, } from '@bsv/sdk';
3
+ /**
4
+ * Lock PREFIX as number array - sCrypt contract prefix shared with OrdLock template
5
+ */
6
+ export const LOCK_PREFIX = Utils.toArray(LOCK_PREFIX_HEX, 'hex');
7
+ /**
8
+ * Lock SUFFIX as number array - time-lock contract validation script
9
+ */
10
+ export const LOCK_SUFFIX = Utils.toArray(LOCK_SUFFIX_HEX, 'hex');
11
+ /**
12
+ * Finds the index of a subarray within an array
13
+ */
14
+ function indexOf(arr, subArr, fromIndex = 0) {
15
+ for (let i = fromIndex; i <= arr.length - subArr.length; i++) {
16
+ let found = true;
17
+ for (let j = 0; j < subArr.length; j++) {
18
+ if (arr[i + j] !== subArr[j]) {
19
+ found = false;
20
+ break;
21
+ }
22
+ }
23
+ if (found)
24
+ return i;
25
+ }
26
+ return -1;
27
+ }
28
+ /**
29
+ * Checks if an array contains a subarray
30
+ */
31
+ function contains(arr, subArr, fromIndex = 0) {
32
+ return indexOf(arr, subArr, fromIndex) !== -1;
33
+ }
34
+ /**
35
+ * Lock - Time-lock template for locking outputs until a specific block height or timestamp
36
+ *
37
+ * The Lock template enables creating outputs that can only be spent after a certain
38
+ * block height or Unix timestamp has been reached.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Decode a Lock from a script
43
+ * const lock = Lock.decode(script);
44
+ * if (lock) {
45
+ * console.log(`Address: ${lock.address}`);
46
+ * console.log(`Locked until: ${lock.until}`);
47
+ * }
48
+ * ```
49
+ */
50
+ export default class Lock {
51
+ /**
52
+ * Decodes a Lock from a script
53
+ *
54
+ * @param script - The script to decode
55
+ * @param mainnet - Whether to use mainnet address prefix (default: true)
56
+ * @returns Decoded Lock data or null if not found/invalid
57
+ */
58
+ static decode(script, mainnet = true) {
59
+ try {
60
+ const scriptBinary = script.toBinary();
61
+ // Find PREFIX
62
+ const prefixIndex = indexOf(scriptBinary, LOCK_PREFIX);
63
+ if (prefixIndex === -1)
64
+ return null;
65
+ // Check that SUFFIX exists somewhere after PREFIX
66
+ if (!contains(scriptBinary, LOCK_SUFFIX, prefixIndex))
67
+ return null;
68
+ // Extract data starting after PREFIX
69
+ const pos = prefixIndex + LOCK_PREFIX.length;
70
+ // Parse the data as script chunks
71
+ const remainingBytes = scriptBinary.slice(pos);
72
+ const dataScript = Script.fromBinary(remainingBytes);
73
+ const chunks = dataScript.chunks;
74
+ if (chunks.length < 2)
75
+ return null;
76
+ // Chunk 0: address PKHash (20 bytes)
77
+ const addressChunk = chunks[0];
78
+ if (addressChunk?.data == null || addressChunk.data.length !== 20)
79
+ return null;
80
+ // Chunk 1: until value (variable length, little-endian)
81
+ const untilChunk = chunks[1];
82
+ if (untilChunk?.data == null)
83
+ return null;
84
+ // Read little-endian uint32 (pad to 4 bytes if needed)
85
+ const untilData = untilChunk.data;
86
+ const padded = new Uint8Array(4);
87
+ for (let i = 0; i < Math.min(untilData.length, 4); i++) {
88
+ padded[i] = untilData[i];
89
+ }
90
+ const until = padded[0] | (padded[1] << 8) | (padded[2] << 16) | (padded[3] << 24);
91
+ // Convert PKHash to address
92
+ const addressPrefix = mainnet ? [0x00] : [0x6f];
93
+ const address = Utils.toBase58Check(addressChunk.data, addressPrefix);
94
+ return {
95
+ address,
96
+ until: until >>> 0, // Ensure unsigned
97
+ };
98
+ }
99
+ catch {
100
+ return null;
101
+ }
102
+ }
103
+ /**
104
+ * Checks if a script contains a Lock pattern
105
+ *
106
+ * @param script - The script to check
107
+ * @returns true if the script contains Lock pattern
108
+ */
109
+ static isLock(script) {
110
+ const scriptBinary = script.toBinary();
111
+ const prefixIndex = indexOf(scriptBinary, LOCK_PREFIX);
112
+ if (prefixIndex === -1)
113
+ return false;
114
+ return contains(scriptBinary, LOCK_SUFFIX, prefixIndex);
115
+ }
116
+ /**
117
+ * Creates a Lock locking script
118
+ *
119
+ * @param address - The address whose PKH is embedded in the lock
120
+ * @param until - Block height until which the output is locked
121
+ * @returns The locking script
122
+ */
123
+ static lock(address, until) {
124
+ const pkh = Utils.fromBase58Check(address).data;
125
+ return new Script()
126
+ .writeScript(Script.fromBinary(LOCK_PREFIX))
127
+ .writeBin(pkh)
128
+ .writeNumber(until)
129
+ .writeScript(Script.fromBinary(LOCK_SUFFIX));
130
+ }
131
+ /**
132
+ * Creates an unlocking script template for spending a Lock output.
133
+ *
134
+ * @param privateKey - The private key corresponding to the locked address
135
+ * @param sighashType - Sighash type ('all', 'none', 'single')
136
+ * @param anyoneCanPay - Whether SIGHASH_ANYONECANPAY is set
137
+ * @param sourceSatoshis - Satoshis of the source output (optional if sourceTransaction is on the input)
138
+ * @param lockingScript - Locking script of the source output (optional if sourceTransaction is on the input)
139
+ * @returns Object with sign and estimateLength methods
140
+ */
141
+ static unlock(privateKey, sighashType = 'all', anyoneCanPay = false, sourceSatoshis, lockingScript) {
142
+ const unlock = {
143
+ sign: async (tx, inputIndex) => {
144
+ let signatureScope = TransactionSignature.SIGHASH_FORKID;
145
+ if (sighashType === 'all') {
146
+ signatureScope |= TransactionSignature.SIGHASH_ALL;
147
+ }
148
+ if (sighashType === 'none') {
149
+ signatureScope |= TransactionSignature.SIGHASH_NONE;
150
+ }
151
+ if (sighashType === 'single') {
152
+ signatureScope |= TransactionSignature.SIGHASH_SINGLE;
153
+ }
154
+ if (anyoneCanPay) {
155
+ signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY;
156
+ }
157
+ const input = tx.inputs[inputIndex];
158
+ const otherInputs = tx.inputs.filter((_, index) => index !== inputIndex);
159
+ const sourceTXID = input.sourceTXID
160
+ ? input.sourceTXID
161
+ : input.sourceTransaction?.id('hex');
162
+ if (!sourceTXID) {
163
+ throw new Error('The input sourceTXID or sourceTransaction is required for transaction signing.');
164
+ }
165
+ sourceSatoshis ||=
166
+ input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis;
167
+ if (!sourceSatoshis) {
168
+ throw new Error('The sourceSatoshis or input sourceTransaction is required for transaction signing.');
169
+ }
170
+ lockingScript ||=
171
+ input.sourceTransaction?.outputs[input.sourceOutputIndex]
172
+ .lockingScript;
173
+ if (!lockingScript) {
174
+ throw new Error('The lockingScript or input sourceTransaction is required for transaction signing.');
175
+ }
176
+ const preimage = TransactionSignature.format({
177
+ sourceTXID,
178
+ sourceOutputIndex: input.sourceOutputIndex,
179
+ sourceSatoshis,
180
+ transactionVersion: tx.version,
181
+ otherInputs,
182
+ outputs: tx.outputs,
183
+ inputIndex,
184
+ subscript: lockingScript,
185
+ inputSequence: input.sequence === undefined ? 0xffffffff : input.sequence,
186
+ lockTime: tx.lockTime,
187
+ scope: signatureScope,
188
+ });
189
+ const p2pkh = new P2PKH().unlock(privateKey, sighashType, anyoneCanPay, sourceSatoshis, lockingScript);
190
+ return (await p2pkh.sign(tx, inputIndex)).writeBin(preimage);
191
+ },
192
+ estimateLength: async (tx, inputIndex) => {
193
+ return (await unlock.sign(tx, inputIndex)).toBinary().length;
194
+ },
195
+ };
196
+ return unlock;
197
+ }
198
+ /**
199
+ * Creates an unlocking script template using a BRC-100 wallet for signing.
200
+ *
201
+ * Uses wallet.createSignature and wallet.getPublicKey instead of a raw PrivateKey,
202
+ * enabling key derivation via protocolID/keyID.
203
+ *
204
+ * @param wallet - A BRC-100 WalletInterface with createSignature and getPublicKey
205
+ * @param protocolID - The protocol ID for key derivation
206
+ * @param keyID - The key ID for key derivation
207
+ * @param counterparty - The counterparty for key derivation (default: 'self')
208
+ * @param sighashType - Sighash type ('all', 'none', 'single')
209
+ * @param anyoneCanPay - Whether SIGHASH_ANYONECANPAY is set
210
+ * @returns Object with sign and estimateLength methods
211
+ */
212
+ static unlockWithWallet(wallet, protocolID, keyID, counterparty = 'self', sighashType = 'all', anyoneCanPay = false) {
213
+ const unlock = {
214
+ sign: async (tx, inputIndex) => {
215
+ let signatureScope = TransactionSignature.SIGHASH_FORKID;
216
+ if (sighashType === 'all') {
217
+ signatureScope |= TransactionSignature.SIGHASH_ALL;
218
+ }
219
+ if (sighashType === 'none') {
220
+ signatureScope |= TransactionSignature.SIGHASH_NONE;
221
+ }
222
+ if (sighashType === 'single') {
223
+ signatureScope |= TransactionSignature.SIGHASH_SINGLE;
224
+ }
225
+ if (anyoneCanPay) {
226
+ signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY;
227
+ }
228
+ const input = tx.inputs[inputIndex];
229
+ const otherInputs = tx.inputs.filter((_, index) => index !== inputIndex);
230
+ const sourceTXID = input.sourceTXID
231
+ ? input.sourceTXID
232
+ : input.sourceTransaction?.id('hex');
233
+ if (!sourceTXID) {
234
+ throw new Error('The input sourceTXID or sourceTransaction is required for transaction signing.');
235
+ }
236
+ const sourceSatoshis = input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis;
237
+ if (!sourceSatoshis) {
238
+ throw new Error('The sourceSatoshis or input sourceTransaction is required for transaction signing.');
239
+ }
240
+ const lockingScript = input.sourceTransaction?.outputs[input.sourceOutputIndex]
241
+ .lockingScript;
242
+ if (!lockingScript) {
243
+ throw new Error('The lockingScript or input sourceTransaction is required for transaction signing.');
244
+ }
245
+ const preimage = TransactionSignature.format({
246
+ sourceTXID,
247
+ sourceOutputIndex: input.sourceOutputIndex,
248
+ sourceSatoshis,
249
+ transactionVersion: tx.version,
250
+ otherInputs,
251
+ outputs: tx.outputs,
252
+ inputIndex,
253
+ subscript: lockingScript,
254
+ inputSequence: input.sequence === undefined ? 0xffffffff : input.sequence,
255
+ lockTime: tx.lockTime,
256
+ scope: signatureScope,
257
+ });
258
+ const sighash = Hash.sha256(Hash.sha256(preimage));
259
+ const { signature } = await wallet.createSignature({
260
+ protocolID,
261
+ keyID,
262
+ counterparty,
263
+ hashToDirectlySign: Array.from(sighash),
264
+ });
265
+ const { publicKey } = await wallet.getPublicKey({
266
+ protocolID,
267
+ keyID,
268
+ counterparty,
269
+ forSelf: true,
270
+ });
271
+ return new Script()
272
+ .writeBin(signature)
273
+ .writeBin(Utils.toArray(publicKey, 'hex'))
274
+ .writeBin(Array.from(preimage));
275
+ },
276
+ estimateLength: async (tx, inputIndex) => {
277
+ return (await unlock.sign(tx, inputIndex)).toBinary().length;
278
+ },
279
+ };
280
+ return unlock;
281
+ }
282
+ }
283
+ //# sourceMappingUR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AAG,MAAM,MAAM,CAAC,eAAe,CAAC;oBAClD,UAAU;oBACV,KAAK;oBACL,YAAY;oBACZ,kBAAkB,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC;iBACvC,CAAC,CAAA;gBAEF,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC;oBAC/C,UAAU;oBACV,KAAK;oBACL,YAAY;oBACZ,OAAO,EAAE,IAAI;iBACb,CAAC,CAAA;gBAEF,OAAO,IAAI,MAAM,EAAE;qBACjB,QAAQ,CAAC,SAAS,CAAC;qBACnB,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;qBACzC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAA;YACjC,CAAC;YACD,cAAc,EAAE,KAAK,EAAE,EAAe,EAAE,UAAkB,EAAE,EAAE;gBAC7D,OAAO,CAAC,MAAM,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAA;YAC7D,CAAC;SACD,CAAA;QACD,OAAO,MAAM,CAAA;IACd,CAAC;CACD"}
@@ -0,0 +1,126 @@
1
+ import { type PrivateKey, Script, type Transaction, UnlockingScript, type WalletInterface, type WalletProtocol } from '@bsv/sdk';
2
+ /**
3
+ * OrdLock PREFIX as number array - sCrypt contract prefix shared with Lock template
4
+ */
5
+ export declare const ORDLOCK_PREFIX: any[];
6
+ /**
7
+ * OrdLock SUFFIX as number array - contract validation script
8
+ */
9
+ export declare const ORDLOCK_SUFFIX: any[];
10
+ /**
11
+ * OrdLock decoded data structure
12
+ */
13
+ export interface OrdLockData {
14
+ /** Seller's address (base58check encoded) */
15
+ seller: string;
16
+ /** Listing price in satoshis */
17
+ price: bigint;
18
+ /** Raw payout output data */
19
+ payout: number[];
20
+ }
21
+ /**
22
+ * OrdLock - Ordinal Lock template for marketplace listings
23
+ *
24
+ * OrdLock enables trustless ordinal/NFT sales by locking an output with a
25
+ * contract that can only be unlocked by providing payment to a specified address.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * // Decode an OrdLock from a script
30
+ * const ordlock = OrdLock.decode(script);
31
+ * if (ordlock) {
32
+ * console.log(`Seller: ${ordlock.seller}`);
33
+ * console.log(`Price: ${ordlock.price} satoshis`);
34
+ * }
35
+ * ```
36
+ */
37
+ export default class OrdLock {
38
+ /**
39
+ * Decodes an OrdLock from a script
40
+ *
41
+ * @param script - The script to decode
42
+ * @param mainnet - Whether to use mainnet address prefix (default: true)
43
+ * @returns Decoded OrdLock data or null if not found/invalid
44
+ */
45
+ static decode(script: Script, mainnet?: boolean): OrdLockData | null;
46
+ /**
47
+ * Checks if a script contains an OrdLock pattern
48
+ *
49
+ * @param script - The script to check
50
+ * @returns true if the script contains OrdLock pattern
51
+ */
52
+ static isOrdLock(script: Script): boolean;
53
+ /**
54
+ * Checks if an unlocking script indicates a purchase (vs cancellation)
55
+ *
56
+ * @param unlockingScript - The unlocking script to check
57
+ * @returns true if the unlock represents a purchase
58
+ */
59
+ static isPurchase(unlockingScript: Script): boolean;
60
+ /**
61
+ * Creates an OrdLock locking script for listing an ordinal
62
+ *
63
+ * @param cancelAddress - Address that can cancel the listing
64
+ * @param payAddress - Address that receives payment on purchase
65
+ * @param price - Listing price in satoshis
66
+ * @returns The OrdLock locking script
67
+ */
68
+ static lock(cancelAddress: string, payAddress: string, price: number): Script;
69
+ /**
70
+ * Builds a serialized transaction output (satoshis + script)
71
+ *
72
+ * @param satoshis - Output value
73
+ * @param script - Locking script as binary
74
+ * @returns Serialized output bytes
75
+ */
76
+ static buildOutput(satoshis: number, script: number[]): number[];
77
+ /**
78
+ * Creates an unlocking script for cancelling a listing
79
+ *
80
+ * @param privateKey - Private key for the cancel address
81
+ * @param signOutputs - Signature scope for outputs
82
+ * @param anyoneCanPay - Whether to use ANYONECANPAY
83
+ * @param sourceSatoshis - Input satoshis (optional if sourceTransaction provided)
84
+ * @param lockingScript - Input locking script (optional if sourceTransaction provided)
85
+ * @returns Unlock template with sign and estimateLength methods
86
+ */
87
+ static cancelListing(privateKey: PrivateKey, signOutputs?: 'all' | 'none' | 'single', anyoneCanPay?: boolean, sourceSatoshis?: number, lockingScript?: Script): {
88
+ sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
89
+ estimateLength: () => Promise<number>;
90
+ };
91
+ /**
92
+ * Creates an unlocking script for cancelling a listing using a BRC-100 wallet.
93
+ *
94
+ * Uses wallet.createSignature and wallet.getPublicKey instead of a raw PrivateKey,
95
+ * enabling key derivation via protocolID/keyID.
96
+ *
97
+ * @param wallet - A BRC-100 WalletInterface
98
+ * @param protocolID - The protocol ID for key derivation
99
+ * @param keyID - The key ID for key derivation
100
+ * @param counterparty - The counterparty for key derivation (default: 'self')
101
+ * @returns Unlock template with sign and estimateLength methods
102
+ */
103
+ static cancelWithWallet(wallet: WalletInterface, protocolID: WalletProtocol, keyID: string, counterparty?: string): {
104
+ sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
105
+ estimateLength: () => Promise<number>;
106
+ };
107
+ /**
108
+ * Creates an unlocking script for purchasing a listing
109
+ *
110
+ * The purchase path requires:
111
+ * - Output 0: The ordinal going to buyer
112
+ * - Output 1: Payment to seller (must match payout in OrdLock)
113
+ * - Output 2+: Additional outputs (marketplace fees, etc.)
114
+ *
115
+ * No signature is required - the contract validates the outputs match.
116
+ *
117
+ * @param sourceSatoshis - Input satoshis (optional if sourceTransaction provided)
118
+ * @param lockingScript - Input locking script (optional if sourceTransaction provided)
119
+ * @returns Unlock template with sign and estimateLength methods
120
+ */
121
+ static purchaseListing(sourceSatoshis?: number, lockingScript?: Script): {
122
+ sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
123
+ estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>;
124
+ };
125
+ }
126
+ //# sourceMappingURL=ordlock.d.ts.map
@@ -0,0 +1 @@
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