@neuraiproject/neurai-scripts 0.6.2 → 0.7.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/NeuraiScripts.global.js +136 -50
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/browser.js +134 -51
- package/dist/browser.js.map +1 -1
- package/dist/covenants/index.d.ts +1 -1
- package/dist/covenants/partial-fill/index.d.ts +1 -0
- package/dist/covenants/partial-fill/witness.d.ts +40 -0
- package/dist/index.cjs +136 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +134 -51
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
export { buildPartialFillScript, buildPartialFillScriptHex, buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex, parsePartialFillScript, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, parsePartialFillScriptPQ, isPartialFillScriptPQ, DEFAULT_PQ_TXHASH_SELECTOR } from './partial-fill/index.js';
|
|
1
|
+
export { buildPartialFillScript, buildPartialFillScriptHex, buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex, parsePartialFillScript, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, parsePartialFillScriptPQ, isPartialFillScriptPQ, DEFAULT_PQ_TXHASH_SELECTOR, buildFillWitnessStack, buildCancelWitnessStack, buildCancelWitnessStackPQ } from './partial-fill/index.js';
|
|
@@ -2,5 +2,6 @@ export { buildPartialFillScript, buildPartialFillScriptHex, encodeP2PKHScriptPub
|
|
|
2
2
|
export { buildPartialFillScriptPQ, buildPartialFillScriptPQHex, DEFAULT_PQ_TXHASH_SELECTOR } from './script-pq.js';
|
|
3
3
|
export { buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex } from './spend.js';
|
|
4
4
|
export { buildCancelScriptSigPQ, buildCancelScriptSigPQHex } from './spend-pq.js';
|
|
5
|
+
export { buildFillWitnessStack, buildCancelWitnessStack, buildCancelWitnessStackPQ } from './witness.js';
|
|
5
6
|
export { parsePartialFillScript } from './parse.js';
|
|
6
7
|
export { parsePartialFillScriptPQ, isPartialFillScriptPQ } from './parse-pq.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Witness-stack builders for the Partial-Fill Sell Order covenant.
|
|
3
|
+
*
|
|
4
|
+
* When the covenant lives behind an AuthScript commitment (the only
|
|
5
|
+
* asset-compatible deployment since the node's OP_XNA_ASSET placement rules
|
|
6
|
+
* — bare covenant outputs can no longer carry assets), the unlock data goes
|
|
7
|
+
* in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
|
|
8
|
+
* return a serialized script that PUSHES the elements; a witness needs the
|
|
9
|
+
* elements THEMSELVES, one per stack slot, so those blobs cannot be reused
|
|
10
|
+
* as a single `args` entry.
|
|
11
|
+
*
|
|
12
|
+
* These builders return the raw `args` stack (bottom → top), mirroring the
|
|
13
|
+
* shapes documented in `spend.ts`:
|
|
14
|
+
*
|
|
15
|
+
* Full fill: [ <1>, <0> ]
|
|
16
|
+
* Partial fill: [ <N>, <0>, <0> ]
|
|
17
|
+
* Cancel: [ <sig>, <pubkey>, <1> ]
|
|
18
|
+
*
|
|
19
|
+
* Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
|
|
20
|
+
* result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
|
|
21
|
+
* (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
|
|
22
|
+
* witness, and serialize the spending transaction with
|
|
23
|
+
* `serializeTransaction` from `@neuraiproject/neurai-create-transaction`
|
|
24
|
+
* (0.5.1+, witness elements as hex strings: `witness.map(bytesToHex)`).
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Witness `args` for the public fill branches. Same semantics and
|
|
28
|
+
* validations as `buildFillScriptSig`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function buildFillWitnessStack(amount: bigint, total: bigint): Uint8Array[];
|
|
31
|
+
/**
|
|
32
|
+
* Witness `args` for the seller's ECDSA cancel branch. Same semantics and
|
|
33
|
+
* validations as `buildCancelScriptSig`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildCancelWitnessStack(signatureDer: Uint8Array, pubKey: Uint8Array): Uint8Array[];
|
|
36
|
+
/**
|
|
37
|
+
* Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
|
|
38
|
+
* semantics and validations as `buildCancelScriptSigPQ`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function buildCancelWitnessStackPQ(sigPQ: Uint8Array, pubKey: Uint8Array): Uint8Array[];
|
package/dist/index.cjs
CHANGED
|
@@ -1120,43 +1120,43 @@ var distExports = requireDist();
|
|
|
1120
1120
|
// Makes the utils un-importable in browsers without a bundler.
|
|
1121
1121
|
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
1122
1122
|
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
1123
|
-
function isBytes(a) {
|
|
1123
|
+
function isBytes$1(a) {
|
|
1124
1124
|
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
1125
1125
|
}
|
|
1126
1126
|
/** Asserts something is Uint8Array. */
|
|
1127
|
-
function abytes(b, ...lengths) {
|
|
1128
|
-
if (!isBytes(b))
|
|
1127
|
+
function abytes$1(b, ...lengths) {
|
|
1128
|
+
if (!isBytes$1(b))
|
|
1129
1129
|
throw new Error('Uint8Array expected');
|
|
1130
1130
|
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
1131
1131
|
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
|
1132
1132
|
}
|
|
1133
1133
|
/** Asserts a hash instance has not been destroyed / finished */
|
|
1134
|
-
function aexists(instance, checkFinished = true) {
|
|
1134
|
+
function aexists$1(instance, checkFinished = true) {
|
|
1135
1135
|
if (instance.destroyed)
|
|
1136
1136
|
throw new Error('Hash instance has been destroyed');
|
|
1137
1137
|
if (checkFinished && instance.finished)
|
|
1138
1138
|
throw new Error('Hash#digest() has already been called');
|
|
1139
1139
|
}
|
|
1140
1140
|
/** Asserts output is properly-sized byte array */
|
|
1141
|
-
function aoutput(out, instance) {
|
|
1142
|
-
abytes(out);
|
|
1141
|
+
function aoutput$1(out, instance) {
|
|
1142
|
+
abytes$1(out);
|
|
1143
1143
|
const min = instance.outputLen;
|
|
1144
1144
|
if (out.length < min) {
|
|
1145
1145
|
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
1146
1146
|
}
|
|
1147
1147
|
}
|
|
1148
1148
|
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
1149
|
-
function clean(...arrays) {
|
|
1149
|
+
function clean$1(...arrays) {
|
|
1150
1150
|
for (let i = 0; i < arrays.length; i++) {
|
|
1151
1151
|
arrays[i].fill(0);
|
|
1152
1152
|
}
|
|
1153
1153
|
}
|
|
1154
1154
|
/** Create DataView of an array for easy byte-level manipulation. */
|
|
1155
|
-
function createView(arr) {
|
|
1155
|
+
function createView$1(arr) {
|
|
1156
1156
|
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1157
1157
|
}
|
|
1158
1158
|
/** The rotate right (circular right shift) operation for uint32 */
|
|
1159
|
-
function rotr(word, shift) {
|
|
1159
|
+
function rotr$1(word, shift) {
|
|
1160
1160
|
return (word << (32 - shift)) | (word >>> shift);
|
|
1161
1161
|
}
|
|
1162
1162
|
/**
|
|
@@ -1176,14 +1176,14 @@ function utf8ToBytes(str) {
|
|
|
1176
1176
|
function toBytes(data) {
|
|
1177
1177
|
if (typeof data === 'string')
|
|
1178
1178
|
data = utf8ToBytes(data);
|
|
1179
|
-
abytes(data);
|
|
1179
|
+
abytes$1(data);
|
|
1180
1180
|
return data;
|
|
1181
1181
|
}
|
|
1182
1182
|
/** For runtime check if class implements interface */
|
|
1183
1183
|
class Hash {
|
|
1184
1184
|
}
|
|
1185
1185
|
/** Wraps hash function, creating an interface on top of it */
|
|
1186
|
-
function createHasher(hashCons) {
|
|
1186
|
+
function createHasher$1(hashCons) {
|
|
1187
1187
|
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
1188
1188
|
const tmp = hashCons();
|
|
1189
1189
|
hashC.outputLen = tmp.outputLen;
|
|
@@ -1210,18 +1210,18 @@ function setBigUint64(view, byteOffset, value, isLE) {
|
|
|
1210
1210
|
view.setUint32(byteOffset + l, wl, isLE);
|
|
1211
1211
|
}
|
|
1212
1212
|
/** Choice: a ? b : c */
|
|
1213
|
-
function Chi(a, b, c) {
|
|
1213
|
+
function Chi$1(a, b, c) {
|
|
1214
1214
|
return (a & b) ^ (~a & c);
|
|
1215
1215
|
}
|
|
1216
1216
|
/** Majority function, true if any two inputs is true. */
|
|
1217
|
-
function Maj(a, b, c) {
|
|
1217
|
+
function Maj$1(a, b, c) {
|
|
1218
1218
|
return (a & b) ^ (a & c) ^ (b & c);
|
|
1219
1219
|
}
|
|
1220
1220
|
/**
|
|
1221
1221
|
* Merkle-Damgard hash construction base class.
|
|
1222
1222
|
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
1223
1223
|
*/
|
|
1224
|
-
class HashMD extends Hash {
|
|
1224
|
+
let HashMD$1 = class HashMD extends Hash {
|
|
1225
1225
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
1226
1226
|
super();
|
|
1227
1227
|
this.finished = false;
|
|
@@ -1233,19 +1233,19 @@ class HashMD extends Hash {
|
|
|
1233
1233
|
this.padOffset = padOffset;
|
|
1234
1234
|
this.isLE = isLE;
|
|
1235
1235
|
this.buffer = new Uint8Array(blockLen);
|
|
1236
|
-
this.view = createView(this.buffer);
|
|
1236
|
+
this.view = createView$1(this.buffer);
|
|
1237
1237
|
}
|
|
1238
1238
|
update(data) {
|
|
1239
|
-
aexists(this);
|
|
1239
|
+
aexists$1(this);
|
|
1240
1240
|
data = toBytes(data);
|
|
1241
|
-
abytes(data);
|
|
1241
|
+
abytes$1(data);
|
|
1242
1242
|
const { view, buffer, blockLen } = this;
|
|
1243
1243
|
const len = data.length;
|
|
1244
1244
|
for (let pos = 0; pos < len;) {
|
|
1245
1245
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1246
1246
|
// Fast path: we have at least one block in input, cast it to view and process
|
|
1247
1247
|
if (take === blockLen) {
|
|
1248
|
-
const dataView = createView(data);
|
|
1248
|
+
const dataView = createView$1(data);
|
|
1249
1249
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
1250
1250
|
this.process(dataView, pos);
|
|
1251
1251
|
continue;
|
|
@@ -1263,8 +1263,8 @@ class HashMD extends Hash {
|
|
|
1263
1263
|
return this;
|
|
1264
1264
|
}
|
|
1265
1265
|
digestInto(out) {
|
|
1266
|
-
aexists(this);
|
|
1267
|
-
aoutput(out, this);
|
|
1266
|
+
aexists$1(this);
|
|
1267
|
+
aoutput$1(out, this);
|
|
1268
1268
|
this.finished = true;
|
|
1269
1269
|
// Padding
|
|
1270
1270
|
// We can avoid allocation of buffer for padding completely if it
|
|
@@ -1273,7 +1273,7 @@ class HashMD extends Hash {
|
|
|
1273
1273
|
let { pos } = this;
|
|
1274
1274
|
// append the bit '1' to the message
|
|
1275
1275
|
buffer[pos++] = 0b10000000;
|
|
1276
|
-
clean(this.buffer.subarray(pos));
|
|
1276
|
+
clean$1(this.buffer.subarray(pos));
|
|
1277
1277
|
// we have less than padOffset left in buffer, so we cannot put length in
|
|
1278
1278
|
// current block, need process it and pad again
|
|
1279
1279
|
if (this.padOffset > blockLen - pos) {
|
|
@@ -1288,7 +1288,7 @@ class HashMD extends Hash {
|
|
|
1288
1288
|
// So we just write lowest 64 bits of that value.
|
|
1289
1289
|
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
1290
1290
|
this.process(view, 0);
|
|
1291
|
-
const oview = createView(out);
|
|
1291
|
+
const oview = createView$1(out);
|
|
1292
1292
|
const len = this.outputLen;
|
|
1293
1293
|
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
1294
1294
|
if (len % 4)
|
|
@@ -1322,13 +1322,13 @@ class HashMD extends Hash {
|
|
|
1322
1322
|
clone() {
|
|
1323
1323
|
return this._cloneInto();
|
|
1324
1324
|
}
|
|
1325
|
-
}
|
|
1325
|
+
};
|
|
1326
1326
|
/**
|
|
1327
1327
|
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
1328
1328
|
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
1329
1329
|
*/
|
|
1330
1330
|
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
|
1331
|
-
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1331
|
+
const SHA256_IV$1 = /* @__PURE__ */ Uint32Array.from([
|
|
1332
1332
|
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
1333
1333
|
]);
|
|
1334
1334
|
|
|
@@ -1344,7 +1344,7 @@ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
|
1344
1344
|
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
|
1345
1345
|
*/
|
|
1346
1346
|
// prettier-ignore
|
|
1347
|
-
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
1347
|
+
const SHA256_K$1 = /* @__PURE__ */ Uint32Array.from([
|
|
1348
1348
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
1349
1349
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
1350
1350
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
@@ -1355,20 +1355,20 @@ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
|
1355
1355
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
1356
1356
|
]);
|
|
1357
1357
|
/** Reusable temporary buffer. "W" comes straight from spec. */
|
|
1358
|
-
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1359
|
-
class SHA256 extends HashMD {
|
|
1358
|
+
const SHA256_W$1 = /* @__PURE__ */ new Uint32Array(64);
|
|
1359
|
+
class SHA256 extends HashMD$1 {
|
|
1360
1360
|
constructor(outputLen = 32) {
|
|
1361
1361
|
super(64, outputLen, 8, false);
|
|
1362
1362
|
// We cannot use array here since array allows indexing by variable
|
|
1363
1363
|
// which means optimizer/compiler cannot use registers.
|
|
1364
|
-
this.A = SHA256_IV[0] | 0;
|
|
1365
|
-
this.B = SHA256_IV[1] | 0;
|
|
1366
|
-
this.C = SHA256_IV[2] | 0;
|
|
1367
|
-
this.D = SHA256_IV[3] | 0;
|
|
1368
|
-
this.E = SHA256_IV[4] | 0;
|
|
1369
|
-
this.F = SHA256_IV[5] | 0;
|
|
1370
|
-
this.G = SHA256_IV[6] | 0;
|
|
1371
|
-
this.H = SHA256_IV[7] | 0;
|
|
1364
|
+
this.A = SHA256_IV$1[0] | 0;
|
|
1365
|
+
this.B = SHA256_IV$1[1] | 0;
|
|
1366
|
+
this.C = SHA256_IV$1[2] | 0;
|
|
1367
|
+
this.D = SHA256_IV$1[3] | 0;
|
|
1368
|
+
this.E = SHA256_IV$1[4] | 0;
|
|
1369
|
+
this.F = SHA256_IV$1[5] | 0;
|
|
1370
|
+
this.G = SHA256_IV$1[6] | 0;
|
|
1371
|
+
this.H = SHA256_IV$1[7] | 0;
|
|
1372
1372
|
}
|
|
1373
1373
|
get() {
|
|
1374
1374
|
const { A, B, C, D, E, F, G, H } = this;
|
|
@@ -1388,21 +1388,21 @@ class SHA256 extends HashMD {
|
|
|
1388
1388
|
process(view, offset) {
|
|
1389
1389
|
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
1390
1390
|
for (let i = 0; i < 16; i++, offset += 4)
|
|
1391
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
1391
|
+
SHA256_W$1[i] = view.getUint32(offset, false);
|
|
1392
1392
|
for (let i = 16; i < 64; i++) {
|
|
1393
|
-
const W15 = SHA256_W[i - 15];
|
|
1394
|
-
const W2 = SHA256_W[i - 2];
|
|
1395
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
1396
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
1397
|
-
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
1393
|
+
const W15 = SHA256_W$1[i - 15];
|
|
1394
|
+
const W2 = SHA256_W$1[i - 2];
|
|
1395
|
+
const s0 = rotr$1(W15, 7) ^ rotr$1(W15, 18) ^ (W15 >>> 3);
|
|
1396
|
+
const s1 = rotr$1(W2, 17) ^ rotr$1(W2, 19) ^ (W2 >>> 10);
|
|
1397
|
+
SHA256_W$1[i] = (s1 + SHA256_W$1[i - 7] + s0 + SHA256_W$1[i - 16]) | 0;
|
|
1398
1398
|
}
|
|
1399
1399
|
// Compression function main loop, 64 rounds
|
|
1400
1400
|
let { A, B, C, D, E, F, G, H } = this;
|
|
1401
1401
|
for (let i = 0; i < 64; i++) {
|
|
1402
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
1403
|
-
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
1404
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
1405
|
-
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
1402
|
+
const sigma1 = rotr$1(E, 6) ^ rotr$1(E, 11) ^ rotr$1(E, 25);
|
|
1403
|
+
const T1 = (H + sigma1 + Chi$1(E, F, G) + SHA256_K$1[i] + SHA256_W$1[i]) | 0;
|
|
1404
|
+
const sigma0 = rotr$1(A, 2) ^ rotr$1(A, 13) ^ rotr$1(A, 22);
|
|
1405
|
+
const T2 = (sigma0 + Maj$1(A, B, C)) | 0;
|
|
1406
1406
|
H = G;
|
|
1407
1407
|
G = F;
|
|
1408
1408
|
F = E;
|
|
@@ -1424,11 +1424,11 @@ class SHA256 extends HashMD {
|
|
|
1424
1424
|
this.set(A, B, C, D, E, F, G, H);
|
|
1425
1425
|
}
|
|
1426
1426
|
roundClean() {
|
|
1427
|
-
clean(SHA256_W);
|
|
1427
|
+
clean$1(SHA256_W$1);
|
|
1428
1428
|
}
|
|
1429
1429
|
destroy() {
|
|
1430
1430
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1431
|
-
clean(this.buffer);
|
|
1431
|
+
clean$1(this.buffer);
|
|
1432
1432
|
}
|
|
1433
1433
|
}
|
|
1434
1434
|
/**
|
|
@@ -1438,7 +1438,7 @@ class SHA256 extends HashMD {
|
|
|
1438
1438
|
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
1439
1439
|
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1440
1440
|
*/
|
|
1441
|
-
const sha256$
|
|
1441
|
+
const sha256$2 = /* @__PURE__ */ createHasher$1(() => new SHA256());
|
|
1442
1442
|
|
|
1443
1443
|
/**
|
|
1444
1444
|
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
|
@@ -1451,7 +1451,7 @@ const sha256$1 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
|
1451
1451
|
* @deprecated
|
|
1452
1452
|
*/
|
|
1453
1453
|
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1454
|
-
const sha256 = sha256$
|
|
1454
|
+
const sha256$1 = sha256$2;
|
|
1455
1455
|
|
|
1456
1456
|
function bs58checkBase (checksumFn) {
|
|
1457
1457
|
// Encode a buffer as a base58-check encoded string
|
|
@@ -1499,7 +1499,7 @@ function bs58checkBase (checksumFn) {
|
|
|
1499
1499
|
|
|
1500
1500
|
// SHA256(SHA256(buffer))
|
|
1501
1501
|
function sha256x2(buffer) {
|
|
1502
|
-
return sha256(sha256(buffer));
|
|
1502
|
+
return sha256$1(sha256$1(buffer));
|
|
1503
1503
|
}
|
|
1504
1504
|
var bs58check = bs58checkBase(sha256x2);
|
|
1505
1505
|
|
|
@@ -2541,6 +2541,89 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
|
|
|
2541
2541
|
return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
|
|
2542
2542
|
}
|
|
2543
2543
|
|
|
2544
|
+
/**
|
|
2545
|
+
* Witness-stack builders for the Partial-Fill Sell Order covenant.
|
|
2546
|
+
*
|
|
2547
|
+
* When the covenant lives behind an AuthScript commitment (the only
|
|
2548
|
+
* asset-compatible deployment since the node's OP_XNA_ASSET placement rules
|
|
2549
|
+
* — bare covenant outputs can no longer carry assets), the unlock data goes
|
|
2550
|
+
* in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
|
|
2551
|
+
* return a serialized script that PUSHES the elements; a witness needs the
|
|
2552
|
+
* elements THEMSELVES, one per stack slot, so those blobs cannot be reused
|
|
2553
|
+
* as a single `args` entry.
|
|
2554
|
+
*
|
|
2555
|
+
* These builders return the raw `args` stack (bottom → top), mirroring the
|
|
2556
|
+
* shapes documented in `spend.ts`:
|
|
2557
|
+
*
|
|
2558
|
+
* Full fill: [ <1>, <0> ]
|
|
2559
|
+
* Partial fill: [ <N>, <0>, <0> ]
|
|
2560
|
+
* Cancel: [ <sig>, <pubkey>, <1> ]
|
|
2561
|
+
*
|
|
2562
|
+
* Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
|
|
2563
|
+
* result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
|
|
2564
|
+
* (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
|
|
2565
|
+
* witness, and serialize the spending transaction with
|
|
2566
|
+
* `serializeTransaction` from `@neuraiproject/neurai-create-transaction`
|
|
2567
|
+
* (0.5.1+, witness elements as hex strings: `witness.map(bytesToHex)`).
|
|
2568
|
+
*/
|
|
2569
|
+
const MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072;
|
|
2570
|
+
/**
|
|
2571
|
+
* Witness `args` for the public fill branches. Same semantics and
|
|
2572
|
+
* validations as `buildFillScriptSig`.
|
|
2573
|
+
*/
|
|
2574
|
+
function buildFillWitnessStack(amount, total) {
|
|
2575
|
+
if (typeof amount !== 'bigint' || typeof total !== 'bigint') {
|
|
2576
|
+
throw new Error('amount and total must be bigint');
|
|
2577
|
+
}
|
|
2578
|
+
if (amount <= 0n) {
|
|
2579
|
+
throw new Error('fill amount must be > 0');
|
|
2580
|
+
}
|
|
2581
|
+
if (total <= 0n) {
|
|
2582
|
+
throw new Error('total must be > 0');
|
|
2583
|
+
}
|
|
2584
|
+
if (amount > total) {
|
|
2585
|
+
throw new Error('fill amount exceeds the covenant total');
|
|
2586
|
+
}
|
|
2587
|
+
if (amount === total) {
|
|
2588
|
+
// Full fill: covenant drains entirely; buyer does not push N.
|
|
2589
|
+
return [encodeScriptNum(1n), encodeScriptNum(0n)];
|
|
2590
|
+
}
|
|
2591
|
+
// Partial fill: N, then both flag values.
|
|
2592
|
+
return [encodeScriptNum(amount), encodeScriptNum(0n), encodeScriptNum(0n)];
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Witness `args` for the seller's ECDSA cancel branch. Same semantics and
|
|
2596
|
+
* validations as `buildCancelScriptSig`.
|
|
2597
|
+
*/
|
|
2598
|
+
function buildCancelWitnessStack(signatureDer, pubKey) {
|
|
2599
|
+
if (!(signatureDer instanceof Uint8Array) || signatureDer.length === 0) {
|
|
2600
|
+
throw new Error('signatureDer must be a non-empty Uint8Array');
|
|
2601
|
+
}
|
|
2602
|
+
if (!(pubKey instanceof Uint8Array) || (pubKey.length !== 33 && pubKey.length !== 65)) {
|
|
2603
|
+
throw new Error('pubKey must be a compressed (33B) or uncompressed (65B) secp256k1 key');
|
|
2604
|
+
}
|
|
2605
|
+
return [signatureDer, pubKey, encodeScriptNum(1n)];
|
|
2606
|
+
}
|
|
2607
|
+
/**
|
|
2608
|
+
* Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
|
|
2609
|
+
* semantics and validations as `buildCancelScriptSigPQ`.
|
|
2610
|
+
*/
|
|
2611
|
+
function buildCancelWitnessStackPQ(sigPQ, pubKey) {
|
|
2612
|
+
if (!(sigPQ instanceof Uint8Array) || sigPQ.length === 0) {
|
|
2613
|
+
throw new Error('sigPQ must be a non-empty Uint8Array');
|
|
2614
|
+
}
|
|
2615
|
+
if (sigPQ.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
|
|
2616
|
+
throw new Error(`sigPQ of ${sigPQ.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
|
|
2617
|
+
}
|
|
2618
|
+
if (!(pubKey instanceof Uint8Array) || pubKey.length === 0) {
|
|
2619
|
+
throw new Error('pubKey must be a non-empty Uint8Array');
|
|
2620
|
+
}
|
|
2621
|
+
if (pubKey.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
|
|
2622
|
+
throw new Error(`pubKey of ${pubKey.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
|
|
2623
|
+
}
|
|
2624
|
+
return [sigPQ, pubKey, encodeScriptNum(1n)];
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2544
2627
|
/**
|
|
2545
2628
|
* Parser for the Partial-Fill Sell Order covenant (three-branch).
|
|
2546
2629
|
*
|
|
@@ -2866,8 +2949,11 @@ exports.buildCancelScriptSig = buildCancelScriptSig;
|
|
|
2866
2949
|
exports.buildCancelScriptSigHex = buildCancelScriptSigHex;
|
|
2867
2950
|
exports.buildCancelScriptSigPQ = buildCancelScriptSigPQ;
|
|
2868
2951
|
exports.buildCancelScriptSigPQHex = buildCancelScriptSigPQHex;
|
|
2952
|
+
exports.buildCancelWitnessStack = buildCancelWitnessStack;
|
|
2953
|
+
exports.buildCancelWitnessStackPQ = buildCancelWitnessStackPQ;
|
|
2869
2954
|
exports.buildFillScriptSig = buildFillScriptSig;
|
|
2870
2955
|
exports.buildFillScriptSigHex = buildFillScriptSigHex;
|
|
2956
|
+
exports.buildFillWitnessStack = buildFillWitnessStack;
|
|
2871
2957
|
exports.buildPartialFillScript = buildPartialFillScript;
|
|
2872
2958
|
exports.buildPartialFillScriptHex = buildPartialFillScriptHex;
|
|
2873
2959
|
exports.buildPartialFillScriptPQ = buildPartialFillScriptPQ;
|