Skip to content

Commit c2ca5fa

Browse files
committed
bigint values for Doge compatibility
1 parent 52739b3 commit c2ca5fa

25 files changed

Lines changed: 208 additions & 149 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { KeyValue, WitnessUtxo } from '../../interfaces';
22
export declare function decode(keyVal: KeyValue): WitnessUtxo;
33
export declare function encode(data: WitnessUtxo): KeyValue;
4-
export declare const expected = "{ script: Buffer; value: number; }";
4+
export declare const expected = "{ script: Buffer; value: bigint; }";
55
export declare function check(data: any): data is WitnessUtxo;
66
export declare function canAdd(currentData: any, newData: any): boolean;

src/lib/converter/input/witnessUtxo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function encode(data) {
3737
};
3838
}
3939
exports.encode = encode;
40-
exports.expected = '{ script: Buffer; value: number; }';
40+
exports.expected = '{ script: Buffer; value: bigint; }';
4141
function check(data) {
42-
return Buffer.isBuffer(data.script) && typeof data.value === 'number';
42+
return Buffer.isBuffer(data.script) && typeof data.value === 'bigint';
4343
}
4444
exports.check = check;
4545
function canAdd(currentData, newData) {

src/lib/converter/tools.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export declare const range: (n: number) => number[];
44
export declare function reverseBuffer(buffer: Buffer): Buffer;
55
export declare function keyValsToBuffer(keyVals: KeyValue[]): Buffer;
66
export declare function keyValToBuffer(keyVal: KeyValue): Buffer;
7-
export declare function readUInt64LE(buffer: Buffer, offset: number): number;
8-
export declare function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
7+
export declare function readUInt64LE(buffer: Buffer, offset: number): bigint;
8+
export declare function writeUInt64LE(buffer: Buffer, value: bigint, offset: number): number;

src/lib/converter/tools.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,26 @@ function keyValToBuffer(keyVal) {
3636
return buffer;
3737
}
3838
exports.keyValToBuffer = keyValToBuffer;
39-
// https://github.com/feross/buffer/blob/master/index.js#L1127
40-
function verifuint(value, max) {
41-
if (typeof value !== 'number')
42-
throw new Error('cannot write a non-number as a number');
39+
function verifuint64(value) {
40+
if (typeof value !== 'bigint')
41+
throw new Error('cannot write a non-bigint as a number');
4342
if (value < 0)
4443
throw new Error('specified a negative value for writing an unsigned value');
45-
if (value > max) throw new Error('RangeError: value out of range');
46-
if (Math.floor(value) !== value)
47-
throw new Error('value has a fractional component');
44+
if (value > 0xffffffffffffffff)
45+
throw new Error('RangeError: value out of range');
4846
}
4947
function readUInt64LE(buffer, offset) {
50-
const a = buffer.readUInt32LE(offset);
51-
let b = buffer.readUInt32LE(offset + 4);
52-
b *= 0x100000000;
53-
verifuint(b + a, 0x001fffffffffffff);
48+
const a = BigInt(buffer.readUInt32LE(offset));
49+
let b = BigInt(buffer.readUInt32LE(offset + 4));
50+
b *= BigInt(0x100000000);
51+
verifuint64(b + a);
5452
return b + a;
5553
}
5654
exports.readUInt64LE = readUInt64LE;
5755
function writeUInt64LE(buffer, value, offset) {
58-
verifuint(value, 0x001fffffffffffff);
59-
buffer.writeInt32LE(value & -1, offset);
60-
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4);
56+
verifuint64(value);
57+
buffer.writeUInt32LE(Number(value & BigInt(0xffffffff)), offset);
58+
buffer.writeUInt32LE(Number(value / BigInt(0x100000000)), offset + 4);
6159
return offset + 8;
6260
}
6361
exports.writeUInt64LE = writeUInt64LE;

src/lib/interfaces.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export interface Bip32Derivation {
7474
}
7575
export interface WitnessUtxo {
7676
script: Buffer;
77-
value: number;
77+
value: bigint;
7878
}
7979
export declare type NonWitnessUtxo = Buffer;
8080
export declare type SighashType = number;

src/lib/utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="node" />
22
import { KeyValue, PsbtGlobal, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from './interfaces';
3+
export declare function toJson(parsed: any): string;
34
export declare function checkForInput(inputs: PsbtInput[], inputIndex: number): PsbtInput;
45
export declare function checkForOutput(outputs: PsbtOutput[], outputIndex: number): PsbtOutput;
56
export declare function checkHasKey(checkKeyVal: KeyValue, keyVals: KeyValue[] | undefined, enumLength: number): void;

src/lib/utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
33
const converter = require('./converter');
4+
function toJson(parsed) {
5+
return JSON.stringify(
6+
parsed,
7+
(key, value) => {
8+
if (key === undefined) return value;
9+
if (value.type === 'Buffer')
10+
return `0x${Buffer.from(value.data).toString('hex')}`;
11+
if (typeof value === 'bigint') return value.toString() + 'n';
12+
return value;
13+
},
14+
2,
15+
);
16+
}
17+
exports.toJson = toJson;
418
function checkForInput(inputs, inputIndex) {
519
const input = inputs[inputIndex];
620
if (input === undefined) throw new Error(`No input #${inputIndex}`);
@@ -57,7 +71,7 @@ exports.inputCheckUncleanFinalized = inputCheckUncleanFinalized;
5771
function throwForUpdateMaker(typeName, name, expected, data) {
5872
throw new Error(
5973
`Data for ${typeName} key ${name} is incorrect: Expected ` +
60-
`${expected} and got ${JSON.stringify(data)}`,
74+
`${expected} and got ${toJson(data)}`,
6175
);
6276
}
6377
function updateMaker(typeName) {

src/tests/combine.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@
22
Object.defineProperty(exports, '__esModule', { value: true });
33
const tape = require('tape');
44
const psbt_1 = require('../lib/psbt');
5+
const utils_1 = require('../lib/utils');
56
const combine_1 = require('./fixtures/combine');
7+
const json_1 = require('./utils/json');
68
const txTools_1 = require('./utils/txTools');
79
for (const f of combine_1.fixtures) {
810
tape('Test: ' + f.description, t => {
911
const psbts = f.psbts.map(p =>
1012
psbt_1.Psbt.fromHex(p, txTools_1.transactionFromBuffer),
1113
);
12-
const jsonA1 = jsonify(psbts[0]);
13-
const jsonA2 = jsonify(psbts[1]);
14+
const jsonA1 = utils_1.toJson(psbts[0]);
15+
const jsonA2 = utils_1.toJson(psbts[1]);
1416
psbts[0].combine(psbts[1]);
15-
const jsonB1 = jsonify(psbts[0]);
16-
const jsonB2 = jsonify(psbts[1]);
17+
const jsonB1 = utils_1.toJson(psbts[0]);
18+
const jsonB2 = utils_1.toJson(psbts[1]);
1719
// console.log(jsonA1);
1820
// console.log(jsonA2);
1921
// console.log(jsonB1);
2022
// console.log(jsonB2);
21-
t.notDeepEqual(JSON.parse(jsonA1), JSON.parse(jsonB1));
22-
t.deepEqual(JSON.parse(jsonA2), JSON.parse(jsonB2));
23+
t.notDeepEqual(json_1.fromJson(jsonA1), json_1.fromJson(jsonB1));
24+
t.deepEqual(json_1.fromJson(jsonA2), json_1.fromJson(jsonB2));
2325
t.equal(psbts[0].toHex(), f.result);
2426
t.end();
2527
});
2628
}
27-
function jsonify(parsed) {
28-
return JSON.stringify(
29-
parsed,
30-
(key, value) => {
31-
return key !== undefined && value.type === 'Buffer'
32-
? Buffer.from(value.data).toString('hex')
33-
: value;
34-
},
35-
2,
36-
);
37-
}

src/tests/first.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Object.defineProperty(exports, '__esModule', { value: true });
33
const tape = require('tape');
44
const psbt_1 = require('../lib/psbt');
5+
const utils_1 = require('../lib/utils');
56
const first_1 = require('./fixtures/first');
7+
const json_1 = require('./utils/json');
68
const txTools_1 = require('./utils/txTools');
79
for (const f of first_1.fixtures) {
810
tape('Test: ' + f.description, t => {
@@ -18,19 +20,8 @@ for (const f of first_1.fixtures) {
1820
t.strictEqual(parsed.toHex(), parsed3.toHex());
1921
// @ts-ignore
2022
parsed3.globalMap.unsignedTx = parsed3.globalMap.unsignedTx.toBuffer();
21-
t.deepEqual(JSON.parse(jsonify(parsed3)), f.output);
23+
t.deepEqual(json_1.fromJson(utils_1.toJson(parsed3)), f.output);
2224
t.equal(hex, hex2);
2325
t.end();
2426
});
2527
}
26-
function jsonify(parsed) {
27-
return JSON.stringify(
28-
parsed,
29-
(key, value) => {
30-
return key !== undefined && value !== undefined && value.type === 'Buffer'
31-
? Buffer.from(value.data).toString('hex')
32-
: value;
33-
},
34-
2,
35-
);
36-
}

src/tests/fixtures/create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ exports.fixtures = [
6262
{
6363
witnessUtxo: {
6464
script: b('a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e887'),
65-
value: 200000000,
65+
value: BigInt(200000000),
6666
},
6767
redeemScript: b(
6868
'00208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903',

0 commit comments

Comments
 (0)