@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.
- package/dist/bitcom/aip.d.ts +88 -0
- package/dist/bitcom/aip.d.ts.map +1 -0
- package/dist/bitcom/aip.js +205 -0
- package/dist/bitcom/aip.js.map +1 -0
- package/dist/bitcom/b.d.ts +99 -0
- package/dist/bitcom/b.d.ts.map +1 -0
- package/dist/bitcom/b.js +206 -0
- package/dist/bitcom/b.js.map +1 -0
- package/dist/bitcom/bap.d.ts +126 -0
- package/dist/bitcom/bap.d.ts.map +1 -0
- package/dist/bitcom/bap.js +334 -0
- package/dist/bitcom/bap.js.map +1 -0
- package/dist/bitcom/bitcom.d.ts +75 -0
- package/dist/bitcom/bitcom.d.ts.map +1 -0
- package/dist/bitcom/bitcom.js +186 -0
- package/dist/bitcom/bitcom.js.map +1 -0
- package/dist/bitcom/map.d.ts +90 -0
- package/dist/bitcom/map.d.ts.map +1 -0
- package/dist/bitcom/map.js +215 -0
- package/dist/bitcom/map.js.map +1 -0
- package/dist/bitcom/sigma.d.ts +93 -0
- package/dist/bitcom/sigma.d.ts.map +1 -0
- package/dist/bitcom/sigma.js +175 -0
- package/dist/bitcom/sigma.js.map +1 -0
- package/dist/bsocial/bsocial.d.ts +152 -0
- package/dist/bsocial/bsocial.d.ts.map +1 -0
- package/dist/bsocial/bsocial.js +397 -0
- package/dist/bsocial/bsocial.js.map +1 -0
- package/dist/bsv20/bsv20.d.ts +277 -0
- package/dist/bsv20/bsv20.d.ts.map +1 -0
- package/dist/bsv20/bsv20.js +544 -0
- package/dist/bsv20/bsv20.js.map +1 -0
- package/dist/bsv21/bsv21.d.ts +231 -0
- package/dist/bsv21/bsv21.d.ts.map +1 -0
- package/dist/bsv21/bsv21.js +475 -0
- package/dist/bsv21/bsv21.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/inscription/inscription.d.ts +165 -0
- package/dist/inscription/inscription.d.ts.map +1 -0
- package/dist/inscription/inscription.js +401 -0
- package/dist/inscription/inscription.js.map +1 -0
- package/dist/lock/lock.d.ts +92 -0
- package/dist/lock/lock.d.ts.map +1 -0
- package/dist/lock/lock.js +283 -0
- package/dist/lock/lock.js.map +1 -0
- package/dist/ordlock/ordlock.d.ts +126 -0
- package/dist/ordlock/ordlock.d.ts.map +1 -0
- package/dist/ordlock/ordlock.js +316 -0
- package/dist/ordlock/ordlock.js.map +1 -0
- package/dist/signer.d.ts +39 -0
- package/dist/signer.d.ts.map +1 -0
- package/dist/signer.js +54 -0
- package/dist/signer.js.map +1 -0
- package/package.json +43 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { LockingScript, Script, type ScriptTemplate, type Transaction, type UnlockingScript } from '@bsv/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing an inscription file
|
|
4
|
+
*/
|
|
5
|
+
export interface InscriptionFile {
|
|
6
|
+
/** SHA256 hash of the content */
|
|
7
|
+
hash: Uint8Array;
|
|
8
|
+
/** Size of the content in bytes */
|
|
9
|
+
size: number;
|
|
10
|
+
/** MIME type of the content */
|
|
11
|
+
type: string;
|
|
12
|
+
/** The actual file content */
|
|
13
|
+
content: Uint8Array;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for inscription creation options
|
|
17
|
+
*/
|
|
18
|
+
export interface InscriptionOptions {
|
|
19
|
+
/** Optional parent inscription reference (36-byte outpoint) */
|
|
20
|
+
parent?: Uint8Array;
|
|
21
|
+
/** Optional script prefix to prepend before inscription (e.g., P2PKH locking script) */
|
|
22
|
+
scriptPrefix?: Script | LockingScript;
|
|
23
|
+
/** Optional script suffix to append after inscription (e.g., OP_RETURN data) */
|
|
24
|
+
scriptSuffix?: Script | LockingScript;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Inscription class implementing ScriptTemplate for Bitcoin ordinals and on-chain data storage.
|
|
28
|
+
*
|
|
29
|
+
* Inscriptions enable embedding arbitrary files, data, and content directly into blockchain
|
|
30
|
+
* transactions using the script pattern: OP_0 OP_IF "ord" OP_1 [MIME_TYPE] OP_0 [CONTENT] OP_ENDIF
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Create inscription from text
|
|
35
|
+
* const inscription = Inscription.fromText("Hello, BSV!", "text/plain");
|
|
36
|
+
* const lockingScript = inscription.lock();
|
|
37
|
+
*
|
|
38
|
+
* // Create inscription from binary data
|
|
39
|
+
* const imageData = new Uint8Array([...]); // PNG image data
|
|
40
|
+
* const imageInscription = Inscription.create(imageData, "image/png");
|
|
41
|
+
*
|
|
42
|
+
* // Parse inscription from script
|
|
43
|
+
* const parsed = Inscription.decode(someScript);
|
|
44
|
+
* if (parsed) {
|
|
45
|
+
* console.log(`Content type: ${parsed.file.type}`);
|
|
46
|
+
* console.log(`Content size: ${parsed.file.size} bytes`);
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export default class Inscription implements ScriptTemplate {
|
|
51
|
+
/** Inscription file data */
|
|
52
|
+
readonly file: InscriptionFile;
|
|
53
|
+
/** Optional parent inscription reference */
|
|
54
|
+
readonly parent?: Uint8Array;
|
|
55
|
+
/** Optional script prefix */
|
|
56
|
+
readonly scriptPrefix?: Script | LockingScript;
|
|
57
|
+
/** Optional script suffix */
|
|
58
|
+
readonly scriptSuffix?: Script | LockingScript;
|
|
59
|
+
/** Unknown/custom fields (field number -> data) */
|
|
60
|
+
readonly fields?: Map<string, Uint8Array>;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new Inscription instance
|
|
63
|
+
*
|
|
64
|
+
* @param file - The inscription file data
|
|
65
|
+
* @param parent - Optional parent inscription reference (32 or 36-byte outpoint)
|
|
66
|
+
* @param scriptPrefix - Optional script prefix (Script or LockingScript)
|
|
67
|
+
* @param scriptSuffix - Optional script suffix (Script or LockingScript)
|
|
68
|
+
* @param fields - Optional map of unknown/custom fields
|
|
69
|
+
*/
|
|
70
|
+
constructor(file: InscriptionFile, parent?: Uint8Array, scriptPrefix?: Script | LockingScript, scriptSuffix?: Script | LockingScript, fields?: Map<string, Uint8Array>);
|
|
71
|
+
/**
|
|
72
|
+
* Creates an inscription locking script following the ordinals protocol format:
|
|
73
|
+
* [ScriptPrefix] OP_0 OP_IF "ord" OP_1 [MIME_TYPE] OP_0 [CONTENT] OP_ENDIF [ScriptSuffix]
|
|
74
|
+
*
|
|
75
|
+
* @returns A locking script containing the inscription data
|
|
76
|
+
*/
|
|
77
|
+
lock(): LockingScript;
|
|
78
|
+
/**
|
|
79
|
+
* Unlock method is not applicable for inscription scripts.
|
|
80
|
+
* Inscriptions are typically used in outputs that don't require unlocking.
|
|
81
|
+
*
|
|
82
|
+
* @throws Error indicating unlock is not supported
|
|
83
|
+
*/
|
|
84
|
+
unlock(): {
|
|
85
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
86
|
+
estimateLength: () => Promise<number>;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Verifies that the file hash matches the content
|
|
90
|
+
*
|
|
91
|
+
* @returns true if the hash is valid, false otherwise
|
|
92
|
+
*/
|
|
93
|
+
verify(): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new inscription from content and MIME type
|
|
96
|
+
*
|
|
97
|
+
* @param content - The file content as bytes
|
|
98
|
+
* @param contentType - The MIME type of the content
|
|
99
|
+
* @param options - Optional inscription parameters
|
|
100
|
+
* @returns A new Inscription instance
|
|
101
|
+
*/
|
|
102
|
+
static create(content: Uint8Array, contentType: string, options?: InscriptionOptions): Inscription;
|
|
103
|
+
/**
|
|
104
|
+
* Creates an inscription from text content
|
|
105
|
+
*
|
|
106
|
+
* @param text - The text content
|
|
107
|
+
* @param contentType - Optional MIME type (defaults to "text/plain;charset=utf-8")
|
|
108
|
+
* @param options - Optional inscription parameters
|
|
109
|
+
* @returns A new Inscription instance
|
|
110
|
+
*/
|
|
111
|
+
static fromText(text: string, contentType?: string, options?: InscriptionOptions): Inscription;
|
|
112
|
+
/**
|
|
113
|
+
* Creates an inscription from a browser File object
|
|
114
|
+
*
|
|
115
|
+
* @param file - The File object
|
|
116
|
+
* @param options - Optional inscription parameters
|
|
117
|
+
* @returns Promise resolving to a new Inscription instance
|
|
118
|
+
*/
|
|
119
|
+
static fromFile(file: File, options?: InscriptionOptions): Promise<Inscription>;
|
|
120
|
+
/**
|
|
121
|
+
* Checks if a script contains an inscription
|
|
122
|
+
*
|
|
123
|
+
* @param script - The script to check
|
|
124
|
+
* @returns true if the script contains an inscription pattern
|
|
125
|
+
*/
|
|
126
|
+
static isInscription(script: Script): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Decodes an inscription from a script
|
|
129
|
+
*
|
|
130
|
+
* @param script - The script containing inscription data
|
|
131
|
+
* @returns Decoded inscription or null if not found/invalid
|
|
132
|
+
*/
|
|
133
|
+
static decode(script: Script): Inscription | null;
|
|
134
|
+
/**
|
|
135
|
+
* Extracts text content from the inscription if it's a text type
|
|
136
|
+
*
|
|
137
|
+
* @returns The text content or null if not text or invalid UTF-8
|
|
138
|
+
*/
|
|
139
|
+
getText(): string | null;
|
|
140
|
+
/**
|
|
141
|
+
* Parses JSON content from the inscription
|
|
142
|
+
*
|
|
143
|
+
* @returns The parsed JSON object or null if not JSON or invalid
|
|
144
|
+
*/
|
|
145
|
+
getJSON<T = unknown>(): T | null;
|
|
146
|
+
/**
|
|
147
|
+
* Gets the raw content as bytes
|
|
148
|
+
*
|
|
149
|
+
* @returns The file content
|
|
150
|
+
*/
|
|
151
|
+
getContent(): Uint8Array;
|
|
152
|
+
/**
|
|
153
|
+
* Gets content as base64 string
|
|
154
|
+
*
|
|
155
|
+
* @returns Base64 encoded content
|
|
156
|
+
*/
|
|
157
|
+
getBase64(): string;
|
|
158
|
+
/**
|
|
159
|
+
* Gets content as hex string
|
|
160
|
+
*
|
|
161
|
+
* @returns Hex encoded content
|
|
162
|
+
*/
|
|
163
|
+
getHex(): string;
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=inscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inscription.d.ts","sourceRoot":"","sources":["../../src/inscription/inscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,aAAa,EAEb,MAAM,EACN,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,eAAe,EAEpB,MAAM,UAAU,CAAA;AAEjB;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B,iCAAiC;IACjC,IAAI,EAAE,UAAU,CAAA;IAChB,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAA;IACZ,+BAA+B;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,8BAA8B;IAC9B,OAAO,EAAE,UAAU,CAAA;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC,+DAA+D;IAC/D,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,wFAAwF;IACxF,YAAY,CAAC,EAAE,MAAM,GAAG,aAAa,CAAA;IACrC,gFAAgF;IAChF,YAAY,CAAC,EAAE,MAAM,GAAG,aAAa,CAAA;CACrC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,OAAO,WAAY,YAAW,cAAc;IACzD,4BAA4B;IAC5B,SAAgB,IAAI,EAAE,eAAe,CAAA;IACrC,4CAA4C;IAC5C,SAAgB,MAAM,CAAC,EAAE,UAAU,CAAA;IACnC,6BAA6B;IAC7B,SAAgB,YAAY,CAAC,EAAE,MAAM,GAAG,aAAa,CAAA;IACrD,6BAA6B;IAC7B,SAAgB,YAAY,CAAC,EAAE,MAAM,GAAG,aAAa,CAAA;IACrD,mDAAmD;IACnD,SAAgB,MAAM,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IAEhD;;;;;;;;O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AIG;IACH,MAAM,IAAI,MAAM;CAGhB"}
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { Hash, LockingScript, OP, Script, Utils, } from '@bsv/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Inscription class implementing ScriptTemplate for Bitcoin ordinals and on-chain data storage.
|
|
4
|
+
*
|
|
5
|
+
* Inscriptions enable embedding arbitrary files, data, and content directly into blockchain
|
|
6
|
+
* transactions using the script pattern: OP_0 OP_IF "ord" OP_1 [MIME_TYPE] OP_0 [CONTENT] OP_ENDIF
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // Create inscription from text
|
|
11
|
+
* const inscription = Inscription.fromText("Hello, BSV!", "text/plain");
|
|
12
|
+
* const lockingScript = inscription.lock();
|
|
13
|
+
*
|
|
14
|
+
* // Create inscription from binary data
|
|
15
|
+
* const imageData = new Uint8Array([...]); // PNG image data
|
|
16
|
+
* const imageInscription = Inscription.create(imageData, "image/png");
|
|
17
|
+
*
|
|
18
|
+
* // Parse inscription from script
|
|
19
|
+
* const parsed = Inscription.decode(someScript);
|
|
20
|
+
* if (parsed) {
|
|
21
|
+
* console.log(`Content type: ${parsed.file.type}`);
|
|
22
|
+
* console.log(`Content size: ${parsed.file.size} bytes`);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export default class Inscription {
|
|
27
|
+
/** Inscription file data */
|
|
28
|
+
file;
|
|
29
|
+
/** Optional parent inscription reference */
|
|
30
|
+
parent;
|
|
31
|
+
/** Optional script prefix */
|
|
32
|
+
scriptPrefix;
|
|
33
|
+
/** Optional script suffix */
|
|
34
|
+
scriptSuffix;
|
|
35
|
+
/** Unknown/custom fields (field number -> data) */
|
|
36
|
+
fields;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new Inscription instance
|
|
39
|
+
*
|
|
40
|
+
* @param file - The inscription file data
|
|
41
|
+
* @param parent - Optional parent inscription reference (32 or 36-byte outpoint)
|
|
42
|
+
* @param scriptPrefix - Optional script prefix (Script or LockingScript)
|
|
43
|
+
* @param scriptSuffix - Optional script suffix (Script or LockingScript)
|
|
44
|
+
* @param fields - Optional map of unknown/custom fields
|
|
45
|
+
*/
|
|
46
|
+
constructor(file, parent, scriptPrefix, scriptSuffix, fields) {
|
|
47
|
+
this.file = file;
|
|
48
|
+
this.parent = parent;
|
|
49
|
+
this.scriptPrefix = scriptPrefix;
|
|
50
|
+
this.scriptSuffix = scriptSuffix;
|
|
51
|
+
this.fields = fields;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates an inscription locking script following the ordinals protocol format:
|
|
55
|
+
* [ScriptPrefix] OP_0 OP_IF "ord" OP_1 [MIME_TYPE] OP_0 [CONTENT] OP_ENDIF [ScriptSuffix]
|
|
56
|
+
*
|
|
57
|
+
* @returns A locking script containing the inscription data
|
|
58
|
+
*/
|
|
59
|
+
lock() {
|
|
60
|
+
const script = new Script();
|
|
61
|
+
// Add script prefix if present - iterate over chunks to preserve data pushes
|
|
62
|
+
if (this.scriptPrefix != null) {
|
|
63
|
+
for (const chunk of this.scriptPrefix.chunks) {
|
|
64
|
+
script.chunks.push(chunk);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Start inscription envelope: OP_0 OP_IF
|
|
68
|
+
script.writeOpCode(OP.OP_0);
|
|
69
|
+
script.writeOpCode(OP.OP_IF);
|
|
70
|
+
// Protocol identifier: "ord"
|
|
71
|
+
script.writeBin(Utils.toArray('ord', 'utf8'));
|
|
72
|
+
// Add MIME type (field 1)
|
|
73
|
+
script.writeOpCode(OP.OP_1);
|
|
74
|
+
script.writeBin(Utils.toArray(this.file.type, 'utf8'));
|
|
75
|
+
// Add parent if present (field 3)
|
|
76
|
+
if (this.parent != null && this.parent.length === 36) {
|
|
77
|
+
script.writeOpCode(OP.OP_3);
|
|
78
|
+
script.writeBin(Array.from(this.parent));
|
|
79
|
+
}
|
|
80
|
+
// Add content (field 0)
|
|
81
|
+
script.writeOpCode(OP.OP_0);
|
|
82
|
+
script.writeBin(Array.from(this.file.content));
|
|
83
|
+
// End inscription envelope: OP_ENDIF
|
|
84
|
+
script.writeOpCode(OP.OP_ENDIF);
|
|
85
|
+
// Add script suffix if present - iterate over chunks to preserve data pushes
|
|
86
|
+
if (this.scriptSuffix != null) {
|
|
87
|
+
for (const chunk of this.scriptSuffix.chunks) {
|
|
88
|
+
script.chunks.push(chunk);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return new LockingScript(script.chunks);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Unlock method is not applicable for inscription scripts.
|
|
95
|
+
* Inscriptions are typically used in outputs that don't require unlocking.
|
|
96
|
+
*
|
|
97
|
+
* @throws Error indicating unlock is not supported
|
|
98
|
+
*/
|
|
99
|
+
unlock() {
|
|
100
|
+
throw new Error('Unlock is not supported for inscription scripts');
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Verifies that the file hash matches the content
|
|
104
|
+
*
|
|
105
|
+
* @returns true if the hash is valid, false otherwise
|
|
106
|
+
*/
|
|
107
|
+
verify() {
|
|
108
|
+
const calculatedHash = Hash.sha256(Array.from(this.file.content));
|
|
109
|
+
return (Utils.toHex(calculatedHash) === Utils.toHex(Array.from(this.file.hash)));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Creates a new inscription from content and MIME type
|
|
113
|
+
*
|
|
114
|
+
* @param content - The file content as bytes
|
|
115
|
+
* @param contentType - The MIME type of the content
|
|
116
|
+
* @param options - Optional inscription parameters
|
|
117
|
+
* @returns A new Inscription instance
|
|
118
|
+
*/
|
|
119
|
+
static create(content, contentType, options = {}) {
|
|
120
|
+
// Validate MIME type
|
|
121
|
+
if (contentType === '' || contentType.length > 255) {
|
|
122
|
+
throw new Error('Content type must be a non-empty string with max 255 characters');
|
|
123
|
+
}
|
|
124
|
+
// Validate UTF-8 encoding for content type
|
|
125
|
+
try {
|
|
126
|
+
Utils.toArray(contentType, 'utf8');
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
throw new Error('Content type must be valid UTF-8');
|
|
130
|
+
}
|
|
131
|
+
// Validate parent if provided
|
|
132
|
+
if (options.parent != null && options.parent.length !== 36) {
|
|
133
|
+
throw new Error('Parent must be exactly 36 bytes (transaction outpoint)');
|
|
134
|
+
}
|
|
135
|
+
// Calculate hash and create file structure
|
|
136
|
+
const hash = new Uint8Array(Hash.sha256(Array.from(content)));
|
|
137
|
+
const file = {
|
|
138
|
+
hash,
|
|
139
|
+
size: content.length,
|
|
140
|
+
type: contentType,
|
|
141
|
+
content,
|
|
142
|
+
};
|
|
143
|
+
return new Inscription(file, options.parent, options.scriptPrefix, options.scriptSuffix);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates an inscription from text content
|
|
147
|
+
*
|
|
148
|
+
* @param text - The text content
|
|
149
|
+
* @param contentType - Optional MIME type (defaults to "text/plain;charset=utf-8")
|
|
150
|
+
* @param options - Optional inscription parameters
|
|
151
|
+
* @returns A new Inscription instance
|
|
152
|
+
*/
|
|
153
|
+
static fromText(text, contentType = 'text/plain;charset=utf-8', options = {}) {
|
|
154
|
+
const content = new Uint8Array(Utils.toArray(text, 'utf8'));
|
|
155
|
+
return Inscription.create(content, contentType, options);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates an inscription from a browser File object
|
|
159
|
+
*
|
|
160
|
+
* @param file - The File object
|
|
161
|
+
* @param options - Optional inscription parameters
|
|
162
|
+
* @returns Promise resolving to a new Inscription instance
|
|
163
|
+
*/
|
|
164
|
+
static async fromFile(file, options = {}) {
|
|
165
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
166
|
+
const content = new Uint8Array(arrayBuffer);
|
|
167
|
+
const contentType = file.type !== '' ? file.type : 'application/octet-stream';
|
|
168
|
+
return Inscription.create(content, contentType, options);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Checks if a script contains an inscription
|
|
172
|
+
*
|
|
173
|
+
* @param script - The script to check
|
|
174
|
+
* @returns true if the script contains an inscription pattern
|
|
175
|
+
*/
|
|
176
|
+
static isInscription(script) {
|
|
177
|
+
try {
|
|
178
|
+
const chunks = script.chunks;
|
|
179
|
+
// Look for the pattern: OP_0 OP_IF "ord"
|
|
180
|
+
for (let i = 0; i < chunks.length - 2; i++) {
|
|
181
|
+
const chunk0 = chunks[i];
|
|
182
|
+
const chunk1 = chunks[i + 1];
|
|
183
|
+
const chunk2 = chunks[i + 2];
|
|
184
|
+
if (chunk0?.op === OP.OP_0 &&
|
|
185
|
+
chunk1?.op === OP.OP_IF &&
|
|
186
|
+
chunk2?.data != null &&
|
|
187
|
+
chunk2.data.length === 3 &&
|
|
188
|
+
Utils.toUTF8(chunk2.data) === 'ord') {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Decodes an inscription from a script
|
|
200
|
+
*
|
|
201
|
+
* @param script - The script containing inscription data
|
|
202
|
+
* @returns Decoded inscription or null if not found/invalid
|
|
203
|
+
*/
|
|
204
|
+
static decode(script) {
|
|
205
|
+
try {
|
|
206
|
+
const chunks = script.chunks;
|
|
207
|
+
// Find inscription pattern: OP_0 OP_IF "ord"
|
|
208
|
+
let inscriptionStart = -1;
|
|
209
|
+
for (let i = 0; i < chunks.length - 2; i++) {
|
|
210
|
+
const chunk0 = chunks[i];
|
|
211
|
+
const chunk1 = chunks[i + 1];
|
|
212
|
+
const chunk2 = chunks[i + 2];
|
|
213
|
+
if (chunk0?.op === OP.OP_0 &&
|
|
214
|
+
chunk1?.op === OP.OP_IF &&
|
|
215
|
+
chunk2?.data != null &&
|
|
216
|
+
chunk2.data.length === 3 &&
|
|
217
|
+
Utils.toUTF8(chunk2.data) === 'ord') {
|
|
218
|
+
inscriptionStart = i;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (inscriptionStart === -1) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
// Extract prefix (everything before inscription) - preserve as Script with proper chunks
|
|
226
|
+
let scriptPrefix;
|
|
227
|
+
if (inscriptionStart > 0) {
|
|
228
|
+
scriptPrefix = new Script();
|
|
229
|
+
for (let i = 0; i < inscriptionStart; i++) {
|
|
230
|
+
scriptPrefix.chunks.push(chunks[i]);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// Parse inscription fields
|
|
234
|
+
let pos = inscriptionStart + 3; // Skip OP_0 OP_IF "ord"
|
|
235
|
+
let contentType = '';
|
|
236
|
+
let content = new Uint8Array(0);
|
|
237
|
+
let parent;
|
|
238
|
+
const fields = new Map();
|
|
239
|
+
while (pos < chunks.length) {
|
|
240
|
+
if (chunks[pos].op === OP.OP_ENDIF) {
|
|
241
|
+
pos++;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
// Read field number/key
|
|
245
|
+
const fieldChunk = chunks[pos];
|
|
246
|
+
let fieldNum;
|
|
247
|
+
let fieldKey;
|
|
248
|
+
// Handle OP codes for field numbers
|
|
249
|
+
if (fieldChunk.op === OP.OP_0) {
|
|
250
|
+
fieldNum = 0;
|
|
251
|
+
}
|
|
252
|
+
else if (fieldChunk.op !== undefined &&
|
|
253
|
+
fieldChunk.op > OP.OP_PUSHDATA4 &&
|
|
254
|
+
fieldChunk.op <= OP.OP_16) {
|
|
255
|
+
fieldNum = fieldChunk.op - 80; // OP_1 = 81, so OP_1 - 80 = 1
|
|
256
|
+
}
|
|
257
|
+
else if (fieldChunk.data != null && fieldChunk.data.length === 1) {
|
|
258
|
+
// Single byte data as field number
|
|
259
|
+
fieldNum = fieldChunk.data[0];
|
|
260
|
+
}
|
|
261
|
+
else if (fieldChunk.data != null && fieldChunk.data.length > 1) {
|
|
262
|
+
// Multi-byte data as string key (like Go implementation)
|
|
263
|
+
fieldKey = String.fromCharCode(...fieldChunk.data);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
// Unknown field format, skip
|
|
267
|
+
pos++;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
pos++;
|
|
271
|
+
// Read data
|
|
272
|
+
const dataChunk = chunks[pos];
|
|
273
|
+
if (pos >= chunks.length) {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
// Data chunk may be empty or have data
|
|
277
|
+
const data = dataChunk?.data != null
|
|
278
|
+
? new Uint8Array(dataChunk.data)
|
|
279
|
+
: new Uint8Array(0);
|
|
280
|
+
pos++;
|
|
281
|
+
// Process field based on number or key
|
|
282
|
+
if (fieldKey != null) {
|
|
283
|
+
// String key field - store in fields map
|
|
284
|
+
fields.set(fieldKey, data);
|
|
285
|
+
}
|
|
286
|
+
else if (fieldNum !== undefined) {
|
|
287
|
+
switch (fieldNum) {
|
|
288
|
+
case 0: // Content
|
|
289
|
+
content = data;
|
|
290
|
+
break;
|
|
291
|
+
case 1: // MIME type
|
|
292
|
+
try {
|
|
293
|
+
contentType = Utils.toUTF8(Array.from(data));
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
// Invalid UTF-8, use empty string
|
|
297
|
+
contentType = '';
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
case 3: // Parent - accept both 32 and 36 bytes (like Go)
|
|
301
|
+
if (data.length === 36) {
|
|
302
|
+
parent = data;
|
|
303
|
+
}
|
|
304
|
+
else if (data.length === 32) {
|
|
305
|
+
// 32-byte txid only - create 36-byte outpoint with vout=0
|
|
306
|
+
parent = new Uint8Array(36);
|
|
307
|
+
parent.set(data, 0);
|
|
308
|
+
// Last 4 bytes are already 0 (vout = 0)
|
|
309
|
+
}
|
|
310
|
+
break;
|
|
311
|
+
default:
|
|
312
|
+
// Store unknown numeric fields in fields map
|
|
313
|
+
fields.set(fieldNum.toString(), data);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// Extract suffix (everything after OP_ENDIF) - preserve as Script with proper chunks
|
|
318
|
+
let scriptSuffix;
|
|
319
|
+
if (pos < chunks.length) {
|
|
320
|
+
scriptSuffix = new Script();
|
|
321
|
+
for (let i = pos; i < chunks.length; i++) {
|
|
322
|
+
scriptSuffix.chunks.push(chunks[i]);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Must have content to be valid
|
|
326
|
+
if (content.length === 0) {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
// Calculate hash
|
|
330
|
+
const hash = new Uint8Array(Hash.sha256(Array.from(content)));
|
|
331
|
+
const file = {
|
|
332
|
+
hash,
|
|
333
|
+
size: content.length,
|
|
334
|
+
type: contentType,
|
|
335
|
+
content,
|
|
336
|
+
};
|
|
337
|
+
return new Inscription(file, parent, scriptPrefix, scriptSuffix, fields.size > 0 ? fields : undefined);
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Extracts text content from the inscription if it's a text type
|
|
345
|
+
*
|
|
346
|
+
* @returns The text content or null if not text or invalid UTF-8
|
|
347
|
+
*/
|
|
348
|
+
getText() {
|
|
349
|
+
if (!this.file.type.startsWith('text/') &&
|
|
350
|
+
this.file.type !== 'application/json') {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
try {
|
|
354
|
+
return Utils.toUTF8(Array.from(this.file.content));
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Parses JSON content from the inscription
|
|
362
|
+
*
|
|
363
|
+
* @returns The parsed JSON object or null if not JSON or invalid
|
|
364
|
+
*/
|
|
365
|
+
getJSON() {
|
|
366
|
+
const text = this.getText();
|
|
367
|
+
if (text == null)
|
|
368
|
+
return null;
|
|
369
|
+
try {
|
|
370
|
+
return JSON.parse(text);
|
|
371
|
+
}
|
|
372
|
+
catch {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Gets the raw content as bytes
|
|
378
|
+
*
|
|
379
|
+
* @returns The file content
|
|
380
|
+
*/
|
|
381
|
+
getContent() {
|
|
382
|
+
return this.file.content;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Gets content as base64 string
|
|
386
|
+
*
|
|
387
|
+
* @returns Base64 encoded content
|
|
388
|
+
*/
|
|
389
|
+
getBase64() {
|
|
390
|
+
return Utils.toBase64(Array.from(this.file.content));
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Gets content as hex string
|
|
394
|
+
*
|
|
395
|
+
* @returns Hex encoded content
|
|
396
|
+
*/
|
|
397
|
+
getHex() {
|
|
398
|
+
return Utils.toHex(Array.from(this.file.content));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
//# sourceMappingURL=inscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inscription.js","sourceRoot":"","sources":["../../src/inscription/inscription.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,IAAI,EACJ,aAAa,EACb,EAAE,EACF,MAAM,EAIN,KAAK,GACL,MAAM,UAAU,CAAA;AA4BjB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAC/B,4BAA4B;IACZ,IAAI,CAAiB;IACrC,4CAA4C;IAC5B,MAAM,CAAa;IACnC,6BAA6B;IACb,YAAY,CAAyB;IACrD,6BAA6B;IACb,YAAY,CAAyB;IACrD,mDAAmD;IACnC,MAAM,CAA0B;IAEhD;;;;;;;;OAQG;IACH,YACC,IAAqB,EACrB,MAAmB,EACnB,YAAqC,EACrC,YAAqC,EACrC,MAAgC;QAEhC,IAAI,CAAC,IAAI,GAAG,IAA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AAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;YACnC,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,kBAAkB,EACpC,CAAC;YACF,OAAO,IAAI,CAAA;QACZ,CAAC;QAED,IAAI,CAAC;YACJ,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QACnD,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,IAAI,CAAA;QACZ,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,OAAO;QACN,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;QAC3B,IAAI,IAAI,IAAI,IAAI;YAAE,OAAO,IAAI,CAAA;QAE7B,IAAI,CAAC;YACJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAA;QAC7B,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,IAAI,CAAA;QACZ,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,UAAU;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAA;IACzB,CAAC;IAED;;;;OAIG;IACH,SAAS;QACR,OAAO,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;IACrD,CAAC;IAED;;;;OAIG;IACH,MAAM;QACL,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;IAClD,CAAC;CACD"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type PrivateKey, Script, type Transaction, type UnlockingScript, type WalletInterface, type WalletProtocol } from '@bsv/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Lock PREFIX as number array - sCrypt contract prefix shared with OrdLock template
|
|
4
|
+
*/
|
|
5
|
+
export declare const LOCK_PREFIX: any[];
|
|
6
|
+
/**
|
|
7
|
+
* Lock SUFFIX as number array - time-lock contract validation script
|
|
8
|
+
*/
|
|
9
|
+
export declare const LOCK_SUFFIX: any[];
|
|
10
|
+
/**
|
|
11
|
+
* Lock decoded data structure
|
|
12
|
+
*/
|
|
13
|
+
export interface LockData {
|
|
14
|
+
/** Owner's address (base58check encoded) */
|
|
15
|
+
address: string;
|
|
16
|
+
/** Block height or timestamp until which the output is locked */
|
|
17
|
+
until: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Lock - Time-lock template for locking outputs until a specific block height or timestamp
|
|
21
|
+
*
|
|
22
|
+
* The Lock template enables creating outputs that can only be spent after a certain
|
|
23
|
+
* block height or Unix timestamp has been reached.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Decode a Lock from a script
|
|
28
|
+
* const lock = Lock.decode(script);
|
|
29
|
+
* if (lock) {
|
|
30
|
+
* console.log(`Address: ${lock.address}`);
|
|
31
|
+
* console.log(`Locked until: ${lock.until}`);
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export default class Lock {
|
|
36
|
+
/**
|
|
37
|
+
* Decodes a Lock from a script
|
|
38
|
+
*
|
|
39
|
+
* @param script - The script to decode
|
|
40
|
+
* @param mainnet - Whether to use mainnet address prefix (default: true)
|
|
41
|
+
* @returns Decoded Lock data or null if not found/invalid
|
|
42
|
+
*/
|
|
43
|
+
static decode(script: Script, mainnet?: boolean): LockData | null;
|
|
44
|
+
/**
|
|
45
|
+
* Checks if a script contains a Lock pattern
|
|
46
|
+
*
|
|
47
|
+
* @param script - The script to check
|
|
48
|
+
* @returns true if the script contains Lock pattern
|
|
49
|
+
*/
|
|
50
|
+
static isLock(script: Script): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a Lock locking script
|
|
53
|
+
*
|
|
54
|
+
* @param address - The address whose PKH is embedded in the lock
|
|
55
|
+
* @param until - Block height until which the output is locked
|
|
56
|
+
* @returns The locking script
|
|
57
|
+
*/
|
|
58
|
+
static lock(address: string, until: number): Script;
|
|
59
|
+
/**
|
|
60
|
+
* Creates an unlocking script template for spending a Lock output.
|
|
61
|
+
*
|
|
62
|
+
* @param privateKey - The private key corresponding to the locked address
|
|
63
|
+
* @param sighashType - Sighash type ('all', 'none', 'single')
|
|
64
|
+
* @param anyoneCanPay - Whether SIGHASH_ANYONECANPAY is set
|
|
65
|
+
* @param sourceSatoshis - Satoshis of the source output (optional if sourceTransaction is on the input)
|
|
66
|
+
* @param lockingScript - Locking script of the source output (optional if sourceTransaction is on the input)
|
|
67
|
+
* @returns Object with sign and estimateLength methods
|
|
68
|
+
*/
|
|
69
|
+
static unlock(privateKey: PrivateKey, sighashType?: 'all' | 'none' | 'single', anyoneCanPay?: boolean, sourceSatoshis?: number, lockingScript?: Script): {
|
|
70
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
71
|
+
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Creates an unlocking script template using a BRC-100 wallet for signing.
|
|
75
|
+
*
|
|
76
|
+
* Uses wallet.createSignature and wallet.getPublicKey instead of a raw PrivateKey,
|
|
77
|
+
* enabling key derivation via protocolID/keyID.
|
|
78
|
+
*
|
|
79
|
+
* @param wallet - A BRC-100 WalletInterface with createSignature and getPublicKey
|
|
80
|
+
* @param protocolID - The protocol ID for key derivation
|
|
81
|
+
* @param keyID - The key ID for key derivation
|
|
82
|
+
* @param counterparty - The counterparty for key derivation (default: 'self')
|
|
83
|
+
* @param sighashType - Sighash type ('all', 'none', 'single')
|
|
84
|
+
* @param anyoneCanPay - Whether SIGHASH_ANYONECANPAY is set
|
|
85
|
+
* @returns Object with sign and estimateLength methods
|
|
86
|
+
*/
|
|
87
|
+
static unlockWithWallet(wallet: WalletInterface, protocolID: WalletProtocol, keyID: string, counterparty?: string, sighashType?: 'all' | 'none' | 'single', anyoneCanPay?: boolean): {
|
|
88
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
89
|
+
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=lock.d.ts.map
|