@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,186 @@
|
|
|
1
|
+
import { LockingScript, OP, Script, Utils } from '@bsv/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* BitCom template for handling multi-protocol Bitcoin transactions
|
|
4
|
+
*
|
|
5
|
+
* BitCom allows multiple protocols to be embedded in a single transaction output
|
|
6
|
+
* using the pipe ('|') delimiter to separate different protocol sections.
|
|
7
|
+
*
|
|
8
|
+
* This implementation mirrors the Go template structure and behavior.
|
|
9
|
+
*/
|
|
10
|
+
export default class BitCom {
|
|
11
|
+
protocols;
|
|
12
|
+
scriptPrefix;
|
|
13
|
+
constructor(protocols = [], scriptPrefix = []) {
|
|
14
|
+
this.protocols = protocols;
|
|
15
|
+
this.scriptPrefix = scriptPrefix;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Decodes a script to extract BitCom protocol data
|
|
19
|
+
*
|
|
20
|
+
* Mirrors the Go implementation's decode algorithm exactly.
|
|
21
|
+
*
|
|
22
|
+
* @param script - The script to decode
|
|
23
|
+
* @returns BitComDecoded - The decoded BitCom structure, or null if no OP_RETURN found
|
|
24
|
+
*/
|
|
25
|
+
static decode(script) {
|
|
26
|
+
// Handle null script safely - return empty structure like Go
|
|
27
|
+
if (script == null) {
|
|
28
|
+
return {
|
|
29
|
+
protocols: [],
|
|
30
|
+
scriptPrefix: [],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
let scriptBytes;
|
|
34
|
+
if (script instanceof LockingScript) {
|
|
35
|
+
scriptBytes = script.toBinary();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
scriptBytes = script.toBinary();
|
|
39
|
+
}
|
|
40
|
+
const pos = BitCom.findReturn(scriptBytes, 0);
|
|
41
|
+
if (pos === -1) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
// Get prefix if present (matches Go: prefix = (*scr)[:pos])
|
|
45
|
+
let prefix = [];
|
|
46
|
+
if (pos > 0) {
|
|
47
|
+
prefix = scriptBytes.slice(0, pos);
|
|
48
|
+
}
|
|
49
|
+
const bitcom = {
|
|
50
|
+
scriptPrefix: prefix,
|
|
51
|
+
protocols: [],
|
|
52
|
+
};
|
|
53
|
+
// Start after OP_RETURN (matches Go: pos++)
|
|
54
|
+
const currentPos = pos + 1;
|
|
55
|
+
// Parse protocols using SDK's built-in parsing (no custom readOp functions)
|
|
56
|
+
const remainingScript = Script.fromBinary(scriptBytes.slice(currentPos));
|
|
57
|
+
const chunks = remainingScript.chunks;
|
|
58
|
+
let chunkIndex = 0;
|
|
59
|
+
let currentProtocolPos = currentPos;
|
|
60
|
+
while (chunkIndex < chunks.length) {
|
|
61
|
+
const protocol = {
|
|
62
|
+
protocol: '',
|
|
63
|
+
script: [],
|
|
64
|
+
pos: currentProtocolPos,
|
|
65
|
+
};
|
|
66
|
+
// Read protocol identifier from current chunk
|
|
67
|
+
const protocolChunk = chunks[chunkIndex];
|
|
68
|
+
if (protocolChunk.data == null) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
protocol.protocol = Utils.toUTF8(protocolChunk.data);
|
|
72
|
+
chunkIndex++;
|
|
73
|
+
// Collect script chunks until we find a pipe delimiter or reach end
|
|
74
|
+
const protocolChunks = [];
|
|
75
|
+
while (chunkIndex < chunks.length) {
|
|
76
|
+
const chunk = chunks[chunkIndex];
|
|
77
|
+
// Check if this chunk is a pipe delimiter
|
|
78
|
+
if (chunk.data != null &&
|
|
79
|
+
chunk.data.length === 1 &&
|
|
80
|
+
chunk.data[0] === 0x7c) {
|
|
81
|
+
// Found pipe delimiter - end current protocol
|
|
82
|
+
chunkIndex++; // Skip the pipe
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
// Add chunk to current protocol (preserve chunk structure)
|
|
86
|
+
protocolChunks.push(chunk);
|
|
87
|
+
chunkIndex++;
|
|
88
|
+
}
|
|
89
|
+
// Convert chunks back to binary for protocol.script
|
|
90
|
+
const protocolScript = new Script(protocolChunks);
|
|
91
|
+
protocol.script = protocolScript.toBinary();
|
|
92
|
+
bitcom.protocols.push(protocol);
|
|
93
|
+
// Update position for next protocol
|
|
94
|
+
currentProtocolPos +=
|
|
95
|
+
protocolChunk.op +
|
|
96
|
+
1 +
|
|
97
|
+
protocol.script.length +
|
|
98
|
+
(chunkIndex < chunks.length ? 2 : 0); // +2 for pipe delimiter
|
|
99
|
+
}
|
|
100
|
+
return bitcom;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a locking script from the BitCom structure
|
|
104
|
+
*
|
|
105
|
+
* Mirrors the Go Lock() method exactly.
|
|
106
|
+
*
|
|
107
|
+
* @returns LockingScript - The BitCom locking script
|
|
108
|
+
*/
|
|
109
|
+
lock() {
|
|
110
|
+
const script = new Script();
|
|
111
|
+
// Add prefix if present (matches Go: s := script.NewFromBytes(b.ScriptPrefix))
|
|
112
|
+
if (this.scriptPrefix.length > 0) {
|
|
113
|
+
script.writeBin(this.scriptPrefix);
|
|
114
|
+
}
|
|
115
|
+
if (this.protocols.length > 0) {
|
|
116
|
+
// Add OP_RETURN (matches Go: _ = s.AppendOpcodes(script.OpRETURN))
|
|
117
|
+
script.writeOpCode(OP.OP_RETURN);
|
|
118
|
+
for (let i = 0; i < this.protocols.length; i++) {
|
|
119
|
+
const protocol = this.protocols[i];
|
|
120
|
+
// Add protocol identifier as push data (matches Go: _ = s.AppendPushData([]byte(p.Protocol)))
|
|
121
|
+
const protocolBytes = Utils.toArray(protocol.protocol, 'utf8');
|
|
122
|
+
script.writeBin(protocolBytes);
|
|
123
|
+
// Add protocol script data directly (matches Go: s = script.NewFromBytes(append(*s, p.Script...)))
|
|
124
|
+
if (protocol.script.length > 0) {
|
|
125
|
+
// Parse the protocol script to get its chunks and append them
|
|
126
|
+
const protocolScript = Script.fromBinary(protocol.script);
|
|
127
|
+
for (const chunk of protocolScript.chunks) {
|
|
128
|
+
if (chunk.data != null) {
|
|
129
|
+
script.writeBin(chunk.data);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
script.writeOpCode(chunk.op);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Add pipe delimiter if not the last protocol (matches Go: _ = s.AppendPushData([]byte("|")))
|
|
137
|
+
if (i < this.protocols.length - 1) {
|
|
138
|
+
const pipeBytes = Utils.toArray('|', 'utf8');
|
|
139
|
+
script.writeBin(pipeBytes);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return new LockingScript(script.chunks);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Finds the position of OP_RETURN in the script
|
|
147
|
+
*
|
|
148
|
+
* Mirrors the Go findReturn function exactly.
|
|
149
|
+
*
|
|
150
|
+
* @param scriptBytes - The script bytes to search
|
|
151
|
+
* @param from - Starting position
|
|
152
|
+
* @returns number - Position of OP_RETURN, or -1 if not found
|
|
153
|
+
*/
|
|
154
|
+
static findReturn(scriptBytes, from) {
|
|
155
|
+
for (let i = from; i < scriptBytes.length; i++) {
|
|
156
|
+
if (scriptBytes[i] === OP.OP_RETURN) {
|
|
157
|
+
return i;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return -1;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Converts various input types to a Script object
|
|
164
|
+
*
|
|
165
|
+
* Mirrors the Go ToScript function exactly.
|
|
166
|
+
*
|
|
167
|
+
* @param data - The data to convert (Script, LockingScript, or byte array)
|
|
168
|
+
* @returns Script - The converted script, or null if conversion fails
|
|
169
|
+
*/
|
|
170
|
+
static toScript(data) {
|
|
171
|
+
if (data instanceof Script) {
|
|
172
|
+
return data;
|
|
173
|
+
}
|
|
174
|
+
if (data instanceof LockingScript) {
|
|
175
|
+
return new Script(data.chunks);
|
|
176
|
+
}
|
|
177
|
+
if (Array.isArray(data)) {
|
|
178
|
+
if (data.length === 0) {
|
|
179
|
+
return new Script();
|
|
180
|
+
}
|
|
181
|
+
return Script.fromBinary(data);
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=bitcom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bitcom.js","sourceRoot":"","sources":["../../src/bitcom/bitcom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AA6B3D;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IAC1B,SAAS,CAAY;IACrB,YAAY,CAAU;IAEtB,YAAY,YAAwB,EAAE,EAAE,eAAyB,EAAE;QAClE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA;IACjC,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,CAAC,MAA8B;QAC3C,6DAA6D;QAC7D,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACpB,OAAO;gBACN,SAAS,EAAE,EAAE;gBACb,YAAY,EAAE,EAAE;aAChB,CAAA;QACF,CAAC;QAED,IAAI,WAAqB,CAAA;QACzB,IAAI,MAAM,YAAY,aAAa,EAAE,CAAC;YACrC,WAAW,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A;gBAC3B,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO,IAAI,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IACxC,CAAC;IAED;;;;;;;;OAQG;IACK,MAAM,CAAC,UAAU,CAAC,WAAqB,EAAE,IAAY;QAC5D,KAAK,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAChD,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,SAAS,EAAE,CAAC;gBACrC,OAAO,CAAC,CAAA;YACT,CAAC;QACF,CAAC;QACD,OAAO,CAAC,CAAC,CAAA;IACV,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAuC;QACtD,IAAI,IAAI,YAAY,MAAM,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAA;QACZ,CAAC;QACD,IAAI,IAAI,YAAY,aAAa,EAAE,CAAC;YACnC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAC/B,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvB,OAAO,IAAI,MAAM,EAAE,CAAA;YACpB,CAAC;YACD,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAC/B,CAAC;QACD,OAAO,IAAI,CAAA;IACZ,CAAC;CACD"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { MAP_PREFIX } from '@1sat/types';
|
|
2
|
+
import { type LockingScript, Script } from '@bsv/sdk';
|
|
3
|
+
export { MAP_PREFIX };
|
|
4
|
+
/**
|
|
5
|
+
* MAP protocol commands
|
|
6
|
+
*/
|
|
7
|
+
export declare enum MAPCommand {
|
|
8
|
+
SET = "SET",
|
|
9
|
+
DEL = "DEL",
|
|
10
|
+
ADD = "ADD",
|
|
11
|
+
SELECT = "SELECT"
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* MAP protocol data structure
|
|
15
|
+
*/
|
|
16
|
+
export interface MAPData {
|
|
17
|
+
cmd: MAPCommand | string;
|
|
18
|
+
data: Record<string, string>;
|
|
19
|
+
adds?: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* MAP (Metadata and Payload) Protocol Template
|
|
23
|
+
*
|
|
24
|
+
* The MAP protocol provides a way to store key-value metadata on the blockchain.
|
|
25
|
+
* It supports different commands for setting, deleting, adding, and selecting data.
|
|
26
|
+
*/
|
|
27
|
+
export default class MAP {
|
|
28
|
+
/**
|
|
29
|
+
* Creates a MAP protocol locking script with SET command
|
|
30
|
+
*
|
|
31
|
+
* @param data - Key-value pairs to set
|
|
32
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
33
|
+
*/
|
|
34
|
+
static set(data: Record<string, string>): LockingScript;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a MAP protocol locking script with ADD command
|
|
37
|
+
*
|
|
38
|
+
* @param key - The key to add values to
|
|
39
|
+
* @param values - Array of values to add
|
|
40
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
41
|
+
*/
|
|
42
|
+
static add(key: string, values: string[]): LockingScript;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a MAP protocol locking script with DEL command
|
|
45
|
+
*
|
|
46
|
+
* @param keys - Array of keys to delete
|
|
47
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
48
|
+
*/
|
|
49
|
+
static del(keys: string[]): LockingScript;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a MAP protocol locking script
|
|
52
|
+
*
|
|
53
|
+
* @param command - The MAP command
|
|
54
|
+
* @param data - The key-value data
|
|
55
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
56
|
+
*/
|
|
57
|
+
static lock(command: MAPCommand | string, data: Record<string, string>): LockingScript;
|
|
58
|
+
/**
|
|
59
|
+
* Decodes MAP protocol data from script
|
|
60
|
+
*
|
|
61
|
+
* @param script - The script containing MAP protocol data
|
|
62
|
+
* @returns MAPData - The decoded MAP protocol data, or null if invalid
|
|
63
|
+
*/
|
|
64
|
+
static decode(script: Script | LockingScript | number[]): MAPData | null;
|
|
65
|
+
/**
|
|
66
|
+
* Cleans a string by replacing null bytes with spaces and handling escape sequences
|
|
67
|
+
*
|
|
68
|
+
* @param str - The string to clean
|
|
69
|
+
* @returns string - The cleaned string
|
|
70
|
+
*/
|
|
71
|
+
private static cleanString;
|
|
72
|
+
/**
|
|
73
|
+
* Helper method to create a simple key-value MAP
|
|
74
|
+
*
|
|
75
|
+
* @param key - The key
|
|
76
|
+
* @param value - The value
|
|
77
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
78
|
+
*/
|
|
79
|
+
static keyValue(key: string, value: string): LockingScript;
|
|
80
|
+
/**
|
|
81
|
+
* Helper method to create an app identification MAP
|
|
82
|
+
*
|
|
83
|
+
* @param appName - The application name
|
|
84
|
+
* @param type - The action/content type
|
|
85
|
+
* @param additionalData - Optional additional key-value pairs
|
|
86
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
87
|
+
*/
|
|
88
|
+
static app(appName: string, type: string, additionalData?: Record<string, string>): LockingScript;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/bitcom/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,EAAS,MAAM,UAAU,CAAA;AAG5D,OAAO,EAAE,UAAU,EAAE,CAAA;AAErB;;GAEG;AACH,oBAAY,UAAU;IACrB,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,MAAM,WAAW;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO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export { MAP_PREFIX };
|
|
5
|
+
/**
|
|
6
|
+
* MAP protocol commands
|
|
7
|
+
*/
|
|
8
|
+
export var MAPCommand;
|
|
9
|
+
(function (MAPCommand) {
|
|
10
|
+
MAPCommand["SET"] = "SET";
|
|
11
|
+
MAPCommand["DEL"] = "DEL";
|
|
12
|
+
MAPCommand["ADD"] = "ADD";
|
|
13
|
+
MAPCommand["SELECT"] = "SELECT";
|
|
14
|
+
})(MAPCommand || (MAPCommand = {}));
|
|
15
|
+
/**
|
|
16
|
+
* MAP (Metadata and Payload) Protocol Template
|
|
17
|
+
*
|
|
18
|
+
* The MAP protocol provides a way to store key-value metadata on the blockchain.
|
|
19
|
+
* It supports different commands for setting, deleting, adding, and selecting data.
|
|
20
|
+
*/
|
|
21
|
+
export default class MAP {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a MAP protocol locking script with SET command
|
|
24
|
+
*
|
|
25
|
+
* @param data - Key-value pairs to set
|
|
26
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
27
|
+
*/
|
|
28
|
+
static set(data) {
|
|
29
|
+
return MAP.lock(MAPCommand.SET, data);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates a MAP protocol locking script with ADD command
|
|
33
|
+
*
|
|
34
|
+
* @param key - The key to add values to
|
|
35
|
+
* @param values - Array of values to add
|
|
36
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
37
|
+
*/
|
|
38
|
+
static add(key, values) {
|
|
39
|
+
const data = {};
|
|
40
|
+
data[key] = values.join(' '); // Join values with space
|
|
41
|
+
return MAP.lock(MAPCommand.ADD, data);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a MAP protocol locking script with DEL command
|
|
45
|
+
*
|
|
46
|
+
* @param keys - Array of keys to delete
|
|
47
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
48
|
+
*/
|
|
49
|
+
static del(keys) {
|
|
50
|
+
const data = {};
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
data[key] = '';
|
|
53
|
+
}
|
|
54
|
+
return MAP.lock(MAPCommand.DEL, data);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a MAP protocol locking script
|
|
58
|
+
*
|
|
59
|
+
* @param command - The MAP command
|
|
60
|
+
* @param data - The key-value data
|
|
61
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
62
|
+
*/
|
|
63
|
+
static lock(command, data) {
|
|
64
|
+
const protocols = [
|
|
65
|
+
{
|
|
66
|
+
protocol: MAP_PREFIX,
|
|
67
|
+
script: [],
|
|
68
|
+
pos: 0,
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
// Build the MAP protocol script: CMD KEY1 VALUE1 KEY2 VALUE2 ...
|
|
72
|
+
const script = new Script();
|
|
73
|
+
// Add COMMAND
|
|
74
|
+
script.writeBin(Utils.toArray(command.toString()));
|
|
75
|
+
// Add key-value pairs
|
|
76
|
+
for (const [key, value] of Object.entries(data)) {
|
|
77
|
+
// Add KEY
|
|
78
|
+
script.writeBin(Utils.toArray(MAP.cleanString(key)));
|
|
79
|
+
// Add VALUE
|
|
80
|
+
script.writeBin(Utils.toArray(MAP.cleanString(value)));
|
|
81
|
+
}
|
|
82
|
+
protocols[0].script = script.toBinary();
|
|
83
|
+
// Create BitCom structure and return locking script
|
|
84
|
+
const bitcom = new BitCom(protocols);
|
|
85
|
+
return bitcom.lock();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Decodes MAP protocol data from script
|
|
89
|
+
*
|
|
90
|
+
* @param script - The script containing MAP protocol data
|
|
91
|
+
* @returns MAPData - The decoded MAP protocol data, or null if invalid
|
|
92
|
+
*/
|
|
93
|
+
static decode(script) {
|
|
94
|
+
let scriptToProcess = null;
|
|
95
|
+
if (Array.isArray(script)) {
|
|
96
|
+
scriptToProcess = Script.fromBinary(script);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
scriptToProcess = BitCom.toScript(script);
|
|
100
|
+
}
|
|
101
|
+
if (scriptToProcess == null) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
// First decode as BitCom to find MAP protocol
|
|
105
|
+
const bitcomData = BitCom.decode(scriptToProcess);
|
|
106
|
+
if (bitcomData == null) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
// Find MAP protocol in the protocols
|
|
110
|
+
const mapProtocol = bitcomData.protocols.find((p) => p.protocol === MAP_PREFIX);
|
|
111
|
+
if (mapProtocol == null) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
// Parse the MAP protocol script using SDK native parsing
|
|
115
|
+
const parsedScript = Script.fromBinary(mapProtocol.script);
|
|
116
|
+
const chunks = parsedScript.chunks;
|
|
117
|
+
if (chunks.length < 1) {
|
|
118
|
+
// At least command
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
// Read COMMAND (first chunk)
|
|
122
|
+
const cmdChunk = chunks[0];
|
|
123
|
+
if (cmdChunk.data == null) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const cmd = Utils.toUTF8(cmdChunk.data);
|
|
127
|
+
const mapData = {
|
|
128
|
+
cmd,
|
|
129
|
+
data: {},
|
|
130
|
+
};
|
|
131
|
+
// Handle SET command - read key-value pairs
|
|
132
|
+
if (cmd === MAPCommand.SET) {
|
|
133
|
+
for (let i = 1; i < chunks.length; i += 2) {
|
|
134
|
+
// Read key
|
|
135
|
+
const keyChunk = chunks[i];
|
|
136
|
+
if (keyChunk?.data == null)
|
|
137
|
+
break;
|
|
138
|
+
// Read value (next chunk)
|
|
139
|
+
const valueChunk = chunks[i + 1];
|
|
140
|
+
if (valueChunk?.data == null)
|
|
141
|
+
break;
|
|
142
|
+
const key = MAP.cleanString(Utils.toUTF8(keyChunk.data));
|
|
143
|
+
const value = MAP.cleanString(Utils.toUTF8(valueChunk.data));
|
|
144
|
+
mapData.data[key] = value;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (cmd === MAPCommand.ADD) {
|
|
148
|
+
// For ADD command, read key and then all remaining values
|
|
149
|
+
if (chunks.length >= 2) {
|
|
150
|
+
const keyChunk = chunks[1];
|
|
151
|
+
if (keyChunk?.data != null) {
|
|
152
|
+
const key = MAP.cleanString(Utils.toUTF8(keyChunk.data));
|
|
153
|
+
const values = [];
|
|
154
|
+
for (let i = 2; i < chunks.length; i++) {
|
|
155
|
+
const valueChunk = chunks[i];
|
|
156
|
+
if (valueChunk?.data != null) {
|
|
157
|
+
values.push(MAP.cleanString(Utils.toUTF8(valueChunk.data)));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
mapData.data[key] = values.join(' ');
|
|
161
|
+
mapData.adds = values;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (cmd === MAPCommand.DEL) {
|
|
166
|
+
// For DEL command, read all keys to delete
|
|
167
|
+
for (let i = 1; i < chunks.length; i++) {
|
|
168
|
+
const keyChunk = chunks[i];
|
|
169
|
+
if (keyChunk?.data != null) {
|
|
170
|
+
const key = MAP.cleanString(Utils.toUTF8(keyChunk.data));
|
|
171
|
+
mapData.data[key] = '';
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return mapData;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Cleans a string by replacing null bytes with spaces and handling escape sequences
|
|
179
|
+
*
|
|
180
|
+
* @param str - The string to clean
|
|
181
|
+
* @returns string - The cleaned string
|
|
182
|
+
*/
|
|
183
|
+
static cleanString(str) {
|
|
184
|
+
return str
|
|
185
|
+
.replace(/\0/g, ' ') // Replace null bytes with spaces
|
|
186
|
+
.replace(/\\u0000/g, ' '); // Replace escaped null bytes with spaces
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Helper method to create a simple key-value MAP
|
|
190
|
+
*
|
|
191
|
+
* @param key - The key
|
|
192
|
+
* @param value - The value
|
|
193
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
194
|
+
*/
|
|
195
|
+
static keyValue(key, value) {
|
|
196
|
+
return MAP.set({ [key]: value });
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Helper method to create an app identification MAP
|
|
200
|
+
*
|
|
201
|
+
* @param appName - The application name
|
|
202
|
+
* @param type - The action/content type
|
|
203
|
+
* @param additionalData - Optional additional key-value pairs
|
|
204
|
+
* @returns LockingScript - The MAP protocol locking script
|
|
205
|
+
*/
|
|
206
|
+
static app(appName, type, additionalData = {}) {
|
|
207
|
+
const data = {
|
|
208
|
+
app: appName,
|
|
209
|
+
type,
|
|
210
|
+
...additionalData,
|
|
211
|
+
};
|
|
212
|
+
return MAP.set(data);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.js","sourceRoot":"","sources":["../../src/bitcom/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAsB,MAAM,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAC5D,OAAO,MAAyB,MAAM,aAAa,CAAA;AAEnD,OAAO,EAAE,UAAU,EAAE,CAAA;AAErB;;GAEG;AACH,MAAM,CAAN,IAAY,UAKX;AALD,WAAY,UAAU;IACrB,yBAAW,CAAA;IACX,yBAAW,CAAA;IACX,yBAAW,CAAA;IACX,+BAAiB,CAAA;AAClB,CAAC,EALW,UAAU,KAAV,UAAU,QAKrB;AAWD;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,GAAG;IACvB;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,IAA4B;QACtC,OAAO,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAW,EAAE,MAAgB;QACvC,M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AC;QACF,CAAC;QAED,OAAO,OAAO,CAAA;IACf,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,WAAW,CAAC,GAAW;QACrC,OAAO,GAAG;aACR,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,iCAAiC;aACrD,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAA,CAAC,yCAAyC;IACrE,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,QAAQ,CAAC,GAAW,EAAE,KAAa;QACzC,OAAO,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;IACjC,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,GAAG,CACT,OAAe,EACf,IAAY,EACZ,iBAAyC,EAAE;QAE3C,MAAM,IAAI,GAAG;YACZ,GAAG,EAAE,OAAO;YACZ,IAAI;YACJ,GAAG,cAAc;SACjB,CAAA;QACD,OAAO,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;CACD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type LockingScript, type PrivateKey, type PublicKey, Script, type ScriptTemplate, type Transaction, type UnlockingScript } from '@bsv/sdk';
|
|
2
|
+
import { Algorithm } from 'sigma-protocol';
|
|
3
|
+
import { type BitComDecoded } from './bitcom.js';
|
|
4
|
+
/**
|
|
5
|
+
* SIGMA protocol identifier
|
|
6
|
+
*/
|
|
7
|
+
export declare const SIGMA_PREFIX = "SIGMA";
|
|
8
|
+
/**
|
|
9
|
+
* Re-export Algorithm from sigma-protocol for consistency
|
|
10
|
+
*/
|
|
11
|
+
export { Algorithm as SigmaAlgorithm };
|
|
12
|
+
/**
|
|
13
|
+
* SIGMA signature data structure
|
|
14
|
+
*/
|
|
15
|
+
export interface SigmaData {
|
|
16
|
+
/** BitCom protocol index */
|
|
17
|
+
bitcomIndex?: number;
|
|
18
|
+
/** Signing algorithm (BSM or BRC77) */
|
|
19
|
+
algorithm: Algorithm;
|
|
20
|
+
/** Bitcoin address of signer */
|
|
21
|
+
address: string;
|
|
22
|
+
/** Cryptographic signature as number array */
|
|
23
|
+
signature: number[];
|
|
24
|
+
/** Input index (vin) that anchors the signature */
|
|
25
|
+
vin: number;
|
|
26
|
+
/** Whether signature verification passed */
|
|
27
|
+
valid?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Options for SIGMA signature creation
|
|
31
|
+
*/
|
|
32
|
+
export interface SigmaOptions {
|
|
33
|
+
/** Signing algorithm (default: BSM) */
|
|
34
|
+
algorithm?: Algorithm;
|
|
35
|
+
/** Input index to anchor signature (default: 0) */
|
|
36
|
+
vin?: number;
|
|
37
|
+
/** For BRC-77: specific verifier public key (private signature) */
|
|
38
|
+
verifier?: PublicKey;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* SIGMA (Secure Identity for Global Message Authentication) implementation
|
|
42
|
+
*
|
|
43
|
+
* SIGMA enables cryptographic signing of blockchain content by combining:
|
|
44
|
+
* - Input hash: SHA256 of the outpoint (txid + vout) from a specific input
|
|
45
|
+
* - Data hash: SHA256 of the script data before the SIGMA protocol marker
|
|
46
|
+
*
|
|
47
|
+
* Supports both BSM (Bitcoin Signed Message) and BRC-77 signing algorithms.
|
|
48
|
+
*/
|
|
49
|
+
export default class Sigma implements ScriptTemplate {
|
|
50
|
+
readonly data: SigmaData;
|
|
51
|
+
constructor(data: SigmaData);
|
|
52
|
+
/**
|
|
53
|
+
* Extract SIGMA signatures from BitCom transaction
|
|
54
|
+
*/
|
|
55
|
+
static decode(bitcom: BitComDecoded): Sigma[];
|
|
56
|
+
/**
|
|
57
|
+
* Decode SIGMA signatures directly from a Script
|
|
58
|
+
*/
|
|
59
|
+
static decodeFromScript(script: Script | LockingScript): Sigma[];
|
|
60
|
+
/**
|
|
61
|
+
* Create SIGMA signature for data
|
|
62
|
+
*
|
|
63
|
+
* @param inputHash - SHA256 hash of the input outpoint
|
|
64
|
+
* @param dataHash - SHA256 hash of the script data
|
|
65
|
+
* @param privateKey - Private key for signing
|
|
66
|
+
* @param options - Additional signing options
|
|
67
|
+
*/
|
|
68
|
+
static sign(inputHash: number[], dataHash: number[], privateKey: PrivateKey, options?: SigmaOptions): Sigma;
|
|
69
|
+
/**
|
|
70
|
+
* Verify SIGMA signature against provided hashes
|
|
71
|
+
*
|
|
72
|
+
* @param inputHash - SHA256 hash of the input outpoint
|
|
73
|
+
* @param dataHash - SHA256 hash of the script data
|
|
74
|
+
* @param recipientPrivateKey - For BRC-77 private signatures, the recipient's key
|
|
75
|
+
*/
|
|
76
|
+
verifyWithHashes(inputHash: number[], dataHash: number[], recipientPrivateKey?: PrivateKey): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Check if signature was previously verified
|
|
79
|
+
*/
|
|
80
|
+
verify(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Generate locking script for SIGMA within BitCom
|
|
83
|
+
*/
|
|
84
|
+
lock(): LockingScript;
|
|
85
|
+
/**
|
|
86
|
+
* Unlock method is not available for SIGMA scripts
|
|
87
|
+
*/
|
|
88
|
+
unlock(): {
|
|
89
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
90
|
+
estimateLength: () => Promise<number>;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=sigma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sigma.d.ts","sourceRoot":"","sources":["../../src/bitcom/sigma.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,SAAS,EACd,MAAM,EACN,KAAK,cAAc,EAGnB,KAAK,WAAW,EAChB,KAAK,eAAe,EAEpB,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAe,EAAiB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA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