@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,88 @@
|
|
|
1
|
+
import { type LockingScript, type ScriptTemplate, type UnlockingScript } from '@bsv/sdk';
|
|
2
|
+
import type { Signer } from '../signer.js';
|
|
3
|
+
import { type BitComDecoded } from './bitcom.js';
|
|
4
|
+
/**
|
|
5
|
+
* AIP (Author Identity Protocol) prefix for BitCom transactions
|
|
6
|
+
*/
|
|
7
|
+
export declare const AIP_PREFIX = "15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva";
|
|
8
|
+
/**
|
|
9
|
+
* AIP signature data structure
|
|
10
|
+
*/
|
|
11
|
+
export interface AIPData {
|
|
12
|
+
/** BitCom protocol index */
|
|
13
|
+
bitcomIndex?: number;
|
|
14
|
+
/** Signing algorithm (typically "BITCOIN_ECDSA") */
|
|
15
|
+
algorithm: string;
|
|
16
|
+
/** Bitcoin address of signer */
|
|
17
|
+
address: string;
|
|
18
|
+
/** Cryptographic signature as number array */
|
|
19
|
+
signature: number[];
|
|
20
|
+
/** Optional field indexes that were signed */
|
|
21
|
+
fieldIndexes?: number[];
|
|
22
|
+
/** Whether signature verification passed */
|
|
23
|
+
valid?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for AIP signature creation
|
|
27
|
+
*/
|
|
28
|
+
export interface AIPOptions {
|
|
29
|
+
/** Signing algorithm (default: "BITCOIN_ECDSA") */
|
|
30
|
+
algorithm?: string;
|
|
31
|
+
/** Specific field indexes to sign (default: sign all fields) */
|
|
32
|
+
fieldIndexes?: number[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* AIP (Author Identity Protocol) implementation
|
|
36
|
+
*
|
|
37
|
+
* AIP enables cryptographic signing of blockchain content with Bitcoin addresses,
|
|
38
|
+
* providing verifiable authorship and identity verification within BitCom transactions.
|
|
39
|
+
*/
|
|
40
|
+
export default class AIP implements ScriptTemplate {
|
|
41
|
+
readonly data: AIPData;
|
|
42
|
+
constructor(data: AIPData);
|
|
43
|
+
/**
|
|
44
|
+
* Extract AIP signatures from BitCom transaction
|
|
45
|
+
*
|
|
46
|
+
* @param bitcom - Decoded BitCom transaction
|
|
47
|
+
* @returns Array of AIP signatures found in transaction
|
|
48
|
+
*/
|
|
49
|
+
static decode(bitcom: BitComDecoded): AIP[];
|
|
50
|
+
/**
|
|
51
|
+
* Create AIP signature for data using a Signer.
|
|
52
|
+
*
|
|
53
|
+
* @param data - Data to sign as number array
|
|
54
|
+
* @param signer - A Signer (PrivateKeySigner or WalletSigner)
|
|
55
|
+
* @param options - Additional signing options
|
|
56
|
+
* @returns AIP signature object
|
|
57
|
+
*/
|
|
58
|
+
static sign(data: number[], signer: Signer, options?: AIPOptions): Promise<AIP>;
|
|
59
|
+
/**
|
|
60
|
+
* Verify AIP signature
|
|
61
|
+
*
|
|
62
|
+
* @returns True if signature is valid
|
|
63
|
+
*/
|
|
64
|
+
verify(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Generate locking script for AIP within BitCom
|
|
67
|
+
*
|
|
68
|
+
* @returns Locking script
|
|
69
|
+
*/
|
|
70
|
+
lock(): LockingScript;
|
|
71
|
+
/**
|
|
72
|
+
* Unlock method is not available for AIP scripts
|
|
73
|
+
*
|
|
74
|
+
* @throws Error - AIP signatures cannot be unlocked
|
|
75
|
+
*/
|
|
76
|
+
unlock(): {
|
|
77
|
+
sign: (tx: unknown, inputIndex: number) => Promise<UnlockingScript>;
|
|
78
|
+
estimateLength: () => Promise<number>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Validate AIP signature against protocol data
|
|
82
|
+
*
|
|
83
|
+
* @param aip - AIP to validate
|
|
84
|
+
* @param protocols - Protocols that came before this AIP
|
|
85
|
+
*/
|
|
86
|
+
private static validateAIP;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=aip.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aip.d.ts","sourceRoot":"","sources":["../../src/bitcom/aip.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,aAAa,EAIlB,KAAK,cAAc,EAEnB,KAAK,eAAe,EAEpB,MAAM,UAAU,CAAA;AACjB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAe,EAAiB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA;AAEvE;;GAEG;AACH,eAAO,MAAM,UAAU,uCAAuC,CAAA;AAE9D;;GAEG;AACH,MAAM,WAAW,OAAO;IACvB,4BAA4B;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAA;IACjB,gCAAgC;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,8CAA8C;IAC9C,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB,8CAA8C;IAC9C,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB,4CAA4C;IAC5C,KAAK,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,mDAAmD;IACnD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,gEAAgE;IAChE,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CACvB;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,GAAI,YAAW,cAAc;IACjD,SAAgB,IAAI,EAAE,OAAO,CAAA;gBAEjB,IAAI,EAAE,OAAO;IAIzB;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,GAAG,EAAE;IAuD3C;;;;;;;OAOG;WACU,IAAI,CAChB,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,UAAe,GACtB,OAAO,CAAC,GAAG,CAAC;IAyBf;;;;OAIG;IACH,MAAM,IAAI,OAAO;IAIjB;;;;OAIG;IACH,IAAI,IAAI,aAAa;IAgCrB;;;;OAIG;IACH,MAAM,IAAI;QACT,IAAI,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;QACnE,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;KACrC;IAID;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;CAsE1B"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { BSM, BigNumber, OP, PublicKey, Script, Signature, Utils, } from '@bsv/sdk';
|
|
2
|
+
import BitCom from './bitcom.js';
|
|
3
|
+
/**
|
|
4
|
+
* AIP (Author Identity Protocol) prefix for BitCom transactions
|
|
5
|
+
*/
|
|
6
|
+
export const AIP_PREFIX = '15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva';
|
|
7
|
+
/**
|
|
8
|
+
* AIP (Author Identity Protocol) implementation
|
|
9
|
+
*
|
|
10
|
+
* AIP enables cryptographic signing of blockchain content with Bitcoin addresses,
|
|
11
|
+
* providing verifiable authorship and identity verification within BitCom transactions.
|
|
12
|
+
*/
|
|
13
|
+
export default class AIP {
|
|
14
|
+
data;
|
|
15
|
+
constructor(data) {
|
|
16
|
+
this.data = data;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Extract AIP signatures from BitCom transaction
|
|
20
|
+
*
|
|
21
|
+
* @param bitcom - Decoded BitCom transaction
|
|
22
|
+
* @returns Array of AIP signatures found in transaction
|
|
23
|
+
*/
|
|
24
|
+
static decode(bitcom) {
|
|
25
|
+
const aips = [];
|
|
26
|
+
// Safety check for nil
|
|
27
|
+
if (bitcom?.protocols?.length === 0) {
|
|
28
|
+
return aips;
|
|
29
|
+
}
|
|
30
|
+
for (let protoIdx = 0; protoIdx < bitcom.protocols.length; protoIdx++) {
|
|
31
|
+
const protocol = bitcom.protocols[protoIdx];
|
|
32
|
+
if (protocol.protocol === AIP_PREFIX) {
|
|
33
|
+
try {
|
|
34
|
+
const script = Script.fromBinary(protocol.script);
|
|
35
|
+
const chunks = script.chunks;
|
|
36
|
+
// Need at least algorithm, address, and signature
|
|
37
|
+
if (chunks?.length < 3) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const aip = new AIP({
|
|
41
|
+
bitcomIndex: protoIdx,
|
|
42
|
+
algorithm: Utils.toUTF8(chunks[0].data ?? []),
|
|
43
|
+
address: Utils.toUTF8(chunks[1].data ?? []),
|
|
44
|
+
signature: Array.from(chunks[2].data ?? []),
|
|
45
|
+
fieldIndexes: undefined,
|
|
46
|
+
valid: undefined,
|
|
47
|
+
});
|
|
48
|
+
// Read optional field indexes (remaining chunks)
|
|
49
|
+
const fieldIndexes = [];
|
|
50
|
+
for (let i = 3; i < chunks.length; i++) {
|
|
51
|
+
const indexStr = Utils.toUTF8(chunks[i].data ?? []);
|
|
52
|
+
const index = Number.parseInt(indexStr, 10);
|
|
53
|
+
if (Number.isInteger(index) && !Number.isNaN(index)) {
|
|
54
|
+
fieldIndexes.push(index);
|
|
55
|
+
}
|
|
56
|
+
// Continue parsing even if we encounter invalid field indexes
|
|
57
|
+
}
|
|
58
|
+
if (fieldIndexes.length > 0) {
|
|
59
|
+
aip.data.fieldIndexes = fieldIndexes;
|
|
60
|
+
}
|
|
61
|
+
// Validate signature against protocols that came before this AIP
|
|
62
|
+
AIP.validateAIP(aip, bitcom.protocols.slice(0, protoIdx));
|
|
63
|
+
aips.push(aip);
|
|
64
|
+
}
|
|
65
|
+
catch { }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return aips;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create AIP signature for data using a Signer.
|
|
72
|
+
*
|
|
73
|
+
* @param data - Data to sign as number array
|
|
74
|
+
* @param signer - A Signer (PrivateKeySigner or WalletSigner)
|
|
75
|
+
* @param options - Additional signing options
|
|
76
|
+
* @returns AIP signature object
|
|
77
|
+
*/
|
|
78
|
+
static async sign(data, signer, options = {}) {
|
|
79
|
+
const algorithm = options.algorithm ?? 'BITCOIN_ECDSA';
|
|
80
|
+
const bsmHash = BSM.magicHash(data);
|
|
81
|
+
const derSig = await signer.signHash(Array.from(bsmHash));
|
|
82
|
+
const pubKeyHex = await signer.getPublicKey();
|
|
83
|
+
const publicKey = PublicKey.fromString(pubKeyHex);
|
|
84
|
+
const signature = Signature.fromDER(derSig);
|
|
85
|
+
const recovery = signature.CalculateRecoveryFactor(publicKey, new BigNumber(bsmHash));
|
|
86
|
+
const compactSig = signature.toCompact(recovery, true);
|
|
87
|
+
return new AIP({
|
|
88
|
+
algorithm,
|
|
89
|
+
address: publicKey.toAddress(),
|
|
90
|
+
signature: compactSig,
|
|
91
|
+
fieldIndexes: options.fieldIndexes,
|
|
92
|
+
valid: true,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Verify AIP signature
|
|
97
|
+
*
|
|
98
|
+
* @returns True if signature is valid
|
|
99
|
+
*/
|
|
100
|
+
verify() {
|
|
101
|
+
return this.data.valid === true;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Generate locking script for AIP within BitCom
|
|
105
|
+
*
|
|
106
|
+
* @returns Locking script
|
|
107
|
+
*/
|
|
108
|
+
lock() {
|
|
109
|
+
const script = new Script();
|
|
110
|
+
// Add algorithm
|
|
111
|
+
script.writeBin(Utils.toArray(this.data.algorithm, 'utf8'));
|
|
112
|
+
// Add address
|
|
113
|
+
script.writeBin(Utils.toArray(this.data.address, 'utf8'));
|
|
114
|
+
// Add signature
|
|
115
|
+
script.writeBin(this.data.signature);
|
|
116
|
+
// Add field indexes if present
|
|
117
|
+
if (this.data.fieldIndexes != null && this.data.fieldIndexes.length > 0) {
|
|
118
|
+
for (const index of this.data.fieldIndexes) {
|
|
119
|
+
script.writeBin(Utils.toArray(index.toString(), 'utf8'));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Create BitCom protocol
|
|
123
|
+
const protocols = [
|
|
124
|
+
{
|
|
125
|
+
protocol: AIP_PREFIX,
|
|
126
|
+
script: script.toBinary(),
|
|
127
|
+
pos: 0,
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
const bitcom = new BitCom(protocols);
|
|
131
|
+
return bitcom.lock();
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Unlock method is not available for AIP scripts
|
|
135
|
+
*
|
|
136
|
+
* @throws Error - AIP signatures cannot be unlocked
|
|
137
|
+
*/
|
|
138
|
+
unlock() {
|
|
139
|
+
throw new Error('AIP signatures cannot be unlocked');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Validate AIP signature against protocol data
|
|
143
|
+
*
|
|
144
|
+
* @param aip - AIP to validate
|
|
145
|
+
* @param protocols - Protocols that came before this AIP
|
|
146
|
+
*/
|
|
147
|
+
static validateAIP(aip, protocols) {
|
|
148
|
+
try {
|
|
149
|
+
// Reconstruct signed data exactly as Go implementation
|
|
150
|
+
const data = [];
|
|
151
|
+
let fieldIndex = 0;
|
|
152
|
+
// Add OP_RETURN
|
|
153
|
+
data.push(OP.OP_RETURN);
|
|
154
|
+
// Process each protocol
|
|
155
|
+
for (const protocol of protocols) {
|
|
156
|
+
// Add protocol prefix
|
|
157
|
+
data.push(...Utils.toArray(protocol.protocol, 'utf8'));
|
|
158
|
+
// Parse protocol script
|
|
159
|
+
const script = Script.fromBinary(protocol.script);
|
|
160
|
+
const chunks = script.chunks;
|
|
161
|
+
// Process script chunks
|
|
162
|
+
for (const chunk of chunks) {
|
|
163
|
+
if (chunk.data != null && chunk.data.length > 0) {
|
|
164
|
+
// Check if this field should be signed
|
|
165
|
+
const shouldSign = aip.data.fieldIndexes == null ||
|
|
166
|
+
aip.data.fieldIndexes.includes(fieldIndex);
|
|
167
|
+
if (shouldSign) {
|
|
168
|
+
data.push(...Array.from(chunk.data));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (chunk.op != null && chunk.op > 0x43 && chunk.op < 0x4f) {
|
|
172
|
+
// Handle printable opcodes
|
|
173
|
+
data.push(chunk.op);
|
|
174
|
+
}
|
|
175
|
+
fieldIndex++;
|
|
176
|
+
}
|
|
177
|
+
// Add pipe delimiter
|
|
178
|
+
data.push(0x7c); // '|'
|
|
179
|
+
}
|
|
180
|
+
// Verify signature using BSM following the pattern from BAP library
|
|
181
|
+
const signatureBase64 = Utils.toBase64(aip.data.signature);
|
|
182
|
+
const sig = Signature.fromCompact(signatureBase64, 'base64');
|
|
183
|
+
// Try all recovery factors (0-3) to find the correct public key
|
|
184
|
+
for (let recovery = 0; recovery < 4; recovery++) {
|
|
185
|
+
try {
|
|
186
|
+
const publicKey = sig.RecoverPublicKey(recovery, new BigNumber(BSM.magicHash(data)));
|
|
187
|
+
const sigFitsPubkey = BSM.verify(data, sig, publicKey);
|
|
188
|
+
if (sigFitsPubkey &&
|
|
189
|
+
publicKey.toAddress().toString() === aip.data.address) {
|
|
190
|
+
aip.data.valid = true;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
// Try next recovery factor
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
aip.data.valid = false;
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
aip.data.valid = false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}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AC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YAC1D,MAAM,GAAG,GAAG,SAAS,CAAC,WAAW,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAA;YAE5D,gEAAgE;YAChE,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE,QAAQ,EAAE,EAAE,CAAC;gBACjD,IAAI,CAAC;oBACJ,MAAM,SAAS,GAAG,GAAG,CAAC,gBAAgB,CACrC,QAAQ,EACR,IAAI,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAClC,CAAA;oBAED,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAA;oBACtD,IACC,aAAa;wBACb,SAAS,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,KAAK,GAAG,CAAC,IAAI,CAAC,OAAO,EACpD,CAAC;wBACF,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;wBACrB,OAAM;oBACP,CAAC;gBACF,CAAC;gBAAC,MAAM,CAAC;oBACR,2BAA2B;gBAC5B,CAAC;YACF,CAAC;YAED,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QACvB,CAAC;QAAC,MAAM,CAAC;YACR,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QACvB,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { type LockingScript, Script } from '@bsv/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* B Protocol Prefix - the BitCom protocol address for B
|
|
4
|
+
*/
|
|
5
|
+
export declare const B_PREFIX = "19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut";
|
|
6
|
+
/**
|
|
7
|
+
* Media types for B protocol
|
|
8
|
+
*/
|
|
9
|
+
export declare enum MediaType {
|
|
10
|
+
TextPlain = "text/plain",
|
|
11
|
+
TextMarkdown = "text/markdown",
|
|
12
|
+
TextHTML = "text/html",
|
|
13
|
+
ImagePNG = "image/png",
|
|
14
|
+
ImageJPEG = "image/jpeg",
|
|
15
|
+
ApplicationJSON = "application/json",
|
|
16
|
+
ApplicationPDF = "application/pdf"
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Encoding types for B protocol
|
|
20
|
+
*/
|
|
21
|
+
export declare enum Encoding {
|
|
22
|
+
UTF8 = "utf-8",
|
|
23
|
+
Binary = "binary",
|
|
24
|
+
Base64 = "base64",
|
|
25
|
+
Hex = "hex"
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* B protocol data structure
|
|
29
|
+
* B PROTOCOL FORMAT: PREFIX DATA MEDIA_TYPE ENCODING FILENAME
|
|
30
|
+
*/
|
|
31
|
+
export interface BData {
|
|
32
|
+
mediaType: MediaType | string;
|
|
33
|
+
encoding: Encoding | string;
|
|
34
|
+
data: number[];
|
|
35
|
+
filename?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* B (Binary) Protocol Template
|
|
39
|
+
*
|
|
40
|
+
* The B protocol is used for storing arbitrary binary data on the blockchain.
|
|
41
|
+
* It follows the format: PREFIX DATA MEDIA_TYPE ENCODING [FILENAME]
|
|
42
|
+
* where FILENAME is optional.
|
|
43
|
+
*/
|
|
44
|
+
export default class B {
|
|
45
|
+
/**
|
|
46
|
+
* Creates a B protocol locking script
|
|
47
|
+
*
|
|
48
|
+
* @param data - The binary data to store
|
|
49
|
+
* @param mediaType - The MIME type of the data
|
|
50
|
+
* @param encoding - The encoding format
|
|
51
|
+
* @param filename - Optional filename
|
|
52
|
+
* @returns LockingScript - The B protocol locking script
|
|
53
|
+
*/
|
|
54
|
+
static lock(data: number[] | string | Uint8Array, mediaType?: MediaType | string, encoding?: Encoding | string, filename?: string): LockingScript;
|
|
55
|
+
/**
|
|
56
|
+
* Decodes B protocol data from script
|
|
57
|
+
*
|
|
58
|
+
* @param script - The script containing B protocol data
|
|
59
|
+
* @returns BData - The decoded B protocol data, or null if invalid
|
|
60
|
+
*/
|
|
61
|
+
static decode(script: Script | LockingScript | number[]): BData | null;
|
|
62
|
+
/**
|
|
63
|
+
* Helper method to create text content
|
|
64
|
+
*
|
|
65
|
+
* @param text - The text content
|
|
66
|
+
* @param mediaType - Optional media type (defaults to text/plain)
|
|
67
|
+
* @param filename - Optional filename
|
|
68
|
+
* @returns LockingScript - The B protocol locking script
|
|
69
|
+
*/
|
|
70
|
+
static text(text: string, mediaType?: MediaType | string, filename?: string): LockingScript;
|
|
71
|
+
/**
|
|
72
|
+
* Helper method to create binary content
|
|
73
|
+
*
|
|
74
|
+
* @param data - The binary data
|
|
75
|
+
* @param mediaType - The media type
|
|
76
|
+
* @param filename - Optional filename
|
|
77
|
+
* @returns LockingScript - The B protocol locking script
|
|
78
|
+
*/
|
|
79
|
+
static binary(data: number[] | Uint8Array, mediaType: MediaType | string, filename?: string): LockingScript;
|
|
80
|
+
/**
|
|
81
|
+
* Helper method to create base64 encoded content
|
|
82
|
+
*
|
|
83
|
+
* @param base64Data - The base64 encoded data
|
|
84
|
+
* @param mediaType - The media type
|
|
85
|
+
* @param filename - Optional filename
|
|
86
|
+
* @returns LockingScript - The B protocol locking script
|
|
87
|
+
*/
|
|
88
|
+
static base64(base64Data: string, mediaType: MediaType | string, filename?: string): LockingScript;
|
|
89
|
+
/**
|
|
90
|
+
* Helper method to create hex encoded content
|
|
91
|
+
*
|
|
92
|
+
* @param hexData - The hex encoded data
|
|
93
|
+
* @param mediaType - The media type
|
|
94
|
+
* @param filename - Optional filename
|
|
95
|
+
* @returns LockingScript - The B protocol locking script
|
|
96
|
+
*/
|
|
97
|
+
static hex(hexData: string, mediaType: MediaType | string, filename?: string): LockingScript;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=b.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"b.d.ts","sourceRoot":"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I,EAAE,MAAM,EAAE,GAAG,UAAU,EAC3B,SAAS,EAAE,SAAS,GAAG,MAAM,EAC7B,QAAQ,CAAC,EAAE,MAAM,GACf,aAAa;IAIhB;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,CACZ,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,SAAS,GAAG,MAAM,EAC7B,QAAQ,CAAC,EAAE,MAAM,GACf,aAAa;IAIhB;;;;;;;OAOG;IACH,MAAM,CAAC,GAAG,CACT,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,SAAS,GAAG,MAAM,EAC7B,QAAQ,CAAC,EAAE,MAAM,GACf,aAAa;CAGhB"}
|
package/dist/bitcom/b.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Script, Utils } from '@bsv/sdk';
|
|
2
|
+
import BitCom from './bitcom.js';
|
|
3
|
+
/**
|
|
4
|
+
* B Protocol Prefix - the BitCom protocol address for B
|
|
5
|
+
*/
|
|
6
|
+
export const B_PREFIX = '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut';
|
|
7
|
+
/**
|
|
8
|
+
* Media types for B protocol
|
|
9
|
+
*/
|
|
10
|
+
export var MediaType;
|
|
11
|
+
(function (MediaType) {
|
|
12
|
+
MediaType["TextPlain"] = "text/plain";
|
|
13
|
+
MediaType["TextMarkdown"] = "text/markdown";
|
|
14
|
+
MediaType["TextHTML"] = "text/html";
|
|
15
|
+
MediaType["ImagePNG"] = "image/png";
|
|
16
|
+
MediaType["ImageJPEG"] = "image/jpeg";
|
|
17
|
+
MediaType["ApplicationJSON"] = "application/json";
|
|
18
|
+
MediaType["ApplicationPDF"] = "application/pdf";
|
|
19
|
+
})(MediaType || (MediaType = {}));
|
|
20
|
+
/**
|
|
21
|
+
* Encoding types for B protocol
|
|
22
|
+
*/
|
|
23
|
+
export var Encoding;
|
|
24
|
+
(function (Encoding) {
|
|
25
|
+
Encoding["UTF8"] = "utf-8";
|
|
26
|
+
Encoding["Binary"] = "binary";
|
|
27
|
+
Encoding["Base64"] = "base64";
|
|
28
|
+
Encoding["Hex"] = "hex";
|
|
29
|
+
})(Encoding || (Encoding = {}));
|
|
30
|
+
/**
|
|
31
|
+
* B (Binary) Protocol Template
|
|
32
|
+
*
|
|
33
|
+
* The B protocol is used for storing arbitrary binary data on the blockchain.
|
|
34
|
+
* It follows the format: PREFIX DATA MEDIA_TYPE ENCODING [FILENAME]
|
|
35
|
+
* where FILENAME is optional.
|
|
36
|
+
*/
|
|
37
|
+
export default class B {
|
|
38
|
+
/**
|
|
39
|
+
* Creates a B protocol locking script
|
|
40
|
+
*
|
|
41
|
+
* @param data - The binary data to store
|
|
42
|
+
* @param mediaType - The MIME type of the data
|
|
43
|
+
* @param encoding - The encoding format
|
|
44
|
+
* @param filename - Optional filename
|
|
45
|
+
* @returns LockingScript - The B protocol locking script
|
|
46
|
+
*/
|
|
47
|
+
static lock(data, mediaType = MediaType.TextPlain, encoding = Encoding.UTF8, filename) {
|
|
48
|
+
// Convert data to number array
|
|
49
|
+
let dataBytes;
|
|
50
|
+
if (typeof data === 'string') {
|
|
51
|
+
if (encoding === Encoding.Hex) {
|
|
52
|
+
dataBytes = Utils.toArray(data, 'hex');
|
|
53
|
+
}
|
|
54
|
+
else if (encoding === Encoding.Base64) {
|
|
55
|
+
dataBytes = Utils.toArray(data, 'base64');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
dataBytes = Utils.toArray(data, 'utf8');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else if (data instanceof Uint8Array) {
|
|
62
|
+
dataBytes = Array.from(data);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
dataBytes = data;
|
|
66
|
+
}
|
|
67
|
+
const protocols = [
|
|
68
|
+
{
|
|
69
|
+
protocol: B_PREFIX,
|
|
70
|
+
script: [],
|
|
71
|
+
pos: 0,
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
// Build the B protocol script: DATA MEDIA_TYPE ENCODING [FILENAME]
|
|
75
|
+
const script = new Script();
|
|
76
|
+
// Add DATA as push data
|
|
77
|
+
script.writeBin(dataBytes);
|
|
78
|
+
// Add MEDIA_TYPE as push data
|
|
79
|
+
script.writeBin(Utils.toArray(mediaType));
|
|
80
|
+
// Add ENCODING as push data
|
|
81
|
+
script.writeBin(Utils.toArray(encoding));
|
|
82
|
+
// Add optional FILENAME as push data
|
|
83
|
+
if (filename != null && filename !== '') {
|
|
84
|
+
script.writeBin(Utils.toArray(filename));
|
|
85
|
+
}
|
|
86
|
+
protocols[0].script = script.toBinary();
|
|
87
|
+
// Create BitCom structure and return locking script
|
|
88
|
+
const bitcom = new BitCom(protocols);
|
|
89
|
+
return bitcom.lock();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Decodes B protocol data from script
|
|
93
|
+
*
|
|
94
|
+
* @param script - The script containing B protocol data
|
|
95
|
+
* @returns BData - The decoded B protocol data, or null if invalid
|
|
96
|
+
*/
|
|
97
|
+
static decode(script) {
|
|
98
|
+
let scriptToProcess = null;
|
|
99
|
+
if (Array.isArray(script)) {
|
|
100
|
+
scriptToProcess = Script.fromBinary(script);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
scriptToProcess = BitCom.toScript(script);
|
|
104
|
+
}
|
|
105
|
+
if (scriptToProcess == null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
// First decode as BitCom to find B protocol
|
|
109
|
+
const bitcomData = BitCom.decode(scriptToProcess);
|
|
110
|
+
if (bitcomData == null) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
// Find B protocol in the protocols
|
|
114
|
+
const bProtocol = bitcomData.protocols.find((p) => p.protocol === B_PREFIX);
|
|
115
|
+
if (bProtocol == null) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
// Parse the B protocol script: DATA MEDIA_TYPE ENCODING [FILENAME]
|
|
119
|
+
const parsedScript = Script.fromBinary(bProtocol.script);
|
|
120
|
+
const chunks = parsedScript.chunks;
|
|
121
|
+
if (chunks.length < 3) {
|
|
122
|
+
// Need at least DATA, MEDIA_TYPE, ENCODING
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
// Access parsed chunks directly
|
|
126
|
+
const dataChunk = chunks[0];
|
|
127
|
+
if (dataChunk.data == null) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const data = dataChunk.data;
|
|
131
|
+
// Return null for empty data as expected by tests
|
|
132
|
+
if (data.length === 0)
|
|
133
|
+
return null;
|
|
134
|
+
// Read MEDIA_TYPE (second chunk)
|
|
135
|
+
const mediaTypeChunk = chunks[1];
|
|
136
|
+
if (mediaTypeChunk.data == null) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const mediaType = Utils.toUTF8(mediaTypeChunk.data);
|
|
140
|
+
// Read ENCODING (third chunk)
|
|
141
|
+
const encodingChunk = chunks[2];
|
|
142
|
+
if (encodingChunk.data == null) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const encoding = Utils.toUTF8(encodingChunk.data);
|
|
146
|
+
// Try to read optional FILENAME (fourth chunk)
|
|
147
|
+
let filename;
|
|
148
|
+
if (chunks.length >= 4) {
|
|
149
|
+
const filenameChunk = chunks[3];
|
|
150
|
+
if (filenameChunk.data != null) {
|
|
151
|
+
filename = Utils.toUTF8(filenameChunk.data);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
data,
|
|
156
|
+
mediaType,
|
|
157
|
+
encoding,
|
|
158
|
+
filename,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Helper method to create text content
|
|
163
|
+
*
|
|
164
|
+
* @param text - The text content
|
|
165
|
+
* @param mediaType - Optional media type (defaults to text/plain)
|
|
166
|
+
* @param filename - Optional filename
|
|
167
|
+
* @returns LockingScript - The B protocol locking script
|
|
168
|
+
*/
|
|
169
|
+
static text(text, mediaType = MediaType.TextPlain, filename) {
|
|
170
|
+
return B.lock(text, mediaType, Encoding.UTF8, filename);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Helper method to create binary content
|
|
174
|
+
*
|
|
175
|
+
* @param data - The binary data
|
|
176
|
+
* @param mediaType - The media type
|
|
177
|
+
* @param filename - Optional filename
|
|
178
|
+
* @returns LockingScript - The B protocol locking script
|
|
179
|
+
*/
|
|
180
|
+
static binary(data, mediaType, filename) {
|
|
181
|
+
return B.lock(data, mediaType, Encoding.Binary, filename);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Helper method to create base64 encoded content
|
|
185
|
+
*
|
|
186
|
+
* @param base64Data - The base64 encoded data
|
|
187
|
+
* @param mediaType - The media type
|
|
188
|
+
* @param filename - Optional filename
|
|
189
|
+
* @returns LockingScript - The B protocol locking script
|
|
190
|
+
*/
|
|
191
|
+
static base64(base64Data, mediaType, filename) {
|
|
192
|
+
return B.lock(base64Data, mediaType, Encoding.Base64, filename);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Helper method to create hex encoded content
|
|
196
|
+
*
|
|
197
|
+
* @param hexData - The hex encoded data
|
|
198
|
+
* @param mediaType - The media type
|
|
199
|
+
* @param filename - Optional filename
|
|
200
|
+
* @returns LockingScript - The B protocol locking script
|
|
201
|
+
*/
|
|
202
|
+
static hex(hexData, mediaType, filename) {
|
|
203
|
+
return B.lock(hexData, mediaType, Encoding.Hex, filename);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=b.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"b.js","sourceRoot":"","sources":["../../src/bitcom/b.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,MAAM,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAC5D,OAAO,MAAyB,MAAM,aAAa,CAAA;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,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