phantasma-sdk-ts 0.2.11 → 0.3.0
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/cjs/core/types/Carbon/IntX.js +24 -29
- package/dist/cjs/core/types/CarbonSerialization.js +70 -43
- package/dist/esm/core/types/Carbon/IntX.js +24 -29
- package/dist/esm/core/types/CarbonSerialization.js +70 -43
- package/dist/types/core/types/Carbon/IntX.d.ts.map +1 -1
- package/dist/types/core/types/CarbonSerialization.d.ts +3 -4
- package/dist/types/core/types/CarbonSerialization.d.ts.map +1 -1
- package/package.json +9 -9
|
@@ -59,41 +59,36 @@ class IntX {
|
|
|
59
59
|
// Deserialization (must match C# IntX.Read)
|
|
60
60
|
read(r) {
|
|
61
61
|
const header = r.read1();
|
|
62
|
-
if (header === 0) {
|
|
63
|
-
// Zero in compact format
|
|
64
|
-
this.isBig = false;
|
|
65
|
-
this.small = 0n;
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
62
|
const len = header & 0x3f;
|
|
69
|
-
|
|
63
|
+
if (len < 8) {
|
|
64
|
+
throw new Error('invalid intx packing');
|
|
65
|
+
}
|
|
70
66
|
if (len === 8) {
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
// IntX keeps the 8-byte fast path, but the header sign bit still refers to the reconstructed
|
|
68
|
+
// 256-bit value. If those signs disagree, this payload is not an int64; it is a wider IntX
|
|
69
|
+
// whose compact validator encoding happens to fit in 8 serialized bytes.
|
|
70
|
+
const value = r.read8();
|
|
71
|
+
const headerIsNegative = (header & 0x80) !== 0;
|
|
72
|
+
const valueIsNegative = value < 0n;
|
|
73
|
+
if (headerIsNegative === valueIsNegative) {
|
|
74
|
+
this.isBig = false;
|
|
75
|
+
this.small = value;
|
|
76
|
+
return;
|
|
77
77
|
}
|
|
78
|
+
// Rebuild the full 256-bit word using the header sign bit as the omitted fill byte.
|
|
79
|
+
const rawBytes = new Uint8Array(8);
|
|
80
|
+
new DataView(rawBytes.buffer).setBigInt64(0, value, true);
|
|
81
|
+
const fill = headerIsNegative ? 0xff : 0x00;
|
|
82
|
+
const word = new Uint8Array(32);
|
|
83
|
+
word.set(rawBytes, 0);
|
|
84
|
+
word.fill(fill, rawBytes.length);
|
|
85
|
+
this.isBig = true;
|
|
86
|
+
this.big = (0, CarbonSerialization_js_1.twosComplementLEToBigInt)(word);
|
|
78
87
|
return;
|
|
79
88
|
}
|
|
80
|
-
//
|
|
81
|
-
// and convert LE two’s complement to bigint
|
|
82
|
-
if (len > 32) {
|
|
83
|
-
throw new Error('BigInt too big');
|
|
84
|
-
}
|
|
85
|
-
let bytes = r.readExactly(len);
|
|
86
|
-
// Ensure sign extension byte matches header sign
|
|
87
|
-
const inherentNegative = (bytes[bytes.length - 1] & 0x80) !== 0;
|
|
88
|
-
if (inherentNegative !== signBit) {
|
|
89
|
-
const ext = signBit ? 0xff : 0x00;
|
|
90
|
-
const tmp = new Uint8Array(bytes.length + 1);
|
|
91
|
-
tmp.set(bytes, 0);
|
|
92
|
-
tmp[tmp.length - 1] = ext;
|
|
93
|
-
bytes = tmp;
|
|
94
|
-
}
|
|
89
|
+
// Wider IntX values reuse the exact validator BigInt reader contract with the already-consumed header.
|
|
95
90
|
this.isBig = true;
|
|
96
|
-
this.big =
|
|
91
|
+
this.big = r.readBigInt(header);
|
|
97
92
|
}
|
|
98
93
|
static read(r) {
|
|
99
94
|
const v = new IntX();
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* - Little-endian for all integers
|
|
5
5
|
* - Zero-terminated UTF-8 strings
|
|
6
6
|
* - BigInt compact format:
|
|
7
|
-
* header: 0 -> zero; otherwise lower 6 bits = length (
|
|
8
|
-
* payload:
|
|
7
|
+
* header: 0 -> zero; otherwise lower 6 bits = payload length (0..32), bit7 = sign fill
|
|
8
|
+
* payload: the low-order bytes of the validator/runtime 256-bit word after trailing fill trimming
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.readBlob = exports.writeBlob = exports.twosComplementLEToBigInt = exports.bigIntToTwosComplementLE = exports.CarbonBinaryReader = exports.CarbonBinaryWriter = exports.Throw = void 0;
|
|
@@ -105,32 +105,25 @@ class CarbonBinaryWriter {
|
|
|
105
105
|
t.write(this);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
-
// BigInt compact format (<= 32 bytes)
|
|
108
|
+
// BigInt compact format (<= 32 payload bytes after validator trimming)
|
|
109
109
|
writeBigInt(value) {
|
|
110
110
|
if (value === 0n) {
|
|
111
111
|
this.write1(0);
|
|
112
112
|
return;
|
|
113
113
|
}
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.writeBigIntRaw(bytes);
|
|
124
|
-
}
|
|
125
|
-
writeBigIntRaw(bytes) {
|
|
126
|
-
const length = bytes.length;
|
|
127
|
-
Throw.Assert(length > 0 && length <= 32, 'BigInt too big');
|
|
128
|
-
// infer sign from inherent top bit
|
|
129
|
-
const signFromBytes = (bytes[length - 1] & 0x80) !== 0 ? -1 : 1;
|
|
130
|
-
const signBit = signFromBytes < 0 ? 0x80 : 0;
|
|
131
|
-
const header = (length & 0x3f) | signBit;
|
|
114
|
+
// Carbon wire bytes follow the validator/runtime contract, not JS BigInt's sign-safe minimal form.
|
|
115
|
+
// We therefore rebuild the full 256-bit two's-complement word first and only then trim high fill bytes.
|
|
116
|
+
const word = normalizeBigIntWord(bigIntToTwosComplementLE(value));
|
|
117
|
+
// The sign bit in the reconstructed 256-bit word determines which fill byte the validator would omit.
|
|
118
|
+
const fill = (word[31] & 0x80) !== 0 ? 0xff : 0x00;
|
|
119
|
+
// Validator trimming is intentionally simple: drop every trailing fill byte from the 256-bit word.
|
|
120
|
+
const length = computeBigIntSerializedLength(word, fill);
|
|
121
|
+
// The header stores the sign of the reconstructed 256-bit word, even when the payload is empty.
|
|
122
|
+
const header = (length & 0x3f) | (fill & 0x80);
|
|
132
123
|
this.write1(header);
|
|
133
|
-
|
|
124
|
+
if (length > 0) {
|
|
125
|
+
this.write(word.subarray(0, length));
|
|
126
|
+
}
|
|
134
127
|
}
|
|
135
128
|
writeArrayBigInt(items) {
|
|
136
129
|
this.write4(items.length);
|
|
@@ -280,32 +273,27 @@ class CarbonBinaryReader {
|
|
|
280
273
|
}
|
|
281
274
|
return arr;
|
|
282
275
|
}
|
|
283
|
-
// BigInt compact format
|
|
284
|
-
|
|
285
|
-
|
|
276
|
+
// BigInt compact format.
|
|
277
|
+
// `preReadHeader` lets IntX reuse the exact same validator reader after consuming its own framing byte.
|
|
278
|
+
readBigInt(preReadHeader = -1) {
|
|
279
|
+
const header = preReadHeader < 0 ? this.read1() : preReadHeader & 0xff;
|
|
286
280
|
if (header === 0) {
|
|
287
281
|
return 0n;
|
|
288
282
|
}
|
|
289
|
-
const sign = (header & 0x80) !== 0 ? -1 : 1;
|
|
290
283
|
const length = header & 0x3f;
|
|
291
|
-
Throw.If(length > 32, 'BigInt too big');
|
|
292
|
-
|
|
293
|
-
|
|
284
|
+
Throw.If((header & 0x40) !== 0 || length > 32, 'BigInt too big');
|
|
285
|
+
// The header sign bit defines the bytes that were omitted from the high side of the 256-bit word.
|
|
286
|
+
const fill = (header & 0x80) !== 0 ? 0xff : 0x00;
|
|
287
|
+
// Rebuild the full validator/runtime 256-bit word before decoding two's complement.
|
|
288
|
+
// This is what makes shortest negative forms like `0x80` decode to `-1` instead of `0`.
|
|
289
|
+
const word = new Uint8Array(32);
|
|
290
|
+
if (length > 0) {
|
|
291
|
+
word.set(this.readExactly(length), 0);
|
|
294
292
|
}
|
|
295
|
-
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const ext = sign >= 0 ? 0x00 : 0xff;
|
|
300
|
-
const tmp = new Uint8Array(bytes.length + 1);
|
|
301
|
-
tmp.set(bytes, 0);
|
|
302
|
-
tmp[tmp.length - 1] = ext;
|
|
303
|
-
bytes = tmp;
|
|
304
|
-
}
|
|
305
|
-
const bi = twosComplementLEToBigInt(bytes);
|
|
306
|
-
// Sanity check: signs must match
|
|
307
|
-
Throw.Assert((bi < 0n ? -1 : bi > 0n ? 1 : 0) === sign || bi === 0n, 'BigInt sign mismatch');
|
|
308
|
-
return bi;
|
|
293
|
+
word.fill(fill, length);
|
|
294
|
+
// If the reconstructed sign bit disagrees with the header, the payload is not a valid validator word.
|
|
295
|
+
Throw.Assert((word[31] & 0x80) === (header & 0x80), 'non-standard BigInt header');
|
|
296
|
+
return twosComplementLEToBigInt(word);
|
|
309
297
|
}
|
|
310
298
|
readArrayBigInt() {
|
|
311
299
|
const len = this.read4();
|
|
@@ -417,6 +405,45 @@ function bigIntToTwosComplementLE(value) {
|
|
|
417
405
|
return Uint8Array.from(out);
|
|
418
406
|
}
|
|
419
407
|
exports.bigIntToTwosComplementLE = bigIntToTwosComplementLE;
|
|
408
|
+
/**
|
|
409
|
+
* Rebuild the validator/runtime 256-bit word from a sign-safe minimal two's-complement payload.
|
|
410
|
+
*
|
|
411
|
+
* The Carbon protocol does not serialize JS/.NET style sign-guard bytes directly.
|
|
412
|
+
* Instead it reconstructs the full 256-bit two's-complement word first, then trims all high fill bytes.
|
|
413
|
+
* This helper performs the "reconstruct the word" half of that contract.
|
|
414
|
+
*/
|
|
415
|
+
function normalizeBigIntWord(bytes) {
|
|
416
|
+
if (bytes.length === 0) {
|
|
417
|
+
return new Uint8Array(32);
|
|
418
|
+
}
|
|
419
|
+
const sourceFill = (bytes[bytes.length - 1] & 0x80) !== 0 ? 0xff : 0x00;
|
|
420
|
+
let normalized = bytes;
|
|
421
|
+
if (normalized.length > 32) {
|
|
422
|
+
// Any bytes above 256 bits must be pure sign extension. If not, the value does not fit Carbon Int256.
|
|
423
|
+
for (let i = 32; i < normalized.length; i++) {
|
|
424
|
+
Throw.Assert(normalized[i] === sourceFill, 'BigInt overflow');
|
|
425
|
+
}
|
|
426
|
+
normalized = normalized.slice(0, 32);
|
|
427
|
+
}
|
|
428
|
+
const word = new Uint8Array(32);
|
|
429
|
+
word.set(normalized, 0);
|
|
430
|
+
// Shorter sign-safe encodings are expanded back to the full 256-bit two's-complement word.
|
|
431
|
+
word.fill(sourceFill, normalized.length);
|
|
432
|
+
return word;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Compute the validator/runtime payload length for a reconstructed 256-bit word.
|
|
436
|
+
*
|
|
437
|
+
* The validator does not keep an extra guard byte once the word is expanded.
|
|
438
|
+
* It simply removes every contiguous high fill byte from the full word.
|
|
439
|
+
*/
|
|
440
|
+
function computeBigIntSerializedLength(word, fill) {
|
|
441
|
+
let length = word.length;
|
|
442
|
+
while (length > 0 && word[length - 1] === fill) {
|
|
443
|
+
length--;
|
|
444
|
+
}
|
|
445
|
+
return length;
|
|
446
|
+
}
|
|
420
447
|
/** Convert little-endian two's complement bytes to bigint */
|
|
421
448
|
function twosComplementLEToBigInt(bytes) {
|
|
422
449
|
if (bytes.length === 0)
|
|
@@ -56,41 +56,36 @@ export class IntX {
|
|
|
56
56
|
// Deserialization (must match C# IntX.Read)
|
|
57
57
|
read(r) {
|
|
58
58
|
const header = r.read1();
|
|
59
|
-
if (header === 0) {
|
|
60
|
-
// Zero in compact format
|
|
61
|
-
this.isBig = false;
|
|
62
|
-
this.small = 0n;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
59
|
const len = header & 0x3f;
|
|
66
|
-
|
|
60
|
+
if (len < 8) {
|
|
61
|
+
throw new Error('invalid intx packing');
|
|
62
|
+
}
|
|
67
63
|
if (len === 8) {
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
// IntX keeps the 8-byte fast path, but the header sign bit still refers to the reconstructed
|
|
65
|
+
// 256-bit value. If those signs disagree, this payload is not an int64; it is a wider IntX
|
|
66
|
+
// whose compact validator encoding happens to fit in 8 serialized bytes.
|
|
67
|
+
const value = r.read8();
|
|
68
|
+
const headerIsNegative = (header & 0x80) !== 0;
|
|
69
|
+
const valueIsNegative = value < 0n;
|
|
70
|
+
if (headerIsNegative === valueIsNegative) {
|
|
71
|
+
this.isBig = false;
|
|
72
|
+
this.small = value;
|
|
73
|
+
return;
|
|
74
74
|
}
|
|
75
|
+
// Rebuild the full 256-bit word using the header sign bit as the omitted fill byte.
|
|
76
|
+
const rawBytes = new Uint8Array(8);
|
|
77
|
+
new DataView(rawBytes.buffer).setBigInt64(0, value, true);
|
|
78
|
+
const fill = headerIsNegative ? 0xff : 0x00;
|
|
79
|
+
const word = new Uint8Array(32);
|
|
80
|
+
word.set(rawBytes, 0);
|
|
81
|
+
word.fill(fill, rawBytes.length);
|
|
82
|
+
this.isBig = true;
|
|
83
|
+
this.big = twosComplementLEToBigInt(word);
|
|
75
84
|
return;
|
|
76
85
|
}
|
|
77
|
-
//
|
|
78
|
-
// and convert LE two’s complement to bigint
|
|
79
|
-
if (len > 32) {
|
|
80
|
-
throw new Error('BigInt too big');
|
|
81
|
-
}
|
|
82
|
-
let bytes = r.readExactly(len);
|
|
83
|
-
// Ensure sign extension byte matches header sign
|
|
84
|
-
const inherentNegative = (bytes[bytes.length - 1] & 0x80) !== 0;
|
|
85
|
-
if (inherentNegative !== signBit) {
|
|
86
|
-
const ext = signBit ? 0xff : 0x00;
|
|
87
|
-
const tmp = new Uint8Array(bytes.length + 1);
|
|
88
|
-
tmp.set(bytes, 0);
|
|
89
|
-
tmp[tmp.length - 1] = ext;
|
|
90
|
-
bytes = tmp;
|
|
91
|
-
}
|
|
86
|
+
// Wider IntX values reuse the exact validator BigInt reader contract with the already-consumed header.
|
|
92
87
|
this.isBig = true;
|
|
93
|
-
this.big =
|
|
88
|
+
this.big = r.readBigInt(header);
|
|
94
89
|
}
|
|
95
90
|
static read(r) {
|
|
96
91
|
const v = new IntX();
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* - Little-endian for all integers
|
|
4
4
|
* - Zero-terminated UTF-8 strings
|
|
5
5
|
* - BigInt compact format:
|
|
6
|
-
* header: 0 -> zero; otherwise lower 6 bits = length (
|
|
7
|
-
* payload:
|
|
6
|
+
* header: 0 -> zero; otherwise lower 6 bits = payload length (0..32), bit7 = sign fill
|
|
7
|
+
* payload: the low-order bytes of the validator/runtime 256-bit word after trailing fill trimming
|
|
8
8
|
*/
|
|
9
9
|
const textEncoder = new TextEncoder();
|
|
10
10
|
const textDecoder = new TextDecoder();
|
|
@@ -101,32 +101,25 @@ export class CarbonBinaryWriter {
|
|
|
101
101
|
t.write(this);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
// BigInt compact format (<= 32 bytes)
|
|
104
|
+
// BigInt compact format (<= 32 payload bytes after validator trimming)
|
|
105
105
|
writeBigInt(value) {
|
|
106
106
|
if (value === 0n) {
|
|
107
107
|
this.write1(0);
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.writeBigIntRaw(bytes);
|
|
120
|
-
}
|
|
121
|
-
writeBigIntRaw(bytes) {
|
|
122
|
-
const length = bytes.length;
|
|
123
|
-
Throw.Assert(length > 0 && length <= 32, 'BigInt too big');
|
|
124
|
-
// infer sign from inherent top bit
|
|
125
|
-
const signFromBytes = (bytes[length - 1] & 0x80) !== 0 ? -1 : 1;
|
|
126
|
-
const signBit = signFromBytes < 0 ? 0x80 : 0;
|
|
127
|
-
const header = (length & 0x3f) | signBit;
|
|
110
|
+
// Carbon wire bytes follow the validator/runtime contract, not JS BigInt's sign-safe minimal form.
|
|
111
|
+
// We therefore rebuild the full 256-bit two's-complement word first and only then trim high fill bytes.
|
|
112
|
+
const word = normalizeBigIntWord(bigIntToTwosComplementLE(value));
|
|
113
|
+
// The sign bit in the reconstructed 256-bit word determines which fill byte the validator would omit.
|
|
114
|
+
const fill = (word[31] & 0x80) !== 0 ? 0xff : 0x00;
|
|
115
|
+
// Validator trimming is intentionally simple: drop every trailing fill byte from the 256-bit word.
|
|
116
|
+
const length = computeBigIntSerializedLength(word, fill);
|
|
117
|
+
// The header stores the sign of the reconstructed 256-bit word, even when the payload is empty.
|
|
118
|
+
const header = (length & 0x3f) | (fill & 0x80);
|
|
128
119
|
this.write1(header);
|
|
129
|
-
|
|
120
|
+
if (length > 0) {
|
|
121
|
+
this.write(word.subarray(0, length));
|
|
122
|
+
}
|
|
130
123
|
}
|
|
131
124
|
writeArrayBigInt(items) {
|
|
132
125
|
this.write4(items.length);
|
|
@@ -275,32 +268,27 @@ export class CarbonBinaryReader {
|
|
|
275
268
|
}
|
|
276
269
|
return arr;
|
|
277
270
|
}
|
|
278
|
-
// BigInt compact format
|
|
279
|
-
|
|
280
|
-
|
|
271
|
+
// BigInt compact format.
|
|
272
|
+
// `preReadHeader` lets IntX reuse the exact same validator reader after consuming its own framing byte.
|
|
273
|
+
readBigInt(preReadHeader = -1) {
|
|
274
|
+
const header = preReadHeader < 0 ? this.read1() : preReadHeader & 0xff;
|
|
281
275
|
if (header === 0) {
|
|
282
276
|
return 0n;
|
|
283
277
|
}
|
|
284
|
-
const sign = (header & 0x80) !== 0 ? -1 : 1;
|
|
285
278
|
const length = header & 0x3f;
|
|
286
|
-
Throw.If(length > 32, 'BigInt too big');
|
|
287
|
-
|
|
288
|
-
|
|
279
|
+
Throw.If((header & 0x40) !== 0 || length > 32, 'BigInt too big');
|
|
280
|
+
// The header sign bit defines the bytes that were omitted from the high side of the 256-bit word.
|
|
281
|
+
const fill = (header & 0x80) !== 0 ? 0xff : 0x00;
|
|
282
|
+
// Rebuild the full validator/runtime 256-bit word before decoding two's complement.
|
|
283
|
+
// This is what makes shortest negative forms like `0x80` decode to `-1` instead of `0`.
|
|
284
|
+
const word = new Uint8Array(32);
|
|
285
|
+
if (length > 0) {
|
|
286
|
+
word.set(this.readExactly(length), 0);
|
|
289
287
|
}
|
|
290
|
-
|
|
291
|
-
//
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const ext = sign >= 0 ? 0x00 : 0xff;
|
|
295
|
-
const tmp = new Uint8Array(bytes.length + 1);
|
|
296
|
-
tmp.set(bytes, 0);
|
|
297
|
-
tmp[tmp.length - 1] = ext;
|
|
298
|
-
bytes = tmp;
|
|
299
|
-
}
|
|
300
|
-
const bi = twosComplementLEToBigInt(bytes);
|
|
301
|
-
// Sanity check: signs must match
|
|
302
|
-
Throw.Assert((bi < 0n ? -1 : bi > 0n ? 1 : 0) === sign || bi === 0n, 'BigInt sign mismatch');
|
|
303
|
-
return bi;
|
|
288
|
+
word.fill(fill, length);
|
|
289
|
+
// If the reconstructed sign bit disagrees with the header, the payload is not a valid validator word.
|
|
290
|
+
Throw.Assert((word[31] & 0x80) === (header & 0x80), 'non-standard BigInt header');
|
|
291
|
+
return twosComplementLEToBigInt(word);
|
|
304
292
|
}
|
|
305
293
|
readArrayBigInt() {
|
|
306
294
|
const len = this.read4();
|
|
@@ -410,6 +398,45 @@ export function bigIntToTwosComplementLE(value) {
|
|
|
410
398
|
}
|
|
411
399
|
return Uint8Array.from(out);
|
|
412
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Rebuild the validator/runtime 256-bit word from a sign-safe minimal two's-complement payload.
|
|
403
|
+
*
|
|
404
|
+
* The Carbon protocol does not serialize JS/.NET style sign-guard bytes directly.
|
|
405
|
+
* Instead it reconstructs the full 256-bit two's-complement word first, then trims all high fill bytes.
|
|
406
|
+
* This helper performs the "reconstruct the word" half of that contract.
|
|
407
|
+
*/
|
|
408
|
+
function normalizeBigIntWord(bytes) {
|
|
409
|
+
if (bytes.length === 0) {
|
|
410
|
+
return new Uint8Array(32);
|
|
411
|
+
}
|
|
412
|
+
const sourceFill = (bytes[bytes.length - 1] & 0x80) !== 0 ? 0xff : 0x00;
|
|
413
|
+
let normalized = bytes;
|
|
414
|
+
if (normalized.length > 32) {
|
|
415
|
+
// Any bytes above 256 bits must be pure sign extension. If not, the value does not fit Carbon Int256.
|
|
416
|
+
for (let i = 32; i < normalized.length; i++) {
|
|
417
|
+
Throw.Assert(normalized[i] === sourceFill, 'BigInt overflow');
|
|
418
|
+
}
|
|
419
|
+
normalized = normalized.slice(0, 32);
|
|
420
|
+
}
|
|
421
|
+
const word = new Uint8Array(32);
|
|
422
|
+
word.set(normalized, 0);
|
|
423
|
+
// Shorter sign-safe encodings are expanded back to the full 256-bit two's-complement word.
|
|
424
|
+
word.fill(sourceFill, normalized.length);
|
|
425
|
+
return word;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Compute the validator/runtime payload length for a reconstructed 256-bit word.
|
|
429
|
+
*
|
|
430
|
+
* The validator does not keep an extra guard byte once the word is expanded.
|
|
431
|
+
* It simply removes every contiguous high fill byte from the full word.
|
|
432
|
+
*/
|
|
433
|
+
function computeBigIntSerializedLength(word, fill) {
|
|
434
|
+
let length = word.length;
|
|
435
|
+
while (length > 0 && word[length - 1] === fill) {
|
|
436
|
+
length--;
|
|
437
|
+
}
|
|
438
|
+
return length;
|
|
439
|
+
}
|
|
413
440
|
/** Convert little-endian two's complement bytes to bigint */
|
|
414
441
|
export function twosComplementLEToBigInt(bytes) {
|
|
415
442
|
if (bytes.length === 0)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntX.d.ts","sourceRoot":"","sources":["../../../../../src/core/types/Carbon/IntX.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,wCAAwC,CAAC;AACrE,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAEnB,MAAM,2BAA2B,CAAC;AAEnC,qBAAa,IAAK,YAAW,WAAW;IACtC,OAAO,CAAC,GAAG,CAAc;IACzB,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,KAAK,CAAS;IAGtB,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAOlC,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAOxC,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;IAKlB,WAAW,IAAI,OAAO;IAQtB,KAAK,CAAC,CAAC,EAAE,kBAAkB,GAAG,IAAI;IAwBlC,IAAI,CAAC,CAAC,EAAE,kBAAkB,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"IntX.d.ts","sourceRoot":"","sources":["../../../../../src/core/types/Carbon/IntX.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,wCAAwC,CAAC;AACrE,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAEnB,MAAM,2BAA2B,CAAC;AAEnC,qBAAa,IAAK,YAAW,WAAW;IACtC,OAAO,CAAC,GAAG,CAAc;IACzB,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,KAAK,CAAS;IAGtB,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAOlC,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAOxC,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;IAKlB,WAAW,IAAI,OAAO;IAQtB,KAAK,CAAC,CAAC,EAAE,kBAAkB,GAAG,IAAI;IAwBlC,IAAI,CAAC,CAAC,EAAE,kBAAkB,GAAG,IAAI;IAuCjC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,kBAAkB,GAAG,IAAI;CAKzC"}
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* - Little-endian for all integers
|
|
4
4
|
* - Zero-terminated UTF-8 strings
|
|
5
5
|
* - BigInt compact format:
|
|
6
|
-
* header: 0 -> zero; otherwise lower 6 bits = length (
|
|
7
|
-
* payload:
|
|
6
|
+
* header: 0 -> zero; otherwise lower 6 bits = payload length (0..32), bit7 = sign fill
|
|
7
|
+
* payload: the low-order bytes of the validator/runtime 256-bit word after trailing fill trimming
|
|
8
8
|
*/
|
|
9
9
|
import { ICarbonBlob } from '../interfaces/Carbon/ICarbonBlob.js';
|
|
10
10
|
import { Bytes16 } from './Carbon/Bytes16.js';
|
|
@@ -33,7 +33,6 @@ export declare class CarbonBinaryWriter {
|
|
|
33
33
|
writeBlob<T extends ICarbonBlob>(data: T): void;
|
|
34
34
|
writeArrayBlob<T extends ICarbonBlob>(arr: T[]): void;
|
|
35
35
|
writeBigInt(value: bigint): void;
|
|
36
|
-
private writeBigIntRaw;
|
|
37
36
|
writeArrayBigInt(items: bigint[]): void;
|
|
38
37
|
writeSz(s: string): void;
|
|
39
38
|
writeArraySz(arr: string[]): void;
|
|
@@ -67,7 +66,7 @@ export declare class CarbonBinaryReader {
|
|
|
67
66
|
readInto64(out: Bytes64): void;
|
|
68
67
|
readBlob<T extends ICarbonBlob>(ctor: new () => T): T;
|
|
69
68
|
readArrayBlob<T extends ICarbonBlob>(ctor: new () => T): T[];
|
|
70
|
-
readBigInt(): bigint;
|
|
69
|
+
readBigInt(preReadHeader?: number): bigint;
|
|
71
70
|
readArrayBigInt(): bigint[];
|
|
72
71
|
readSz(): string;
|
|
73
72
|
readArraySz(): string[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CarbonSerialization.d.ts","sourceRoot":"","sources":["../../../../src/core/types/CarbonSerialization.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,qCAAqC,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAK9C,qBAAa,KAAK;IAChB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG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G,IAAI;IAK9B,QAAQ,CAAC,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC;IAKrD,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE;IAa5D,UAAU,CAAC,aAAa,SAAK,GAAG,MAAM;IAqBtC,eAAe,IAAI,MAAM,EAAE;IAQ3B,MAAM,IAAI,MAAM;IAYhB,WAAW,IAAI,MAAM,EAAE;IAQvB,SAAS,IAAI,UAAU;IAIvB,iBAAiB,IAAI,UAAU,EAAE;IAOjC,YAAY,IAAI,MAAM,EAAE;IAMxB,WAAW,IAAI,MAAM,EAAE;IAMvB,WAAW,IAAI,MAAM,EAAE;IAMvB,WAAW,IAAI,MAAM,EAAE;IAMvB,UAAU,IAAI,MAAM,EAAE;CAMvB;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CA0BlE;AA8CD,6DAA6D;AAC7D,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAelE;AAGD,wBAAgB,SAAS,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,CAErF;AACD,wBAAgB,QAAQ,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,EAAE,kBAAkB,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,CAI3F"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phantasma-sdk-ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Typescript SDK for interacting with the Phantasma Chain",
|
|
5
5
|
"author": "Phantasma Team",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
},
|
|
65
65
|
"homepage": "https://github.com/phantasma-io/phantasma-ts#readme",
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@ledgerhq/hw-transport-node-hid": "^6.
|
|
68
|
-
"@ledgerhq/hw-transport-webusb": "^6.
|
|
67
|
+
"@ledgerhq/hw-transport-node-hid": "^6.31.0",
|
|
68
|
+
"@ledgerhq/hw-transport-webusb": "^6.31.0",
|
|
69
69
|
"big-integer": "^1.6.52",
|
|
70
70
|
"bip32": "^3.1.0",
|
|
71
71
|
"bip39": "^3.1.0",
|
|
@@ -84,13 +84,13 @@
|
|
|
84
84
|
"@types/jest": "^30.0.0",
|
|
85
85
|
"@types/wif": "^2.0.5",
|
|
86
86
|
"browserify": "^17.0.1",
|
|
87
|
-
"eslint": "^9.
|
|
87
|
+
"eslint": "^9.39.4",
|
|
88
88
|
"eslint-config-prettier": "^10.1.8",
|
|
89
|
-
"globals": "^16.
|
|
90
|
-
"jest": "^30.
|
|
91
|
-
"prettier": "^3.
|
|
92
|
-
"ts-jest": "^29.4.
|
|
89
|
+
"globals": "^16.5.0",
|
|
90
|
+
"jest": "^30.3.0",
|
|
91
|
+
"prettier": "^3.8.1",
|
|
92
|
+
"ts-jest": "^29.4.6",
|
|
93
93
|
"typescript": "^4.9.5",
|
|
94
|
-
"typescript-eslint": "^8.
|
|
94
|
+
"typescript-eslint": "^8.57.0"
|
|
95
95
|
}
|
|
96
96
|
}
|