b017 0.0.0-b
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/CHANGELOG.md +33 -0
- package/LICENSE.txt +56 -0
- package/README.md +206 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +15 -0
- package/dist/lib/boltLib.d.ts +49 -0
- package/dist/lib/boltLib.js +177 -0
- package/dist/lib/multi/multiBoltLib.d.ts +4 -0
- package/dist/lib/multi/multiBoltLib.js +473 -0
- package/dist/lib/scanner/fingerprints.d.ts +25 -0
- package/dist/lib/scanner/fingerprints.js +95 -0
- package/dist/lib/scanner/verifyEvents.d.ts +37 -0
- package/dist/lib/scanner/verifyEvents.js +294 -0
- package/dist/lib/single/singleAncestor.d.ts +8 -0
- package/dist/lib/single/singleAncestor.js +72 -0
- package/dist/lib/single/singleSpend.d.ts +31 -0
- package/dist/lib/single/singleSpend.js +110 -0
- package/dist/tokens/BOLT.d.ts +17 -0
- package/dist/tokens/BOLT.js +9 -0
- package/dist/tokens/MultiBOLT.d.ts +44 -0
- package/dist/tokens/MultiBOLT.js +465 -0
- package/dist/tokens/templates/MinSimple.sx.template.d.ts +23 -0
- package/dist/tokens/templates/MinSimple.sx.template.js +57 -0
- package/dist/tokens/templates/MinSimpleBalance.sx.template.d.ts +24 -0
- package/dist/tokens/templates/MinSimpleBalance.sx.template.js +59 -0
- package/dist/tokens/templates/MinSimpleDiscount.sx.template.d.ts +23 -0
- package/dist/tokens/templates/MinSimpleDiscount.sx.template.js +58 -0
- package/dist/tokens/templates/SimpleMulti.sx.template.d.ts +19 -0
- package/dist/tokens/templates/SimpleMulti.sx.template.js +214 -0
- package/dist/tokens/templates/pay2Proof.d.ts +8 -0
- package/dist/tokens/templates/pay2Proof.js +59 -0
- package/package.json +58 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { Transaction, P2PKH, Hash, Utils } from "@bsv/sdk";
|
|
2
|
+
import SimpleMultiTemplate from "./templates/SimpleMulti.sx.template.js";
|
|
3
|
+
import Pay2ProofTemplate from "./templates/pay2Proof.js";
|
|
4
|
+
import { verifyTx, buildOutpoint } from "../lib/boltLib.js";
|
|
5
|
+
import { BOLT } from "./BOLT.js";
|
|
6
|
+
// SimpleMultiBolt: optimised fungible token (16-byte balance, mandatory change + funding).
|
|
7
|
+
// The runtime lock/unlock/melt assembler is in templates/SimpleMulti.sx.template.ts.
|
|
8
|
+
export class SimpleMultiBOLT extends BOLT {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.skipVerify = false;
|
|
12
|
+
this.verifier = 'bsv';
|
|
13
|
+
this.balance = [];
|
|
14
|
+
this.balanceCommit = new Array(16).fill(0x00);
|
|
15
|
+
this.outputIndexN = [0x00];
|
|
16
|
+
this.createTransferInputs = (toPrivKey, _miscData, isCommitTx = true, forceNoChange = false, fundOverride = undefined, forceNoFund = false) => {
|
|
17
|
+
// Determine ancestor proof vout for settles
|
|
18
|
+
const hasAncestor = !isCommitTx && this.prevTxs.length >= 3;
|
|
19
|
+
let proofVout = 1;
|
|
20
|
+
if (hasAncestor) {
|
|
21
|
+
const ancestorTx = this.prevTxs[this.prevTxs.length - 3];
|
|
22
|
+
proofVout = ancestorTx.outputs.length >= 5 ? 2 : 1;
|
|
23
|
+
}
|
|
24
|
+
const input = {
|
|
25
|
+
sourceTransaction: this.tx,
|
|
26
|
+
sourceOutputIndex: this.voutIdx,
|
|
27
|
+
unlockingScriptTemplate: new SimpleMultiTemplate().unlock(this.privKey, toPrivKey.toPublicKey().encode(true), this.prevTxs, forceNoChange, forceNoFund, [], // nextBalanceCommit empty for transfer (sim sets none); not 16 zeros
|
|
28
|
+
isCommitTx ? [0x21] : [0x20], // nextTxoType
|
|
29
|
+
[0x00], // inputIndexN
|
|
30
|
+
[], // pubKeyHash2
|
|
31
|
+
hasAncestor ? this.voutLE(proofVout) : []),
|
|
32
|
+
sequence: 0xffffffff,
|
|
33
|
+
};
|
|
34
|
+
const funding = fundOverride || {
|
|
35
|
+
sourceTransaction: this.tx,
|
|
36
|
+
sourceOutputIndex: this.tx?.outputs ? this.tx?.outputs.length - 1 : 0,
|
|
37
|
+
unlockingScriptTemplate: new P2PKH().unlock(this.privKey),
|
|
38
|
+
sequence: 0xffffffff,
|
|
39
|
+
};
|
|
40
|
+
if (hasAncestor) {
|
|
41
|
+
const ancestorTx = this.prevTxs[this.prevTxs.length - 3];
|
|
42
|
+
const proof = {
|
|
43
|
+
sourceTransaction: ancestorTx,
|
|
44
|
+
sourceOutputIndex: proofVout,
|
|
45
|
+
unlockingScriptTemplate: new Pay2ProofTemplate().unlock(this.privKey),
|
|
46
|
+
sequence: 0xffffffff,
|
|
47
|
+
};
|
|
48
|
+
return forceNoFund ? [input, proof] : [input, proof, funding];
|
|
49
|
+
}
|
|
50
|
+
return forceNoFund ? [input] : [input, funding];
|
|
51
|
+
};
|
|
52
|
+
this.createTransferOutputs = (toPrivKey, isCommitTx = true, forceNoChange = false, customChangeScript) => {
|
|
53
|
+
const toPubKeyHash = Hash.hash160(toPrivKey.toPublicKey().encode(true));
|
|
54
|
+
const pubKeyHashCommit = isCommitTx
|
|
55
|
+
? toPubKeyHash
|
|
56
|
+
: new Array(20).fill(0x00);
|
|
57
|
+
const tokenLocking = new SimpleMultiTemplate().lock(isCommitTx
|
|
58
|
+
? this.pubKey
|
|
59
|
+
: toPrivKey.toPublicKey().encode(true), this.prevTxs, this.balance, this.balanceCommit, pubKeyHashCommit, new Array(20).fill(0x00), // pubKeyHashCommit2
|
|
60
|
+
new Array(36).fill(0x00), // otherGrandparentOutpoint
|
|
61
|
+
isCommitTx ? [0x21] : [0x20], // txoType
|
|
62
|
+
[0x00], // outputIndexN
|
|
63
|
+
isCommitTx ? (this.voutIdx || 0) : 0);
|
|
64
|
+
const tokenOut = { lockingScript: tokenLocking, satoshis: 1 };
|
|
65
|
+
const proofOut = {
|
|
66
|
+
lockingScript: new Pay2ProofTemplate().lock(pubKeyHashCommit),
|
|
67
|
+
satoshis: 1,
|
|
68
|
+
change: false,
|
|
69
|
+
};
|
|
70
|
+
const changeOut = {
|
|
71
|
+
change: true,
|
|
72
|
+
lockingScript: customChangeScript || new P2PKH().lock(isCommitTx ? this.pubKeyHash : toPubKeyHash),
|
|
73
|
+
};
|
|
74
|
+
if (!isCommitTx)
|
|
75
|
+
return (forceNoChange ? [tokenOut] : [tokenOut, changeOut]);
|
|
76
|
+
return (forceNoChange ? [tokenOut, proofOut] : [tokenOut, proofOut, changeOut]);
|
|
77
|
+
};
|
|
78
|
+
this.signAndClean = async (tx, skipFee = false) => {
|
|
79
|
+
if (!skipFee)
|
|
80
|
+
await tx.fee(0);
|
|
81
|
+
await tx.sign();
|
|
82
|
+
const cleanTx = Transaction.fromHex(tx.toHex());
|
|
83
|
+
tx.inputs.forEach((input, i) => {
|
|
84
|
+
cleanTx.inputs[i].sourceTransaction = input.sourceTransaction;
|
|
85
|
+
});
|
|
86
|
+
return cleanTx;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async mint(privKey, sourceTransaction, _mintData = "", balance = [0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) {
|
|
90
|
+
const pubKey = privKey.toPublicKey().encode(true);
|
|
91
|
+
const pubKeyHashStr = Utils.toHex(Hash.hash160(pubKey));
|
|
92
|
+
const version = 2;
|
|
93
|
+
let sourceOutputIndex = -1;
|
|
94
|
+
const findPKHVoutIdx = (sourceTransaction) => {
|
|
95
|
+
for (let idx = 0; idx < sourceTransaction.outputs.length; idx++) {
|
|
96
|
+
const output = sourceTransaction.outputs[idx];
|
|
97
|
+
const lockingScript = output.lockingScript;
|
|
98
|
+
const pKHChunkStr = Utils.toHex(lockingScript.chunks[2].data);
|
|
99
|
+
if (pKHChunkStr === pubKeyHashStr) {
|
|
100
|
+
sourceOutputIndex = idx;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
findPKHVoutIdx(sourceTransaction);
|
|
106
|
+
const input = {
|
|
107
|
+
sourceTransaction,
|
|
108
|
+
sourceOutputIndex,
|
|
109
|
+
unlockingScriptTemplate: new P2PKH().unlock(privKey),
|
|
110
|
+
sequence: 0xffffffff,
|
|
111
|
+
};
|
|
112
|
+
this.pubKeyHash = Hash.hash160(pubKey);
|
|
113
|
+
this.balance = balance;
|
|
114
|
+
const tokenLocking = new SimpleMultiTemplate().lock(pubKey, this.prevTxs, this.balance, new Array(16).fill(0x00), // balanceCommit = zeros
|
|
115
|
+
new Array(20).fill(0x00), // pubKeyHashCommit
|
|
116
|
+
new Array(20).fill(0x00), // pubKeyHashCommit2
|
|
117
|
+
new Array(36).fill(0x00), // otherGrandparentOutpoint
|
|
118
|
+
[0x20], // txoType = settle (genesis)
|
|
119
|
+
[0x00]);
|
|
120
|
+
const tokenOut = {
|
|
121
|
+
lockingScript: tokenLocking,
|
|
122
|
+
satoshis: 1,
|
|
123
|
+
change: false,
|
|
124
|
+
};
|
|
125
|
+
const mintTx = new Transaction(version, [input], [
|
|
126
|
+
tokenOut,
|
|
127
|
+
{
|
|
128
|
+
change: true,
|
|
129
|
+
lockingScript: new P2PKH().lock(this.pubKeyHash),
|
|
130
|
+
},
|
|
131
|
+
]);
|
|
132
|
+
await mintTx.fee(0);
|
|
133
|
+
await mintTx.sign();
|
|
134
|
+
let { valid } = verifyTx(mintTx);
|
|
135
|
+
if (!valid)
|
|
136
|
+
throw new Error("Mint tx not valid");
|
|
137
|
+
this.tx = mintTx;
|
|
138
|
+
this.voutIdx = 0;
|
|
139
|
+
this.prevTxs.push(this.tx);
|
|
140
|
+
this.pubKey = pubKey;
|
|
141
|
+
this.issuerPubKey = pubKey;
|
|
142
|
+
this.genesisOutpoint = buildOutpoint(mintTx, 0);
|
|
143
|
+
this.privKey = privKey;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
async commit(toPrivKey, commitTxMiscData = "Bolt Protocol Transfer Commit Transaction Miscellaneous Data", forceNoChange = false, fundOverride = undefined, forceNoFund = false, customChangeScript) {
|
|
147
|
+
const version = 2;
|
|
148
|
+
const inputs = this.createTransferInputs(toPrivKey, commitTxMiscData, true, forceNoChange, fundOverride, forceNoFund);
|
|
149
|
+
const outputs = this.createTransferOutputs(toPrivKey, true, forceNoChange, customChangeScript);
|
|
150
|
+
const commitTx = new Transaction(version, inputs, outputs);
|
|
151
|
+
if (!forceNoChange)
|
|
152
|
+
await commitTx.fee(0);
|
|
153
|
+
await commitTx.sign();
|
|
154
|
+
const signedHex = commitTx.toHex();
|
|
155
|
+
this.tx = Transaction.fromHex(signedHex);
|
|
156
|
+
commitTx.inputs.forEach((input, i) => {
|
|
157
|
+
this.tx.inputs[i].sourceTransaction = input.sourceTransaction;
|
|
158
|
+
});
|
|
159
|
+
this.verifyAndLogTransaction(this.tx, 'COMMIT TX', fundOverride?.sourceTransaction);
|
|
160
|
+
this.voutIdx = 0; // commit always places token at vout 0
|
|
161
|
+
this.prevTxs?.push(this.tx);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
async settle(toPrivKey, settleTxMiscData = "Bolt Protocol Transfer Settle Transaction Miscellaneous Data", forceNoChange = false, fundOverride = undefined, forceNoFund = false, customChangeScript) {
|
|
165
|
+
const version = 2;
|
|
166
|
+
const settleOutputs = this.createTransferOutputs(toPrivKey, false, forceNoChange, customChangeScript);
|
|
167
|
+
const settleInputs = this.createTransferInputs(toPrivKey, settleTxMiscData, false, forceNoChange, fundOverride, forceNoFund);
|
|
168
|
+
const settleTx = new Transaction(version, settleInputs, settleOutputs);
|
|
169
|
+
if (!forceNoChange)
|
|
170
|
+
await settleTx.fee(0);
|
|
171
|
+
await settleTx.sign();
|
|
172
|
+
const signedHex = settleTx.toHex();
|
|
173
|
+
this.tx = Transaction.fromHex(signedHex);
|
|
174
|
+
settleTx.inputs.forEach((input, i) => {
|
|
175
|
+
this.tx.inputs[i].sourceTransaction = input.sourceTransaction;
|
|
176
|
+
});
|
|
177
|
+
this.verifyAndLogTransaction(this.tx, 'SETTLE TX', fundOverride?.sourceTransaction);
|
|
178
|
+
this.prevTxs?.push(this.tx);
|
|
179
|
+
this.privKey = toPrivKey;
|
|
180
|
+
this.pubKey = toPrivKey.toPublicKey().encode(true);
|
|
181
|
+
this.pubKeyHash = Hash.hash160(this.pubKey);
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
async transfer(toPrivKey, commitTxMiscData = "Bolt Protocol Transfer Commit Transaction Miscellaneous Data", settleTxMiscData = "Bolt Protocol Transfer Settle Transaction Miscellaneous Data", skipSettle = false, forceNoChange = false, fundOverride = undefined, forceNoFund = false, customChangeScript) {
|
|
185
|
+
await this.commit(toPrivKey, commitTxMiscData, forceNoChange, fundOverride, forceNoFund, customChangeScript);
|
|
186
|
+
if (!skipSettle) {
|
|
187
|
+
await this.settle(toPrivKey, settleTxMiscData, forceNoChange, fundOverride, forceNoFund, customChangeScript);
|
|
188
|
+
}
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
// Read 16 LE bytes into a BigInt as low + (high << 64) (Buffer has no 128-bit read)
|
|
192
|
+
balanceToBigInt(b) {
|
|
193
|
+
const buf = Buffer.from(b.slice(0, 16));
|
|
194
|
+
const low = buf.readBigUInt64LE(0);
|
|
195
|
+
const high = buf.readBigUInt64LE(8);
|
|
196
|
+
return low + (high << BigInt(64));
|
|
197
|
+
}
|
|
198
|
+
// Write a BigInt back to 16 LE bytes (wrap to 128 bits)
|
|
199
|
+
bigIntToBalance(v) {
|
|
200
|
+
const mask64 = (BigInt(1) << BigInt(64)) - BigInt(1);
|
|
201
|
+
const x = v & ((BigInt(1) << BigInt(128)) - BigInt(1));
|
|
202
|
+
const buf = Buffer.alloc(16);
|
|
203
|
+
buf.writeBigUInt64LE(x & mask64, 0);
|
|
204
|
+
buf.writeBigUInt64LE((x >> BigInt(64)) & mask64, 8);
|
|
205
|
+
return Array.from(buf);
|
|
206
|
+
}
|
|
207
|
+
// Add two 16-byte LE balance values
|
|
208
|
+
addBalances(a, b) {
|
|
209
|
+
return this.bigIntToBalance(this.balanceToBigInt(a) + this.balanceToBigInt(b));
|
|
210
|
+
}
|
|
211
|
+
// Subtract two 16-byte LE balance values (a - b)
|
|
212
|
+
subtractBalances(a, b) {
|
|
213
|
+
return this.bigIntToBalance(this.balanceToBigInt(a) - this.balanceToBigInt(b));
|
|
214
|
+
}
|
|
215
|
+
// Find proof vout that matches a key's pubKeyHash in an ancestor commit tx
|
|
216
|
+
findProofVout(ancestorTx, key) {
|
|
217
|
+
const pkh = Utils.toHex(Hash.hash160(key.toPublicKey().encode(true)));
|
|
218
|
+
const startIdx = ancestorTx.outputs.length >= 5 ? 2 : 1;
|
|
219
|
+
for (let i = startIdx; i < ancestorTx.outputs.length - 1; i++) {
|
|
220
|
+
const proofChunks = ancestorTx.outputs[i].lockingScript.chunks;
|
|
221
|
+
if (proofChunks.length >= 5 && Utils.toHex(proofChunks[4]?.data || []) === pkh)
|
|
222
|
+
return i;
|
|
223
|
+
}
|
|
224
|
+
return startIdx;
|
|
225
|
+
}
|
|
226
|
+
// 4-byte LE uint32 for vout indices used in outpoint construction
|
|
227
|
+
voutLE(n) {
|
|
228
|
+
const buf = new ArrayBuffer(4);
|
|
229
|
+
new DataView(buf).setUint32(0, n, true);
|
|
230
|
+
return Array.from(new Uint8Array(buf));
|
|
231
|
+
}
|
|
232
|
+
// Get the parent outpoint from a token's locking script (SMB lock chunk 8)
|
|
233
|
+
getParentOutpoint() {
|
|
234
|
+
return this.tx.outputs[this.voutIdx].lockingScript.chunks[8].data;
|
|
235
|
+
}
|
|
236
|
+
// Merge: absorb other token into this one
|
|
237
|
+
async merge(other, toKey, fundingSource) {
|
|
238
|
+
const tpl = new SimpleMultiTemplate();
|
|
239
|
+
const proofTpl = new Pay2ProofTemplate();
|
|
240
|
+
const version = 2;
|
|
241
|
+
const toPubKeyHash = Hash.hash160(toKey.toPublicKey().encode(true));
|
|
242
|
+
// Default funding: use the last output of whichever token's tx has a change output we can unlock
|
|
243
|
+
const fundTx = fundingSource?.tx || this.tx;
|
|
244
|
+
const fundVout = fundingSource?.vout ?? (this.tx.outputs.length - 1);
|
|
245
|
+
const fundKey = fundingSource?.key || this.privKey;
|
|
246
|
+
// ── Merge Commit ──
|
|
247
|
+
const thisInput = {
|
|
248
|
+
sourceTransaction: this.tx,
|
|
249
|
+
sourceOutputIndex: this.voutIdx,
|
|
250
|
+
unlockingScriptTemplate: tpl.unlock(this.privKey, toKey.toPublicKey().encode(true), this.prevTxs, false, false, other.balance, // nextBalanceCommit = other's balance
|
|
251
|
+
[0x25], // merge commit
|
|
252
|
+
[0x00], [], // pubKeyHash2
|
|
253
|
+
[], [], // no proof vout idx for commit
|
|
254
|
+
[], // interopPubKeyHash
|
|
255
|
+
buildOutpoint(other.tx, other.voutIdx), other.getParentOutpoint()),
|
|
256
|
+
sequence: 0xffffffff,
|
|
257
|
+
};
|
|
258
|
+
const otherInput = {
|
|
259
|
+
sourceTransaction: other.tx,
|
|
260
|
+
sourceOutputIndex: other.voutIdx,
|
|
261
|
+
unlockingScriptTemplate: tpl.unlock(other.privKey, toKey.toPublicKey().encode(true), other.prevTxs, false, false, this.balance, [0x25], [0x01], [], [], [], Hash.hash160(this.pubKey), buildOutpoint(this.tx, this.voutIdx), this.getParentOutpoint()),
|
|
262
|
+
sequence: 0xffffffff,
|
|
263
|
+
};
|
|
264
|
+
const fundInput = {
|
|
265
|
+
sourceTransaction: fundTx,
|
|
266
|
+
sourceOutputIndex: fundVout,
|
|
267
|
+
unlockingScriptTemplate: new P2PKH().unlock(fundKey),
|
|
268
|
+
sequence: 0xffffffff,
|
|
269
|
+
};
|
|
270
|
+
// Merged token output (commit)
|
|
271
|
+
const tokenOut = tpl.lock(this.pubKey, this.prevTxs, this.balance, other.balance, toPubKeyHash, new Array(20).fill(0x00), other.getParentOutpoint(), // otherGrandparentOutpoint = other token's parentOutpoint
|
|
272
|
+
[0x25], [0x00], this.voutIdx);
|
|
273
|
+
const proofOut = { lockingScript: proofTpl.lock(toPubKeyHash), satoshis: 1, change: false };
|
|
274
|
+
const changeOut = { change: true, lockingScript: new P2PKH().lock(this.pubKeyHash) };
|
|
275
|
+
const commitTx = await this.signAndClean(new Transaction(version, [thisInput, otherInput, fundInput], [{ lockingScript: tokenOut, satoshis: 1 }, proofOut, changeOut]));
|
|
276
|
+
this.verifyAndLogTransaction(commitTx, 'MERGE COMMIT TX', fundingSource?.tx, other.prevTxs);
|
|
277
|
+
this.prevTxs.push(commitTx);
|
|
278
|
+
other.prevTxs.push(commitTx);
|
|
279
|
+
// ── Merge Settle ──
|
|
280
|
+
// Ancestor commits at prevTxs[txIdx - 3] for each lineage
|
|
281
|
+
const thisAncestorCommit = this.prevTxs[this.prevTxs.length - 3];
|
|
282
|
+
const otherAncestorCommit = other.prevTxs[other.prevTxs.length - 3];
|
|
283
|
+
const thisProofVout = this.findProofVout(thisAncestorCommit, this.privKey);
|
|
284
|
+
const otherProofVout = this.findProofVout(otherAncestorCommit, other.privKey);
|
|
285
|
+
const settleInput = {
|
|
286
|
+
sourceTransaction: commitTx,
|
|
287
|
+
sourceOutputIndex: 0,
|
|
288
|
+
unlockingScriptTemplate: tpl.unlock(this.privKey, toKey.toPublicKey().encode(true), this.prevTxs, false, false, new Array(16).fill(0x00), [0x24], // merge settle
|
|
289
|
+
[0x00], [], this.voutLE(thisProofVout), // grandparentProofVoutIdx
|
|
290
|
+
this.voutLE(otherProofVout), // interopProofVoutIdx
|
|
291
|
+
[], [], [], otherAncestorCommit),
|
|
292
|
+
sequence: 0xffffffff,
|
|
293
|
+
};
|
|
294
|
+
const proof0 = {
|
|
295
|
+
sourceTransaction: thisAncestorCommit,
|
|
296
|
+
sourceOutputIndex: thisProofVout,
|
|
297
|
+
unlockingScriptTemplate: proofTpl.unlock(this.privKey),
|
|
298
|
+
sequence: 0xffffffff,
|
|
299
|
+
};
|
|
300
|
+
const proof1 = {
|
|
301
|
+
sourceTransaction: otherAncestorCommit,
|
|
302
|
+
sourceOutputIndex: otherProofVout,
|
|
303
|
+
unlockingScriptTemplate: proofTpl.unlock(other.privKey),
|
|
304
|
+
sequence: 0xffffffff,
|
|
305
|
+
};
|
|
306
|
+
const settleFundInput = {
|
|
307
|
+
sourceTransaction: commitTx,
|
|
308
|
+
sourceOutputIndex: commitTx.outputs.length - 1,
|
|
309
|
+
unlockingScriptTemplate: new P2PKH().unlock(this.privKey),
|
|
310
|
+
sequence: 0xffffffff,
|
|
311
|
+
};
|
|
312
|
+
// Merged balance = sum of both tokens' balances (16-byte LE addition)
|
|
313
|
+
const mergedBalance = this.addBalances(this.balance, other.balance);
|
|
314
|
+
const settleTokenOut = tpl.lock(toKey.toPublicKey().encode(true), this.prevTxs, mergedBalance, new Array(16).fill(0x00), new Array(20).fill(0x00), new Array(20).fill(0x00), new Array(36).fill(0x00), [0x24], [0x00], 0);
|
|
315
|
+
// Lock change to the new owner's key so the merged token can fund subsequent operations
|
|
316
|
+
const mergeSettleChangePKH = Hash.hash160(toKey.toPublicKey().encode(true));
|
|
317
|
+
const settleChangeOut = { change: true, lockingScript: new P2PKH().lock(mergeSettleChangePKH) };
|
|
318
|
+
const settleTx = await this.signAndClean(new Transaction(version, [settleInput, proof0, proof1, settleFundInput], [{ lockingScript: settleTokenOut, satoshis: 1 }, settleChangeOut]));
|
|
319
|
+
this.verifyAndLogTransaction(settleTx, 'MERGE SETTLE TX', undefined, other.prevTxs);
|
|
320
|
+
this.tx = settleTx;
|
|
321
|
+
this.voutIdx = 0;
|
|
322
|
+
this.prevTxs.push(settleTx);
|
|
323
|
+
this.privKey = toKey;
|
|
324
|
+
this.pubKey = toKey.toPublicKey().encode(true);
|
|
325
|
+
this.pubKeyHash = Hash.hash160(this.pubKey);
|
|
326
|
+
this.balance = mergedBalance;
|
|
327
|
+
this.balanceCommit = new Array(16).fill(0x00);
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
// Split: divide this token into two with specified balances
|
|
331
|
+
// Returns [tokenA, tokenB]
|
|
332
|
+
async split(toKeyA, toKeyB, splitBalanceCommit, fundingSource) {
|
|
333
|
+
const tpl = new SimpleMultiTemplate();
|
|
334
|
+
const proofTpl = new Pay2ProofTemplate();
|
|
335
|
+
const version = 2;
|
|
336
|
+
const toPubKeyHashA = Hash.hash160(toKeyA.toPublicKey().encode(true));
|
|
337
|
+
const toPubKeyHashB = Hash.hash160(toKeyB.toPublicKey().encode(true));
|
|
338
|
+
const fundTx = fundingSource?.tx || this.tx;
|
|
339
|
+
const fundVout = fundingSource?.vout ?? (this.tx.outputs.length - 1);
|
|
340
|
+
const fundKey = fundingSource?.key || this.privKey;
|
|
341
|
+
// ── Split Commit ──
|
|
342
|
+
const tokenInput = {
|
|
343
|
+
sourceTransaction: this.tx,
|
|
344
|
+
sourceOutputIndex: this.voutIdx,
|
|
345
|
+
unlockingScriptTemplate: tpl.unlock(this.privKey, toKeyA.toPublicKey().encode(true), this.prevTxs, false, false, splitBalanceCommit, // nextBalanceCommit = second split's balance
|
|
346
|
+
[0x23], // split commit
|
|
347
|
+
[0x00], toPubKeyHashB),
|
|
348
|
+
sequence: 0xffffffff,
|
|
349
|
+
};
|
|
350
|
+
const fundInput = {
|
|
351
|
+
sourceTransaction: fundTx,
|
|
352
|
+
sourceOutputIndex: fundVout,
|
|
353
|
+
unlockingScriptTemplate: new P2PKH().unlock(fundKey),
|
|
354
|
+
sequence: 0xffffffff,
|
|
355
|
+
};
|
|
356
|
+
const tokenOut = tpl.lock(this.pubKey, this.prevTxs, this.balance, splitBalanceCommit, toPubKeyHashA, toPubKeyHashB, new Array(36).fill(0x00), [0x23], [0x00], this.voutIdx);
|
|
357
|
+
const proofOut0 = { lockingScript: proofTpl.lock(toPubKeyHashA), satoshis: 1, change: false };
|
|
358
|
+
const proofOut1 = { lockingScript: proofTpl.lock(toPubKeyHashB), satoshis: 1, change: false };
|
|
359
|
+
const changeOut = { change: true, lockingScript: new P2PKH().lock(this.pubKeyHash) };
|
|
360
|
+
const commitTx = await this.signAndClean(new Transaction(version, [tokenInput, fundInput], [{ lockingScript: tokenOut, satoshis: 1 }, proofOut0, proofOut1, changeOut]));
|
|
361
|
+
this.verifyAndLogTransaction(commitTx, 'SPLIT COMMIT TX', fundingSource?.tx);
|
|
362
|
+
this.prevTxs.push(commitTx);
|
|
363
|
+
// ── Split Settle ──
|
|
364
|
+
const ancestorCommit = this.prevTxs[this.prevTxs.length - 3]; // the commit before the split commit
|
|
365
|
+
const ancestorProofVout = ancestorCommit.outputs.length >= 5 ? 2 : 1;
|
|
366
|
+
const settleInput = {
|
|
367
|
+
sourceTransaction: commitTx,
|
|
368
|
+
sourceOutputIndex: 0,
|
|
369
|
+
unlockingScriptTemplate: tpl.unlock(this.privKey, [], // toPubKey empty -> pubKeyHash1 empty (recipients from commitments)
|
|
370
|
+
this.prevTxs, false, false, [], // nextBalanceCommit empty for split settle
|
|
371
|
+
[0x22], // split settle
|
|
372
|
+
[0x00], [], this.voutLE(ancestorProofVout)),
|
|
373
|
+
sequence: 0xffffffff,
|
|
374
|
+
};
|
|
375
|
+
const proofInput = {
|
|
376
|
+
sourceTransaction: ancestorCommit,
|
|
377
|
+
sourceOutputIndex: ancestorProofVout,
|
|
378
|
+
unlockingScriptTemplate: proofTpl.unlock(this.privKey),
|
|
379
|
+
sequence: 0xffffffff,
|
|
380
|
+
};
|
|
381
|
+
const settleFundInput = {
|
|
382
|
+
sourceTransaction: commitTx,
|
|
383
|
+
sourceOutputIndex: commitTx.outputs.length - 1,
|
|
384
|
+
unlockingScriptTemplate: new P2PKH().unlock(this.privKey),
|
|
385
|
+
sequence: 0xffffffff,
|
|
386
|
+
};
|
|
387
|
+
// Two settled token outputs — first gets balance - balanceCommit, second gets balanceCommit
|
|
388
|
+
const mainBalance = this.subtractBalances(this.balance, splitBalanceCommit);
|
|
389
|
+
const settleTokenOut0 = tpl.lock(toKeyA.toPublicKey().encode(true), this.prevTxs, mainBalance, new Array(16).fill(0x00), new Array(20).fill(0x00), new Array(20).fill(0x00), new Array(36).fill(0x00), [0x22], [0x00], 0);
|
|
390
|
+
const settleTokenOut1 = tpl.lock(toKeyB.toPublicKey().encode(true), this.prevTxs, splitBalanceCommit, new Array(16).fill(0x00), new Array(20).fill(0x00), new Array(20).fill(0x00), new Array(36).fill(0x00), [0x22], [0x01], 0);
|
|
391
|
+
// Lock change to first split recipient so the token can fund subsequent operations
|
|
392
|
+
const splitSettleChangePKH = Hash.hash160(toKeyA.toPublicKey().encode(true));
|
|
393
|
+
const settleChangeOut = { change: true, lockingScript: new P2PKH().lock(splitSettleChangePKH) };
|
|
394
|
+
const settleTx = await this.signAndClean(new Transaction(version, [settleInput, proofInput, settleFundInput], [{ lockingScript: settleTokenOut0, satoshis: 1 }, { lockingScript: settleTokenOut1, satoshis: 1 }, settleChangeOut]));
|
|
395
|
+
this.verifyAndLogTransaction(settleTx, 'SPLIT SETTLE TX');
|
|
396
|
+
// Update this token to be the first split
|
|
397
|
+
this.tx = settleTx;
|
|
398
|
+
this.voutIdx = 0;
|
|
399
|
+
this.prevTxs.push(settleTx);
|
|
400
|
+
this.privKey = toKeyA;
|
|
401
|
+
this.pubKey = toKeyA.toPublicKey().encode(true);
|
|
402
|
+
this.pubKeyHash = Hash.hash160(this.pubKey);
|
|
403
|
+
this.balance = mainBalance;
|
|
404
|
+
// Create second split token
|
|
405
|
+
const tokenB = new SimpleMultiBOLT();
|
|
406
|
+
tokenB.tx = settleTx;
|
|
407
|
+
tokenB.voutIdx = 1;
|
|
408
|
+
tokenB.prevTxs = [...this.prevTxs]; // share lineage
|
|
409
|
+
tokenB.privKey = toKeyB;
|
|
410
|
+
tokenB.pubKey = toKeyB.toPublicKey().encode(true);
|
|
411
|
+
tokenB.pubKeyHash = Hash.hash160(tokenB.pubKey);
|
|
412
|
+
tokenB.issuerPubKey = this.issuerPubKey;
|
|
413
|
+
tokenB.genesisOutpoint = this.genesisOutpoint;
|
|
414
|
+
tokenB.balance = splitBalanceCommit;
|
|
415
|
+
tokenB.skipVerify = this.skipVerify;
|
|
416
|
+
tokenB.verifier = this.verifier;
|
|
417
|
+
return [this, tokenB];
|
|
418
|
+
}
|
|
419
|
+
async melt(meltPubKeyHash) {
|
|
420
|
+
const input = {
|
|
421
|
+
sourceTransaction: this.tx,
|
|
422
|
+
sourceOutputIndex: this.voutIdx,
|
|
423
|
+
unlockingScriptTemplate: new SimpleMultiTemplate().melt(this.privKey),
|
|
424
|
+
sequence: 0xffffffff,
|
|
425
|
+
};
|
|
426
|
+
const funding = {
|
|
427
|
+
sourceTransaction: this.tx,
|
|
428
|
+
sourceOutputIndex: this.tx?.outputs ? this.tx?.outputs.length - 1 : 0,
|
|
429
|
+
unlockingScriptTemplate: new P2PKH().unlock(this.privKey),
|
|
430
|
+
sequence: 0xffffffff,
|
|
431
|
+
};
|
|
432
|
+
const version = 2;
|
|
433
|
+
const meltTx = new Transaction(version, [input, funding], [
|
|
434
|
+
{
|
|
435
|
+
change: true,
|
|
436
|
+
lockingScript: new P2PKH().lock(meltPubKeyHash || this.pubKeyHash),
|
|
437
|
+
},
|
|
438
|
+
]);
|
|
439
|
+
await meltTx.fee(0);
|
|
440
|
+
await meltTx.sign();
|
|
441
|
+
const signedHex = meltTx.toHex();
|
|
442
|
+
this.tx = Transaction.fromHex(signedHex);
|
|
443
|
+
meltTx.inputs.forEach((input, i) => {
|
|
444
|
+
this.tx.inputs[i].sourceTransaction = input.sourceTransaction;
|
|
445
|
+
});
|
|
446
|
+
this.verifyAndLogTransaction(this.tx, 'MELT TX');
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
verifyAndLogTransaction(tx, txType, fundOverrideTx = undefined, extraHistory = []) {
|
|
450
|
+
if (this.skipVerify)
|
|
451
|
+
return;
|
|
452
|
+
// Ensure all inputs have sourceTXID set
|
|
453
|
+
tx.inputs.forEach(input => {
|
|
454
|
+
if (!input.sourceTXID && input.sourceTransaction) {
|
|
455
|
+
input.sourceTXID = input.sourceTransaction.id('hex');
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
const { valid, scriptExecutions } = verifyTx(tx, true); // skipOutputCheck: zero-funding txs may have output > input
|
|
459
|
+
if (!valid) {
|
|
460
|
+
const failedIdx = scriptExecutions.findIndex(e => !e.valid);
|
|
461
|
+
console.log(`\n========== SCRIPT FAILURE [bsv] (${txType}) - INPUT ${failedIdx} ==========`);
|
|
462
|
+
throw new Error(`${txType} tx not valid [bsv]`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ScriptTemplate, LockingScript, Script, PrivateKey, Transaction, UnlockingScript } from "@bsv/sdk";
|
|
2
|
+
export default class MinSimpleTemplate implements ScriptTemplate {
|
|
3
|
+
private readonly UNLOCK_SCRIPT_SUFFIX;
|
|
4
|
+
private readonly LOCK_SCRIPT_SUFFIX;
|
|
5
|
+
/**
|
|
6
|
+
* Build a MinSimpleBolt locking script from its 6 contract args.
|
|
7
|
+
* For a genesis identity mint: pubKeyHash = recipient, issuerPubKey = issuer,
|
|
8
|
+
* pubKeyHashCommitment = zeros[20], txoType = [0x00], parent/grandparent = zeros[36].
|
|
9
|
+
*/
|
|
10
|
+
lock(pubKeyHash: number[], issuerPubKey: number[], pubKeyHashCommitment?: number[], txoType?: number[], parentOutpoint?: number[], grandparentOutpoint?: number[]): LockingScript;
|
|
11
|
+
/** The static contract suffix (everything after the 6 data pushes), as a Script. */
|
|
12
|
+
staticSuffix(): Script;
|
|
13
|
+
/**
|
|
14
|
+
* Spend (transfer commit/settle) this identity bolt. The 37-arg unlock layout is contract-agnostic
|
|
15
|
+
* across the NFT family, so it delegates to singleSpendUnlock. Requires tx version >= 2.
|
|
16
|
+
* @param beneficiaryPubKeyHash the next owner's 20-byte pkh
|
|
17
|
+
* @param prevTxs prior lineage txs (mint, commit, ...); a back-reaching settle (>=4 txs) is B2b-2
|
|
18
|
+
*/
|
|
19
|
+
unlock(privateKey: PrivateKey, beneficiaryPubKeyHash: number[], prevTxs?: Transaction[], forceNoChange?: boolean, forceNoFund?: boolean): {
|
|
20
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
21
|
+
estimateLength: () => Promise<number>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// MinSimple.sx.template.ts
|
|
2
|
+
// Runtime LOCK assembler for the MinSimpleBolt identity NFT (no value field).
|
|
3
|
+
// LOCK_SCRIPT_SUFFIX is auto-patched from the pre-compiled artifact by
|
|
4
|
+
// scripts/build-min-simple-bolt.mjs (build-time only) — do NOT hand-edit it.
|
|
5
|
+
//
|
|
6
|
+
// Lock layout (6 dynamic data args, then the static contract suffix):
|
|
7
|
+
// pubKeyHash[20] · pubKeyHashCommitment[20] · txoType[1] · parentOutpoint[36] ·
|
|
8
|
+
// grandparentOutpoint[36] · issuerPubKey[33] + LOCK_SCRIPT_SUFFIX
|
|
9
|
+
//
|
|
10
|
+
// The spend/unlock path (transfer commit/settle, 37 args) is provided via singleSpendUnlock;
|
|
11
|
+
// the identity issuance only needs the lock.
|
|
12
|
+
import { LockingScript, Script } from "@bsv/sdk";
|
|
13
|
+
import { singleSpendUnlock } from "../../lib/single/singleSpend.js";
|
|
14
|
+
export default class MinSimpleTemplate {
|
|
15
|
+
constructor() {
|
|
16
|
+
// Static UNLOCK suffix (after the 37 data pushes), patched from the artifact by
|
|
17
|
+
// scripts/build-min-simple-bolt.mjs — do NOT hand-edit.
|
|
18
|
+
this.UNLOCK_SCRIPT_SUFFIX = "OP_5 OP_PICK OP_5 OP_ROLL OP_5 OP_ROLL OP_5 OP_PICK OP_5 OP_PICK OP_CAT OP_CAT OP_CAT OP_CAT OP_4 OP_ROLL OP_2 OP_ROLL OP_4 OP_ROLL OP_4 OP_ROLL OP_CAT OP_CAT OP_CAT OP_SWAP OP_IFDUP OP_IF 41 OP_TOALTSTACK OP_HASH256 OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT 00 OP_CAT OP_BIN2NUM OP_1 OP_ADD OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SIZE OP_SWAP OP_CAT 022079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179802 OP_SWAP OP_CAT OP_SIZE OP_SWAP OP_CAT 30 OP_SWAP OP_CAT OP_FROMALTSTACK OP_CAT 038ff83d8cf12121491609c4939dc11c4aa35503508fe432dc5a5c1905608b9218 OP_CODESEPARATOR OP_CHECKSIGVERIFY OP_ENDIF";
|
|
19
|
+
this.LOCK_SCRIPT_SUFFIX = "OP_2 OP_PICK OP_BIN2NUM OP_NOT OP_SWAP OP_2SWAP OP_DUP OP_BIN2NUM OP_0NOTEQUAL OP_DUP OP_NOT OP_6 OP_PICK OP_BIN2NUM OP_BOOLAND OP_5 OP_PICK OP_BOOLOR OP_IF OP_3 OP_PICK OP_10 OP_PICK OP_EQUALVERIFY OP_ENDIF OP_8 OP_PICK OP_IFDUP OP_IF OP_DUP 41 OP_TOALTSTACK OP_HASH256 OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_1 OP_SPLIT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT 00 OP_CAT OP_BIN2NUM OP_1 OP_ADD OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_DUP OP_0NOTEQUAL OP_SPLIT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SWAP OP_CAT OP_SIZE OP_SWAP OP_CAT 022079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179802 OP_SWAP OP_CAT OP_SIZE OP_SWAP OP_CAT 30 OP_SWAP OP_CAT OP_FROMALTSTACK OP_CAT 038ff83d8cf12121491609c4939dc11c4aa35503508fe432dc5a5c1905608b9218 OP_CHECKSIGVERIFY OP_4 OP_SPLIT 20 OP_SPLIT 20 OP_SPLIT 24 OP_SPLIT OP_1 OP_SPLIT OP_NIP OP_2 OP_SPLIT OP_SWAP OP_BIN2NUM OP_SPLIT OP_8 OP_SPLIT OP_4 OP_SPLIT 20 OP_SPLIT OP_4 OP_SPLIT OP_1 OP_SPLIT OP_SWAP 41 OP_EQUALVERIFY OP_9 OP_PICK OP_2 OP_GREATERTHANOREQUAL OP_VERIFY OP_5 OP_ROLL 15 OP_SPLIT 15 OP_SPLIT OP_2 OP_SPLIT 25 OP_SPLIT 25 OP_SPLIT 22 OP_SPLIT OP_DUP OP_TOALTSTACK OP_11 OP_PICK OP_DUP OP_BIN2NUM OP_1 OP_EQUALVERIFY 16 OP_PICK OP_NOTIF 1d02b0178876a914 OP_CAT 1c OP_PICK OP_CAT 88ac OP_CAT OP_ENDIF 14 17 OP_PICK OP_NOTIF 19 OP_ELSE 18 OP_ENDIF OP_PICK OP_CAT 14 18 OP_PICK OP_NOTIF 1e OP_PICK OP_ELSE OP_0 14 OP_NUM2BIN OP_ENDIF OP_CAT OP_CAT 17 OP_PICK OP_IF 0100 OP_ELSE 0121 OP_ENDIF OP_CAT 24 OP_15 OP_PICK OP_CAT OP_CAT OP_5 OP_PICK OP_CAT 16 OP_PICK OP_IF 21 1c OP_PICK OP_CAT OP_ELSE OP_3 OP_PICK OP_ENDIF OP_CAT OP_2 OP_PICK OP_CAT fdf104 OP_DUP OP_TOALTSTACK OP_14 OP_PICK OP_SWAP OP_CAT OP_SWAP OP_CAT 17 OP_PICK OP_NOTIF OP_SWAP OP_CAT OP_ELSE OP_NIP OP_ENDIF 1d OP_PICK OP_CAT OP_HASH256 OP_10 OP_PICK OP_EQUALVERIFY OP_12 OP_PICK 16 OP_PICK OP_BIN2NUM 12 OP_PICK OP_BOOLAND OP_IF 38 OP_PICK 2a OP_PICK OP_DROP OP_2 OP_CAT 38 OP_PICK OP_CAT OP_0 1a OP_NUM2BIN 38 OP_PICK 24 OP_SWAP OP_CAT OP_CAT 37 OP_PICK 22 OP_SWAP OP_CAT OP_CAT 36 OP_PICK 14 OP_SWAP OP_CAT OP_CAT 35 OP_PICK OP_SIZE OP_SWAP OP_CAT OP_CAT 34 OP_PICK 21 OP_SWAP OP_CAT OP_CAT 33 OP_PICK 4c68 OP_SWAP OP_CAT 33 OP_PICK 14 OP_SWAP OP_CAT 33 OP_PICK 14 OP_SWAP OP_CAT OP_CAT 32 OP_PICK 01 OP_SWAP OP_CAT OP_CAT 31 OP_PICK 24 OP_SWAP OP_CAT OP_CAT 30 OP_PICK 24 OP_SWAP OP_CAT OP_CAT OP_6 OP_PICK OP_CAT OP_5 OP_PICK OP_CAT OP_DUP ad68 OP_DUP OP_ROT OP_CAT OP_DROP 03fdf304 OP_SWAP 02 OP_SWAP OP_CAT OP_CAT OP_SWAP fdf104 03 OP_SWAP OP_CAT OP_TOALTSTACK 4df104 OP_SWAP OP_CAT OP_CAT OP_CAT OP_CAT 2d OP_PICK 34 OP_SWAP OP_CAT OP_CAT OP_FROMALTSTACK OP_CAT 5579557a557a557955797e7e7e7e547a527a547a547a7e7e7e7c73630141 OP_CAT OP_FROMALTSTACK OP_FROMALTSTACK OP_SWAP OP_TOALTSTACK 20 OP_SPLIT OP_NIP 7d01 OP_SPLIT OP_DROP OP_CAT abad68 OP_CAT OP_SIZE OP_2 OP_NUM2BIN fd OP_SWAP OP_CAT OP_SWAP OP_CAT OP_CAT 2b OP_PICK OP_CAT 2a OP_PICK OP_CAT 29 OP_PICK OP_SIZE OP_SWAP OP_CAT OP_CAT 28 OP_PICK OP_CAT OP_3 OP_CAT OP_13 OP_PICK OP_CAT OP_FROMALTSTACK OP_CAT 27 OP_PICK 14 OP_SWAP OP_CAT OP_CAT 26 OP_PICK 14 OP_SWAP OP_CAT OP_CAT 25 OP_PICK 01 OP_SWAP OP_CAT OP_CAT 24 OP_PICK 24 OP_SWAP OP_CAT OP_CAT 23 OP_PICK 24 OP_SWAP OP_CAT OP_CAT OP_3 OP_PICK OP_CAT OP_2 OP_PICK OP_CAT OP_13 OP_PICK OP_CAT 1d02b0178876a914 OP_CAT 19 OP_PICK OP_CAT 88ac OP_CAT 22 OP_PICK OP_CAT 21 OP_PICK 19 OP_SWAP OP_CAT OP_CAT 20 OP_PICK OP_CAT OP_HASH256 OP_DUP 14 OP_PICK 20 OP_SPLIT OP_DROP OP_EQUALVERIFY 01000000 OP_CAT OP_CAT OP_ELSE 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK 38 OP_PICK OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_CAT OP_SIZE OP_NIP OP_0 OP_EQUALVERIFY OP_ENDIF 1e OP_PICK OP_CAT OP_HASH256 OP_15 OP_PICK OP_EQUALVERIFY 1a OP_PICK OP_DUP OP_SIZE OP_1SUB OP_SPLIT OP_NIP 41 OP_EQUALVERIFY 1a OP_PICK OP_DUP OP_HASH160 1a OP_ELSE OP_10 OP_PICK OP_DUP OP_SIZE OP_1SUB OP_SPLIT OP_NIP 41 OP_EQUALVERIFY OP_10 OP_PICK OP_DUP OP_HASH160 OP_10 OP_ENDIF OP_PICK OP_EQUALVERIFY OP_CHECKSIG";
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Build a MinSimpleBolt locking script from its 6 contract args.
|
|
23
|
+
* For a genesis identity mint: pubKeyHash = recipient, issuerPubKey = issuer,
|
|
24
|
+
* pubKeyHashCommitment = zeros[20], txoType = [0x00], parent/grandparent = zeros[36].
|
|
25
|
+
*/
|
|
26
|
+
lock(pubKeyHash, issuerPubKey, pubKeyHashCommitment = new Array(20).fill(0x00), txoType = [0x00], parentOutpoint = new Array(36).fill(0x00), grandparentOutpoint = new Array(36).fill(0x00)) {
|
|
27
|
+
return new LockingScript([
|
|
28
|
+
...new Script().writeBin(pubKeyHash).chunks,
|
|
29
|
+
...new Script().writeBin(pubKeyHashCommitment).chunks,
|
|
30
|
+
...new Script().writeBin(txoType).chunks,
|
|
31
|
+
...new Script().writeBin(parentOutpoint).chunks,
|
|
32
|
+
...new Script().writeBin(grandparentOutpoint).chunks,
|
|
33
|
+
...new Script().writeBin(issuerPubKey).chunks,
|
|
34
|
+
...Script.fromASM(this.LOCK_SCRIPT_SUFFIX).chunks,
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
/** The static contract suffix (everything after the 6 data pushes), as a Script. */
|
|
38
|
+
staticSuffix() {
|
|
39
|
+
return Script.fromASM(this.LOCK_SCRIPT_SUFFIX);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Spend (transfer commit/settle) this identity bolt. The 37-arg unlock layout is contract-agnostic
|
|
43
|
+
* across the NFT family, so it delegates to singleSpendUnlock. Requires tx version >= 2.
|
|
44
|
+
* @param beneficiaryPubKeyHash the next owner's 20-byte pkh
|
|
45
|
+
* @param prevTxs prior lineage txs (mint, commit, ...); a back-reaching settle (>=4 txs) is B2b-2
|
|
46
|
+
*/
|
|
47
|
+
unlock(privateKey, beneficiaryPubKeyHash, prevTxs = [], forceNoChange = false, forceNoFund = false) {
|
|
48
|
+
return singleSpendUnlock({
|
|
49
|
+
privateKey,
|
|
50
|
+
beneficiaryPubKeyHash,
|
|
51
|
+
unlockScriptSuffixASM: this.UNLOCK_SCRIPT_SUFFIX,
|
|
52
|
+
prevTxs,
|
|
53
|
+
forceNoChange,
|
|
54
|
+
forceNoFund,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ScriptTemplate, LockingScript, Script, PrivateKey, Transaction, UnlockingScript } from "@bsv/sdk";
|
|
2
|
+
export default class MinSimpleBalanceTemplate implements ScriptTemplate {
|
|
3
|
+
private readonly UNLOCK_SCRIPT_SUFFIX;
|
|
4
|
+
private readonly LOCK_SCRIPT_SUFFIX;
|
|
5
|
+
/**
|
|
6
|
+
* Build a MinSimpleBalanceBolt locking script from its 7 contract args.
|
|
7
|
+
* Genesis mint: balance = 16-byte LE, pubKeyHash = recipient, issuerPubKey = issuer,
|
|
8
|
+
* pubKeyHashCommitment = zeros[20], txoType = [0x00], parent/grandparent = zeros[36].
|
|
9
|
+
*/
|
|
10
|
+
lock(balance: number[], pubKeyHash: number[], issuerPubKey: number[], pubKeyHashCommitment?: number[], txoType?: number[], parentOutpoint?: number[], grandparentOutpoint?: number[]): LockingScript;
|
|
11
|
+
/** The static contract suffix (everything after the 7 data pushes), as a Script. */
|
|
12
|
+
staticSuffix(): Script;
|
|
13
|
+
/**
|
|
14
|
+
* Spend (transfer commit/settle) this balance bolt. The balance is immutable and carried by the
|
|
15
|
+
* lock; the 37-arg unlock layout is contract-agnostic, so it delegates to singleSpendUnlock.
|
|
16
|
+
* Requires tx version >= 2.
|
|
17
|
+
* @param beneficiaryPubKeyHash the next owner's 20-byte pkh
|
|
18
|
+
* @param prevTxs prior lineage txs (mint, commit, ...); a back-reaching settle (>=4 txs) is B2b-2
|
|
19
|
+
*/
|
|
20
|
+
unlock(privateKey: PrivateKey, beneficiaryPubKeyHash: number[], prevTxs?: Transaction[], forceNoChange?: boolean, forceNoFund?: boolean): {
|
|
21
|
+
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
|
|
22
|
+
estimateLength: () => Promise<number>;
|
|
23
|
+
};
|
|
24
|
+
}
|