ecash-lib 0.1.0-rc
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/README.md +37 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/io/bytes.d.ts +20 -0
- package/dist/io/bytes.d.ts.map +1 -0
- package/dist/io/bytes.js +54 -0
- package/dist/io/bytes.js.map +1 -0
- package/dist/io/bytes.test.d.ts +2 -0
- package/dist/io/bytes.test.d.ts.map +1 -0
- package/dist/io/bytes.test.js +39 -0
- package/dist/io/bytes.test.js.map +1 -0
- package/dist/io/hex.d.ts +5 -0
- package/dist/io/hex.d.ts.map +1 -0
- package/dist/io/hex.js +65 -0
- package/dist/io/hex.js.map +1 -0
- package/dist/io/int.d.ts +3 -0
- package/dist/io/int.d.ts.map +1 -0
- package/dist/io/int.js +5 -0
- package/dist/io/int.js.map +1 -0
- package/dist/io/varsize.d.ts +16 -0
- package/dist/io/varsize.d.ts.map +1 -0
- package/dist/io/varsize.js +52 -0
- package/dist/io/varsize.js.map +1 -0
- package/dist/io/varsize.test.d.ts +2 -0
- package/dist/io/varsize.test.d.ts.map +1 -0
- package/dist/io/varsize.test.js +81 -0
- package/dist/io/varsize.test.js.map +1 -0
- package/dist/io/writer.d.ts +15 -0
- package/dist/io/writer.d.ts.map +1 -0
- package/dist/io/writer.js +5 -0
- package/dist/io/writer.js.map +1 -0
- package/dist/io/writerbytes.d.ts +27 -0
- package/dist/io/writerbytes.d.ts.map +1 -0
- package/dist/io/writerbytes.js +67 -0
- package/dist/io/writerbytes.js.map +1 -0
- package/dist/io/writerbytes.test.d.ts +2 -0
- package/dist/io/writerbytes.test.d.ts.map +1 -0
- package/dist/io/writerbytes.test.js +37 -0
- package/dist/io/writerbytes.test.js.map +1 -0
- package/dist/io/writerlength.d.ts +21 -0
- package/dist/io/writerlength.d.ts.map +1 -0
- package/dist/io/writerlength.js +33 -0
- package/dist/io/writerlength.js.map +1 -0
- package/dist/io/writerlength.test.d.ts +2 -0
- package/dist/io/writerlength.test.d.ts.map +1 -0
- package/dist/io/writerlength.test.js +30 -0
- package/dist/io/writerlength.test.js.map +1 -0
- package/dist/script.d.ts +13 -0
- package/dist/script.d.ts.map +1 -0
- package/dist/script.js +20 -0
- package/dist/script.js.map +1 -0
- package/dist/script.test.d.ts +2 -0
- package/dist/script.test.d.ts.map +1 -0
- package/dist/script.test.js +25 -0
- package/dist/script.test.js.map +1 -0
- package/dist/tx.d.ts +72 -0
- package/dist/tx.d.ts.map +1 -0
- package/dist/tx.js +61 -0
- package/dist/tx.js.map +1 -0
- package/dist/tx.test.d.ts +2 -0
- package/dist/tx.test.d.ts.map +1 -0
- package/dist/tx.test.js +71 -0
- package/dist/tx.test.js.map +1 -0
- package/package.json +35 -0
- package/tsconfig.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# ecash-lib
|
|
2
|
+
|
|
3
|
+
Library for eCash transaction building.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Tx, Script, fromHex, toHex } from 'ecash-lib';
|
|
9
|
+
|
|
10
|
+
const tx = new Tx({
|
|
11
|
+
inputs: [
|
|
12
|
+
{
|
|
13
|
+
prevOut: {
|
|
14
|
+
txid: new Uint8Array(32),
|
|
15
|
+
outIdx: 0,
|
|
16
|
+
},
|
|
17
|
+
script: new Script(),
|
|
18
|
+
sequence: 0xffffffff,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
outputs: [
|
|
22
|
+
{
|
|
23
|
+
value: 0,
|
|
24
|
+
script: new Script(fromHex('6a68656c6c6f')),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
value: 546,
|
|
28
|
+
script: new Script(
|
|
29
|
+
fromHex('a914d37c4c809fe9840e7bfa77b86bd47163f6fb6c6087'),
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const rawTx = tx.ser();
|
|
36
|
+
console.log(toHex(rawTx));
|
|
37
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './script.js';
|
|
2
|
+
export * from './tx.js';
|
|
3
|
+
export * from './io/bytes.js';
|
|
4
|
+
export * from './io/hex.js';
|
|
5
|
+
export * from './io/int.js';
|
|
6
|
+
export * from './io/varsize.js';
|
|
7
|
+
export * from './io/writer.js';
|
|
8
|
+
export * from './io/writerbytes.js';
|
|
9
|
+
export * from './io/writerlength.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
export * from './script.js';
|
|
5
|
+
export * from './tx.js';
|
|
6
|
+
export * from './io/bytes.js';
|
|
7
|
+
export * from './io/hex.js';
|
|
8
|
+
export * from './io/int.js';
|
|
9
|
+
export * from './io/varsize.js';
|
|
10
|
+
export * from './io/writer.js';
|
|
11
|
+
export * from './io/writerbytes.js';
|
|
12
|
+
export * from './io/writerlength.js';
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Reads ints/bytes from a Uint8Array. All integers are little-endian. */
|
|
2
|
+
export declare class Bytes {
|
|
3
|
+
data: Uint8Array;
|
|
4
|
+
view: DataView;
|
|
5
|
+
idx: number;
|
|
6
|
+
/** Create a new Bytes that reads from the given data */
|
|
7
|
+
constructor(data: Uint8Array);
|
|
8
|
+
/** Read a single byte */
|
|
9
|
+
readU8(): number;
|
|
10
|
+
/** Read 2-byte little-endian integer (uint16_t) */
|
|
11
|
+
readU16(): number;
|
|
12
|
+
/** Read 4-byte little-endian integer (uint32_t) */
|
|
13
|
+
readU32(): number;
|
|
14
|
+
/** Read 8-byte little-endian integer (uint64_t) */
|
|
15
|
+
readU64(): bigint;
|
|
16
|
+
/** Read the given number of bytes as array */
|
|
17
|
+
readBytes(numBytes: number): Uint8Array;
|
|
18
|
+
private ensureSize;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/io/bytes.ts"],"names":[],"mappings":"AAIA,0EAA0E;AAC1E,qBAAa,KAAK;IACP,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IAEnB,wDAAwD;gBACrC,IAAI,EAAE,UAAU;IAUnC,yBAAyB;IAClB,MAAM,IAAI,MAAM;IAOvB,mDAAmD;IAC5C,OAAO,IAAI,MAAM;IAOxB,mDAAmD;IAC5C,OAAO,IAAI,MAAM;IAOxB,mDAAmD;IAC5C,OAAO,IAAI,MAAM;IAOxB,8CAA8C;IACvC,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU;IAO9C,OAAO,CAAC,UAAU;CAQrB"}
|
package/dist/io/bytes.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
/** Reads ints/bytes from a Uint8Array. All integers are little-endian. */
|
|
5
|
+
export class Bytes {
|
|
6
|
+
/** Create a new Bytes that reads from the given data */
|
|
7
|
+
constructor(data) {
|
|
8
|
+
this.data = data;
|
|
9
|
+
this.view = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
|
10
|
+
this.idx = 0;
|
|
11
|
+
}
|
|
12
|
+
/** Read a single byte */
|
|
13
|
+
readU8() {
|
|
14
|
+
this.ensureSize(1);
|
|
15
|
+
const result = this.data[this.idx];
|
|
16
|
+
this.idx++;
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
/** Read 2-byte little-endian integer (uint16_t) */
|
|
20
|
+
readU16() {
|
|
21
|
+
this.ensureSize(2);
|
|
22
|
+
const result = this.view.getUint16(this.idx, true);
|
|
23
|
+
this.idx += 2;
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
/** Read 4-byte little-endian integer (uint32_t) */
|
|
27
|
+
readU32() {
|
|
28
|
+
this.ensureSize(4);
|
|
29
|
+
const result = this.view.getUint32(this.idx, true);
|
|
30
|
+
this.idx += 4;
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** Read 8-byte little-endian integer (uint64_t) */
|
|
34
|
+
readU64() {
|
|
35
|
+
this.ensureSize(8);
|
|
36
|
+
const result = this.view.getBigUint64(this.idx, true);
|
|
37
|
+
this.idx += 8;
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
/** Read the given number of bytes as array */
|
|
41
|
+
readBytes(numBytes) {
|
|
42
|
+
this.ensureSize(numBytes);
|
|
43
|
+
const result = this.data.slice(this.idx, this.idx + numBytes);
|
|
44
|
+
this.idx += numBytes;
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
ensureSize(extraBytes) {
|
|
48
|
+
if (this.data.length < this.idx + extraBytes) {
|
|
49
|
+
throw (`Not enough bytes: Tried reading ${extraBytes} byte(s), but ` +
|
|
50
|
+
`there are only ${this.data.length - this.idx} byte(s) left`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/io/bytes.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,0EAA0E;AAC1E,MAAM,OAAO,KAAK;IAKd,wDAAwD;IACxD,YAAmB,IAAgB;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,QAAQ,CACpB,IAAI,CAAC,IAAI,CAAC,MAAM,EAChB,IAAI,CAAC,IAAI,CAAC,UAAU,EACpB,IAAI,CAAC,IAAI,CAAC,UAAU,CACvB,CAAC;QACF,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC;IACjB,CAAC;IAED,yBAAyB;IAClB,MAAM;QACT,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACnB,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,CAAC,GAAG,EAAE,CAAC;QACX,OAAO,MAAM,CAAC;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,kBAAkB,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,eAAe,CAC/D,CAAC;QACN,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.test.d.ts","sourceRoot":"","sources":["../../src/io/bytes.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { Bytes } from './bytes.js';
|
|
6
|
+
import { fromHex } from './hex.js';
|
|
7
|
+
describe('Bytes', () => {
|
|
8
|
+
it('Bytes.read* one', () => {
|
|
9
|
+
expect(new Bytes(fromHex('de')).readU8()).to.equal(0xde);
|
|
10
|
+
expect(new Bytes(fromHex('adde')).readU16()).to.equal(0xdead);
|
|
11
|
+
expect(new Bytes(fromHex('efbeadde')).readU32()).to.equal(0xdeadbeef);
|
|
12
|
+
expect(new Bytes(fromHex('fecaad0befbeadde')).readU64()).to.equal(0xdeadbeef0badcafen);
|
|
13
|
+
expect(new Bytes(fromHex('abcdef')).readBytes(3)).to.deep.equal(fromHex('abcdef'));
|
|
14
|
+
});
|
|
15
|
+
it('Bytes.read* multiple', () => {
|
|
16
|
+
const bytes = new Bytes(fromHex('000102030405060708090a0b0c0d0e0f10'));
|
|
17
|
+
expect(bytes.idx).to.equal(0);
|
|
18
|
+
expect(bytes.readU8()).to.equal(0x00);
|
|
19
|
+
expect(bytes.idx).to.equal(1);
|
|
20
|
+
expect(bytes.readU16()).to.equal(0x0201);
|
|
21
|
+
expect(bytes.idx).to.equal(3);
|
|
22
|
+
expect(bytes.readU32()).to.equal(0x06050403);
|
|
23
|
+
expect(bytes.idx).to.equal(7);
|
|
24
|
+
expect(bytes.readU64()).to.equal(0x0e0d0c0b0a090807n);
|
|
25
|
+
expect(bytes.idx).to.equal(15);
|
|
26
|
+
expect(bytes.readBytes(2)).to.deep.equal(fromHex('0f10'));
|
|
27
|
+
});
|
|
28
|
+
it('Bytes.read* failure', () => {
|
|
29
|
+
const bytes = new Bytes(fromHex('000102030405060708090a0b0c0d0e0f10'));
|
|
30
|
+
bytes.readBytes(bytes.data.length);
|
|
31
|
+
expect(() => bytes.readU8()).to.throw('Not enough bytes: Tried reading 1 byte(s), but there are only 0 byte(s) left');
|
|
32
|
+
bytes.idx -= 1; // rewind 1 byte to test error msg
|
|
33
|
+
expect(() => bytes.readU16()).to.throw('Not enough bytes: Tried reading 2 byte(s), but there are only 1 byte(s) left');
|
|
34
|
+
expect(() => bytes.readU32()).to.throw('Not enough bytes: Tried reading 4 byte(s), but there are only 1 byte(s) left');
|
|
35
|
+
expect(() => bytes.readU64()).to.throw('Not enough bytes: Tried reading 8 byte(s), but there are only 1 byte(s) left');
|
|
36
|
+
expect(() => bytes.readBytes(5)).to.throw('Not enough bytes: Tried reading 5 byte(s), but there are only 1 byte(s) left');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=bytes.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.test.js","sourceRoot":"","sources":["../../src/io/bytes.test.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACnB,EAAE,CAAC,iBAAiB,EAAE,GAAG,EAAE;QACvB,MAAM,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK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E,CAAC,KAAK,CAClC,8EAA8E,CACjF,CAAC;QACF,MAAM,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CACrC,8EAA8E,CACjF,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/dist/io/hex.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function toHex(buffer: Uint8Array): string;
|
|
2
|
+
export declare function toHexRev(buffer: Uint8Array): string;
|
|
3
|
+
export declare function fromHex(str: string): Uint8Array;
|
|
4
|
+
export declare function fromHexRev(str: string): Uint8Array;
|
|
5
|
+
//# sourceMappingURL=hex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/io/hex.ts"],"names":[],"mappings":"AA+BA,wBAAgB,KAAK,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAMhD;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAMnD;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAe/C;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAIlD"}
|
package/dist/io/hex.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright (c) 2023-2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
const LUT_HEX_4b = [
|
|
5
|
+
'0',
|
|
6
|
+
'1',
|
|
7
|
+
'2',
|
|
8
|
+
'3',
|
|
9
|
+
'4',
|
|
10
|
+
'5',
|
|
11
|
+
'6',
|
|
12
|
+
'7',
|
|
13
|
+
'8',
|
|
14
|
+
'9',
|
|
15
|
+
'a',
|
|
16
|
+
'b',
|
|
17
|
+
'c',
|
|
18
|
+
'd',
|
|
19
|
+
'e',
|
|
20
|
+
'f',
|
|
21
|
+
];
|
|
22
|
+
const LUT_HEX_8b = new Array(0x100);
|
|
23
|
+
const LUT_BIN_8b = {};
|
|
24
|
+
for (let n = 0; n < 0x100; n++) {
|
|
25
|
+
const hex = `${LUT_HEX_4b[(n >>> 4) & 0xf]}${LUT_HEX_4b[n & 0xf]}`;
|
|
26
|
+
LUT_HEX_8b[n] = hex;
|
|
27
|
+
LUT_BIN_8b[hex] = n;
|
|
28
|
+
}
|
|
29
|
+
// End Pre-Init
|
|
30
|
+
export function toHex(buffer) {
|
|
31
|
+
let out = '';
|
|
32
|
+
for (let idx = 0, edx = buffer.length; idx < edx; ++idx) {
|
|
33
|
+
out += LUT_HEX_8b[buffer[idx]];
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
export function toHexRev(buffer) {
|
|
38
|
+
let out = '';
|
|
39
|
+
for (let idx = buffer.length - 1; idx >= 0; --idx) {
|
|
40
|
+
out += LUT_HEX_8b[buffer[idx]];
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
export function fromHex(str) {
|
|
45
|
+
if ((str.length & 1) != 0) {
|
|
46
|
+
throw new Error(`Odd hex length: ${str}`);
|
|
47
|
+
}
|
|
48
|
+
const nBytes = str.length >> 1;
|
|
49
|
+
const array = new Uint8Array(nBytes);
|
|
50
|
+
for (let idx = 0; idx < str.length; idx += 2) {
|
|
51
|
+
const pair = str.substring(idx, idx + 2);
|
|
52
|
+
const byte = LUT_BIN_8b[pair];
|
|
53
|
+
if (byte === undefined) {
|
|
54
|
+
throw new Error(`Invalid hex pair: ${pair}, at index ${idx}`);
|
|
55
|
+
}
|
|
56
|
+
array[idx >> 1] = byte;
|
|
57
|
+
}
|
|
58
|
+
return array;
|
|
59
|
+
}
|
|
60
|
+
export function fromHexRev(str) {
|
|
61
|
+
const array = fromHex(str);
|
|
62
|
+
array.reverse();
|
|
63
|
+
return array;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=hex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../../src/io/hex.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,mEAAmE;AACnE,sEAAsE;AAEtE,MAAM,UAAU,GAAG;IACf,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;CACN,CAAC;AACF,MAAM,UAAU,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,MAAM,UAAU,GAA8B,EAAE,CAAC;AACjD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;IAC7B,MAAM,GAAG,GAAG,GAAG,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC;IACnE,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;IACpB,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AACD,eAAe;AAEf,MAAM,UAAU,KAAK,CAAC,MAAkB;IACpC,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC;QACtD,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,MAAkB;IACvC,IAAI,G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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int.d.ts","sourceRoot":"","sources":["../../src/io/int.ts"],"names":[],"mappings":"AAIA,qEAAqE;AACrE,MAAM,MAAM,GAAG,GAAG,MAAM,GAAG,MAAM,CAAC"}
|
package/dist/io/int.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int.js","sourceRoot":"","sources":["../../src/io/int.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Bytes } from './bytes.js';
|
|
2
|
+
import { Writer } from './writer.js';
|
|
3
|
+
import { Int } from './int.js';
|
|
4
|
+
/**
|
|
5
|
+
* Read a VARINT, which encodes a size in the Bitcoin protocol, see:
|
|
6
|
+
* https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
7
|
+
*/
|
|
8
|
+
export declare function readVarSize(bytes: Bytes): Int;
|
|
9
|
+
/**
|
|
10
|
+
* Write a VARINT, which encodes a size in the Bitcoin protocol, see:
|
|
11
|
+
* https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
12
|
+
* @param size Size integer to write
|
|
13
|
+
* @param writer Writer to write the size to
|
|
14
|
+
*/
|
|
15
|
+
export declare function writeVarSize(size: Int, writer: Writer): void;
|
|
16
|
+
//# sourceMappingURL=varsize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varsize.d.ts","sourceRoot":"","sources":["../../src/io/varsize.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,GAAG,CAa7C;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,QAerD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
/**
|
|
5
|
+
* Read a VARINT, which encodes a size in the Bitcoin protocol, see:
|
|
6
|
+
* https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
7
|
+
*/
|
|
8
|
+
export function readVarSize(bytes) {
|
|
9
|
+
const firstByte = bytes.readU8();
|
|
10
|
+
if (firstByte <= 0xfc) {
|
|
11
|
+
return firstByte;
|
|
12
|
+
}
|
|
13
|
+
else if (firstByte == 0xfd) {
|
|
14
|
+
return bytes.readU16();
|
|
15
|
+
}
|
|
16
|
+
else if (firstByte == 0xfe) {
|
|
17
|
+
return bytes.readU32();
|
|
18
|
+
}
|
|
19
|
+
else if (firstByte == 0xff) {
|
|
20
|
+
return bytes.readU64();
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw 'Unreachable';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Write a VARINT, which encodes a size in the Bitcoin protocol, see:
|
|
28
|
+
* https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
29
|
+
* @param size Size integer to write
|
|
30
|
+
* @param writer Writer to write the size to
|
|
31
|
+
*/
|
|
32
|
+
export function writeVarSize(size, writer) {
|
|
33
|
+
if (size <= 0xfc) {
|
|
34
|
+
writer.putU8(size);
|
|
35
|
+
}
|
|
36
|
+
else if (size <= 0xffff) {
|
|
37
|
+
writer.putU8(0xfd);
|
|
38
|
+
writer.putU16(size);
|
|
39
|
+
}
|
|
40
|
+
else if (size <= 0xffffffff) {
|
|
41
|
+
writer.putU8(0xfe);
|
|
42
|
+
writer.putU32(size);
|
|
43
|
+
}
|
|
44
|
+
else if (size <= 0xffffffffffffffffn) {
|
|
45
|
+
writer.putU8(0xff);
|
|
46
|
+
writer.putU64(size);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw 'Integer too big for VarSize';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=varsize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varsize.js","sourceRoot":"","sources":["../../src/io/varsize.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAMtE;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAAY;IACpC,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;IACjC,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;QACpB,OAAO,SAAS,CAAC;IACrB,CAAC;SAAM,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;SAAM,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;SAAM,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;SAAM,CAAC;QACJ,MAAM,aAAa,CAAC;IACxB,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAS,EAAE,MAAc;IAClD,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;SAAM,IAAI,IAAI,IAAI,MAAM,EAAE,CAAC;QACxB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;SAAM,IAAI,IAAI,IAAI,UAAU,EAAE,CAAC;QAC5B,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;SAAM,IAAI,IAAI,IAAI,mBAAmB,EAAE,CAAC;QACrC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;SAAM,CAAC;QACJ,MAAM,6BAA6B,CAAC;IACxC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varsize.test.d.ts","sourceRoot":"","sources":["../../src/io/varsize.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { Bytes } from './bytes.js';
|
|
6
|
+
import { fromHex } from './hex.js';
|
|
7
|
+
import { readVarSize, writeVarSize } from './varsize.js';
|
|
8
|
+
import { WriterBytes } from './writerbytes.js';
|
|
9
|
+
describe('VARINT', () => {
|
|
10
|
+
it('readVarSize', () => {
|
|
11
|
+
for (let i = 0; i < 0xfd; ++i) {
|
|
12
|
+
expect(readVarSize(new Bytes(new Uint8Array([i])))).to.equal(i);
|
|
13
|
+
}
|
|
14
|
+
expect(readVarSize(new Bytes(fromHex('fdadde')))).to.equal(0xdead);
|
|
15
|
+
expect(readVarSize(new Bytes(fromHex('feefbeadde')))).to.equal(0xdeadbeef);
|
|
16
|
+
expect(readVarSize(new Bytes(fromHex('fffecaad0befbeadde')))).to.equal(0xdeadbeef0badcafen);
|
|
17
|
+
});
|
|
18
|
+
it('readVarSize failure', () => {
|
|
19
|
+
expect(() => readVarSize(new Bytes(new Uint8Array([])))).to.throw('Not enough bytes: Tried reading 1 byte(s), but there are only 0 byte(s) left');
|
|
20
|
+
expect(() => readVarSize(new Bytes(fromHex('fd00')))).to.throw('Not enough bytes: Tried reading 2 byte(s), but there are only 1 byte(s) left');
|
|
21
|
+
expect(() => readVarSize(new Bytes(fromHex('fe010203')))).to.throw('Not enough bytes: Tried reading 4 byte(s), but there are only 3 byte(s) left');
|
|
22
|
+
expect(() => readVarSize(new Bytes(fromHex('ff01020304050607')))).to.throw('Not enough bytes: Tried reading 8 byte(s), but there are only 7 byte(s) left');
|
|
23
|
+
});
|
|
24
|
+
it('writeVarSize 0x00-0xfc', () => {
|
|
25
|
+
for (let i = 0; i < 0xfd; ++i) {
|
|
26
|
+
const writer = new WriterBytes(1);
|
|
27
|
+
writeVarSize(i, writer);
|
|
28
|
+
expect(writer.data).to.deep.equal(new Uint8Array([i]));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
it('writeVarSize 0xfd', () => {
|
|
32
|
+
for (const num of [0x100, 0x101, 0x1111, 0xffff]) {
|
|
33
|
+
const writer = new WriterBytes(3);
|
|
34
|
+
writeVarSize(num, writer);
|
|
35
|
+
expect(writer.data).to.deep.equal(new Uint8Array([0xfd, num & 0xff, (num & 0xff00) >> 8]));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
it('writeVarSize 0xfe', () => {
|
|
39
|
+
for (const num of [0x10000, 0x10001, 0x11111111, 0xffffffff]) {
|
|
40
|
+
const writer = new WriterBytes(5);
|
|
41
|
+
writeVarSize(num, writer);
|
|
42
|
+
expect(writer.data).to.deep.equal(new Uint8Array([
|
|
43
|
+
0xfe,
|
|
44
|
+
num & 0xff,
|
|
45
|
+
(num & 0xff00) >> 8,
|
|
46
|
+
(num & 0xff0000) >> 16,
|
|
47
|
+
(num & 0xff000000) >> 24,
|
|
48
|
+
]));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
it('writeVarSize 0xff', () => {
|
|
52
|
+
for (const num of [
|
|
53
|
+
0x100000000n,
|
|
54
|
+
0x100000001n,
|
|
55
|
+
0x1111111111111111n,
|
|
56
|
+
0xffffffffffffffffn,
|
|
57
|
+
]) {
|
|
58
|
+
const writer = new WriterBytes(9);
|
|
59
|
+
writeVarSize(num, writer);
|
|
60
|
+
expect(writer.data).to.deep.equal(new Uint8Array([
|
|
61
|
+
0xff,
|
|
62
|
+
Number(num & 0xffn),
|
|
63
|
+
Number((num & 0xff00n) >> 8n),
|
|
64
|
+
Number((num & 0xff0000n) >> 16n),
|
|
65
|
+
Number((num & 0xff000000n) >> 24n),
|
|
66
|
+
Number((num & 0xff00000000n) >> 32n),
|
|
67
|
+
Number((num & 0xff0000000000n) >> 40n),
|
|
68
|
+
Number((num & 0xff000000000000n) >> 48n),
|
|
69
|
+
Number((num & 0xff00000000000000n) >> 56n),
|
|
70
|
+
]));
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
it('writeVarSize failure', () => {
|
|
74
|
+
expect(() => writeVarSize(0, new WriterBytes(0))).to.throw('Not enough bytes: Tried writing 1 byte(s), but only 0 byte(s) have been pre-allocated');
|
|
75
|
+
expect(() => writeVarSize(0xfd, new WriterBytes(2))).to.throw('Not enough bytes: Tried writing 2 byte(s), but only 1 byte(s) have been pre-allocated');
|
|
76
|
+
expect(() => writeVarSize(0x10000, new WriterBytes(4))).to.throw('Not enough bytes: Tried writing 4 byte(s), but only 3 byte(s) have been pre-allocated');
|
|
77
|
+
expect(() => writeVarSize(0x100000000, new WriterBytes(8))).to.throw('Not enough bytes: Tried writing 8 byte(s), but only 7 byte(s) have been pre-allocated');
|
|
78
|
+
expect(() => writeVarSize(0x10000000000000000n, new WriterBytes(0))).to.throw('Integer too big for VarSize');
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
//# sourceMappingURL=varsize.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varsize.test.js","sourceRoot":"","sources":["../../src/io/varsize.test.ts"],"names":[],"mappings":"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AAC,GAAG,EAAE,CACR,YAAY,CAAC,oBAAoB,EAAE,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CACzD,CAAC,EAAE,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Int } from './int.js';
|
|
2
|
+
/** Writer interface to abstract over writing Bitcoin objects */
|
|
3
|
+
export interface Writer {
|
|
4
|
+
/** Write a single byte */
|
|
5
|
+
putU8(value: Int): void;
|
|
6
|
+
/** Write a 2-byte little-endian integer (uint16_t) */
|
|
7
|
+
putU16(value: Int): void;
|
|
8
|
+
/** Write a 4-byte little-endian integer (uint32_t) */
|
|
9
|
+
putU32(value: Int): void;
|
|
10
|
+
/** Write an 8-byte little-endian integer (uint64_t) */
|
|
11
|
+
putU64(value: Int): void;
|
|
12
|
+
/** Write the given bytes */
|
|
13
|
+
putBytes(bytes: Uint8Array): void;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../src/io/writer.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,gEAAgE;AAChE,MAAM,WAAW,MAAM;IACnB,0BAA0B;IAC1B,KAAK,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IACxB,sDAAsD;IACtD,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IACzB,sDAAsD;IACtD,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IACzB,uDAAuD;IACvD,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IACzB,4BAA4B;IAC5B,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;CACrC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writer.js","sourceRoot":"","sources":["../../src/io/writer.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Int } from './int.js';
|
|
2
|
+
import { Writer } from './writer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Implementation of `Writer` which writes to an array of pre-allocated size.
|
|
5
|
+
* It's intended to be used in unison with `WriterLength`, which first finds
|
|
6
|
+
* out the length of the serialized object and then the actual data is written
|
|
7
|
+
* using this class.
|
|
8
|
+
**/
|
|
9
|
+
export declare class WriterBytes implements Writer {
|
|
10
|
+
data: Uint8Array;
|
|
11
|
+
view: DataView;
|
|
12
|
+
idx: number;
|
|
13
|
+
/** Create a new WriterBytes with the given pre-allocated size */
|
|
14
|
+
constructor(length: number);
|
|
15
|
+
/** Write a single byte */
|
|
16
|
+
putU8(value: Int): void;
|
|
17
|
+
/** Write a 2-byte little-endian integer (uint16_t) */
|
|
18
|
+
putU16(value: Int): void;
|
|
19
|
+
/** Write a 4-byte little-endian integer (uint32_t) */
|
|
20
|
+
putU32(value: Int): void;
|
|
21
|
+
/** Write an 8-byte little-endian integer (uint64_t) */
|
|
22
|
+
putU64(value: Int): void;
|
|
23
|
+
/** Write the given bytes */
|
|
24
|
+
putBytes(bytes: Uint8Array): void;
|
|
25
|
+
private ensureSize;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=writerbytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerbytes.d.ts","sourceRoot":"","sources":["../../src/io/writerbytes.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;IAKI;AACJ,qBAAa,WAAY,YAAW,MAAM;IAC/B,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IAEnB,iEAAiE;gBAC9C,MAAM,EAAE,MAAM;IAUjC,0BAA0B;IACnB,KAAK,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAS9B,sDAAsD;IAC/C,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAS/B,sDAAsD;IAC/C,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAS/B,uDAAuD;IAChD,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAS/B,4BAA4B;IACrB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAMxC,OAAO,CAAC,UAAU;CASrB"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of `Writer` which writes to an array of pre-allocated size.
|
|
6
|
+
* It's intended to be used in unison with `WriterLength`, which first finds
|
|
7
|
+
* out the length of the serialized object and then the actual data is written
|
|
8
|
+
* using this class.
|
|
9
|
+
**/
|
|
10
|
+
export class WriterBytes {
|
|
11
|
+
/** Create a new WriterBytes with the given pre-allocated size */
|
|
12
|
+
constructor(length) {
|
|
13
|
+
this.data = new Uint8Array(length);
|
|
14
|
+
this.view = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
|
15
|
+
this.idx = 0;
|
|
16
|
+
}
|
|
17
|
+
/** Write a single byte */
|
|
18
|
+
putU8(value) {
|
|
19
|
+
if (value < 0 || value > 0xff) {
|
|
20
|
+
throw `Cannot fit ${value} into a u8`;
|
|
21
|
+
}
|
|
22
|
+
this.ensureSize(1);
|
|
23
|
+
this.data[this.idx] = Number(value);
|
|
24
|
+
this.idx++;
|
|
25
|
+
}
|
|
26
|
+
/** Write a 2-byte little-endian integer (uint16_t) */
|
|
27
|
+
putU16(value) {
|
|
28
|
+
if (value < 0 || value > 0xffff) {
|
|
29
|
+
throw `Cannot fit ${value} into a u16`;
|
|
30
|
+
}
|
|
31
|
+
this.ensureSize(2);
|
|
32
|
+
this.view.setUint16(this.idx, Number(value), true);
|
|
33
|
+
this.idx += 2;
|
|
34
|
+
}
|
|
35
|
+
/** Write a 4-byte little-endian integer (uint32_t) */
|
|
36
|
+
putU32(value) {
|
|
37
|
+
if (value < 0 || value > 0xffffffff) {
|
|
38
|
+
throw `Cannot fit ${value} into a u32`;
|
|
39
|
+
}
|
|
40
|
+
this.ensureSize(4);
|
|
41
|
+
this.view.setUint32(this.idx, Number(value), true);
|
|
42
|
+
this.idx += 4;
|
|
43
|
+
}
|
|
44
|
+
/** Write an 8-byte little-endian integer (uint64_t) */
|
|
45
|
+
putU64(value) {
|
|
46
|
+
if (value < 0 || value > 0xffffffffffffffffn) {
|
|
47
|
+
throw `Cannot fit ${value} into a u64`;
|
|
48
|
+
}
|
|
49
|
+
this.ensureSize(8);
|
|
50
|
+
this.view.setBigUint64(this.idx, BigInt(value), true);
|
|
51
|
+
this.idx += 8;
|
|
52
|
+
}
|
|
53
|
+
/** Write the given bytes */
|
|
54
|
+
putBytes(bytes) {
|
|
55
|
+
this.ensureSize(bytes.length);
|
|
56
|
+
this.data.set(bytes, this.idx);
|
|
57
|
+
this.idx += bytes.length;
|
|
58
|
+
}
|
|
59
|
+
ensureSize(extraBytes) {
|
|
60
|
+
if (this.data.length < this.idx + extraBytes) {
|
|
61
|
+
throw (`Not enough bytes: Tried writing ${extraBytes} byte(s), but ` +
|
|
62
|
+
`only ${this.data.length - this.idx} byte(s) have been ` +
|
|
63
|
+
`pre-allocated`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=writerbytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerbytes.js","sourceRoot":"","sources":["../../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+
{"version":3,"file":"writerbytes.test.d.ts","sourceRoot":"","sources":["../../src/io/writerbytes.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { fromHex } from './hex.js';
|
|
6
|
+
import { WriterBytes } from './writerbytes.js';
|
|
7
|
+
const wrote = (size, fn) => {
|
|
8
|
+
const writer = new WriterBytes(size);
|
|
9
|
+
fn(writer);
|
|
10
|
+
return writer.data;
|
|
11
|
+
};
|
|
12
|
+
describe('WriterBytes', () => {
|
|
13
|
+
it('WriterBytes.put* one', () => {
|
|
14
|
+
expect(wrote(1, writer => writer.putU8(0xde))).to.deep.equal(fromHex('de'));
|
|
15
|
+
expect(wrote(2, writer => writer.putU16(0xdead))).to.deep.equal(fromHex('adde'));
|
|
16
|
+
expect(wrote(4, writer => writer.putU32(0xdeadbeef))).to.deep.equal(fromHex('efbeadde'));
|
|
17
|
+
expect(wrote(8, writer => writer.putU64(0xdeadbeef0badcafen))).to.deep.equal(fromHex('fecaad0befbeadde'));
|
|
18
|
+
expect(wrote(3, writer => writer.putBytes(fromHex('abcdef')))).to.deep.equal(fromHex('abcdef'));
|
|
19
|
+
});
|
|
20
|
+
it('WriterBytes.put* multiple', () => {
|
|
21
|
+
const writer = new WriterBytes(17);
|
|
22
|
+
writer.putU8(0x44);
|
|
23
|
+
writer.putU16(0x0201);
|
|
24
|
+
writer.putU32(0x06050403);
|
|
25
|
+
writer.putU64(0x0e0d0c0b0a090807n);
|
|
26
|
+
writer.putBytes(fromHex('0f10'));
|
|
27
|
+
expect(writer.data).to.deep.equal(fromHex('440102030405060708090a0b0c0d0e0f10'));
|
|
28
|
+
});
|
|
29
|
+
it('WriterBytes.put* failure', () => {
|
|
30
|
+
expect(() => wrote(0, writer => writer.putU8(0))).to.throw('Not enough bytes: Tried writing 1 byte(s), but only 0 byte(s) have been pre-allocated');
|
|
31
|
+
expect(() => wrote(1, writer => writer.putU16(0))).to.throw('Not enough bytes: Tried writing 2 byte(s), but only 1 byte(s) have been pre-allocated');
|
|
32
|
+
expect(() => wrote(3, writer => writer.putU32(0))).to.throw('Not enough bytes: Tried writing 4 byte(s), but only 3 byte(s) have been pre-allocated');
|
|
33
|
+
expect(() => wrote(7, writer => writer.putU64(0n))).to.throw('Not enough bytes: Tried writing 8 byte(s), but only 7 byte(s) have been pre-allocated');
|
|
34
|
+
expect(() => wrote(2, writer => writer.putBytes(new Uint8Array(3)))).to.throw('Not enough bytes: Tried writing 3 byte(s), but only 2 byte(s) have been pre-allocated');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=writerbytes.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerbytes.test.js","sourceRoot":"","sources":["../../src/io/writerbytes.test.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,EAAiC,EAAE,EAAE;IAC9D,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,CAAC;IACrC,EAAE,CAAC,MAAM,CAAC,CAAC;IACX,OAAO,MAAM,CAAC,IAAI,CAAC;AACvB,CAAC,CAAC;AAEF,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAC5B,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC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
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Int } from './int.js';
|
|
2
|
+
import { Writer } from './writer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Writer implementation which only measures the length of the serialized
|
|
5
|
+
* output but doesn't actually store any byte data.
|
|
6
|
+
**/
|
|
7
|
+
export declare class WriterLength implements Writer {
|
|
8
|
+
length: number;
|
|
9
|
+
constructor();
|
|
10
|
+
/** Write a single byte */
|
|
11
|
+
putU8(_value: Int): void;
|
|
12
|
+
/** Write a 2-byte little-endian integer (uint16_t) */
|
|
13
|
+
putU16(_value: Int): void;
|
|
14
|
+
/** Write a 4-byte little-endian integer (uint32_t) */
|
|
15
|
+
putU32(_value: Int): void;
|
|
16
|
+
/** Write an 8-byte little-endian integer (uint64_t) */
|
|
17
|
+
putU64(_value: Int): void;
|
|
18
|
+
/** Write the given bytes */
|
|
19
|
+
putBytes(bytes: Uint8Array): void;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=writerlength.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerlength.d.ts","sourceRoot":"","sources":["../../src/io/writerlength.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;IAGI;AACJ,qBAAa,YAAa,YAAW,MAAM;IAChC,MAAM,EAAE,MAAM,CAAC;;IAMtB,0BAA0B;IACnB,KAAK,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAI/B,sDAAsD;IAC/C,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAIhC,sDAAsD;IAC/C,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAIhC,uDAAuD;IAChD,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAIhC,4BAA4B;IACrB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;CAG3C"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
/**
|
|
5
|
+
* Writer implementation which only measures the length of the serialized
|
|
6
|
+
* output but doesn't actually store any byte data.
|
|
7
|
+
**/
|
|
8
|
+
export class WriterLength {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.length = 0;
|
|
11
|
+
}
|
|
12
|
+
/** Write a single byte */
|
|
13
|
+
putU8(_value) {
|
|
14
|
+
this.length++;
|
|
15
|
+
}
|
|
16
|
+
/** Write a 2-byte little-endian integer (uint16_t) */
|
|
17
|
+
putU16(_value) {
|
|
18
|
+
this.length += 2;
|
|
19
|
+
}
|
|
20
|
+
/** Write a 4-byte little-endian integer (uint32_t) */
|
|
21
|
+
putU32(_value) {
|
|
22
|
+
this.length += 4;
|
|
23
|
+
}
|
|
24
|
+
/** Write an 8-byte little-endian integer (uint64_t) */
|
|
25
|
+
putU64(_value) {
|
|
26
|
+
this.length += 8;
|
|
27
|
+
}
|
|
28
|
+
/** Write the given bytes */
|
|
29
|
+
putBytes(bytes) {
|
|
30
|
+
this.length += bytes.length;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=writerlength.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerlength.js","sourceRoot":"","sources":["../../src/io/writerlength.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAKtE;;;IAGI;AACJ,MAAM,OAAO,YAAY;IAGrB;QACI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IACpB,CAAC;IAED,0BAA0B;IACnB,KAAK,CAAC,MAAW;QACpB,IAAI,CAAC,MAAM,EAAE,CAAC;IAClB,CAAC;IAED,sDAAsD;IAC/C,MAAM,CAAC,MAAW;QACrB,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;IACrB,CAAC;IAED,sDAAsD;IAC/C,MAAM,CAAC,MAAW;QACrB,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;IACrB,CAAC;IAED,uDAAuD;IAChD,MAAM,CAAC,MAAW;QACrB,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;IACrB,CAAC;IAED,4BAA4B;IACrB,QAAQ,CAAC,KAAiB;QAC7B,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAChC,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerlength.test.d.ts","sourceRoot":"","sources":["../../src/io/writerlength.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { fromHex } from './hex.js';
|
|
6
|
+
import { WriterLength } from './writerlength.js';
|
|
7
|
+
const wrote = (fn) => {
|
|
8
|
+
const writer = new WriterLength();
|
|
9
|
+
fn(writer);
|
|
10
|
+
return writer.length;
|
|
11
|
+
};
|
|
12
|
+
describe('WriterLength', () => {
|
|
13
|
+
it('WriterLength.put* one', () => {
|
|
14
|
+
expect(wrote(writer => writer.putU8(0xde))).to.deep.equal(1);
|
|
15
|
+
expect(wrote(writer => writer.putU16(0xdead))).to.deep.equal(2);
|
|
16
|
+
expect(wrote(writer => writer.putU32(0xdeadbeef))).to.deep.equal(4);
|
|
17
|
+
expect(wrote(writer => writer.putU64(0xdeadbeef0badcafen))).to.deep.equal(8);
|
|
18
|
+
expect(wrote(writer => writer.putBytes(fromHex('abcdef')))).to.deep.equal(3);
|
|
19
|
+
});
|
|
20
|
+
it('WriterLength.put* multiple', () => {
|
|
21
|
+
const writer = new WriterLength();
|
|
22
|
+
writer.putU8(0x44);
|
|
23
|
+
writer.putU16(0x0201);
|
|
24
|
+
writer.putU32(0x06050403);
|
|
25
|
+
writer.putU64(0x0e0d0c0b0a090807n);
|
|
26
|
+
writer.putBytes(fromHex('0f10'));
|
|
27
|
+
expect(writer.length).to.deep.equal(17);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=writerlength.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writerlength.test.js","sourceRoot":"","sources":["../../src/io/writerlength.test.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,KAAK,GAAG,CAAC,EAAkC,EAAE,EAAE;IACjD,MAAM,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,EAAE,CAAC,MAAM,CAAC,CAAC;IACX,OAAO,MAAM,CAAC,MAAM,CAAC;AACzB,CAAC,CAAC;AAEF,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,uBAAuB,EAAE,GAAG,EAAE;QAC7B,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7D,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAChE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACpE,MAAM,CACF,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,CACtD,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,MAAM,CACF,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CACtD,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,4BAA4B,EAAE,GAAG,EAAE;QAClC,MAAM,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;QAClC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtB,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC1B,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QACnC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;QACjC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/dist/script.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Writer } from './io/writer.js';
|
|
2
|
+
/** A Bitcoin Script locking/unlocking a UTXO */
|
|
3
|
+
export declare class Script {
|
|
4
|
+
bytecode: Uint8Array;
|
|
5
|
+
/** Create a new Script with the given bytecode or empty */
|
|
6
|
+
constructor(bytecode?: Uint8Array);
|
|
7
|
+
/**
|
|
8
|
+
* Write the script to the writer with the script size as VARINT
|
|
9
|
+
* prepended.
|
|
10
|
+
**/
|
|
11
|
+
writeWithSize(writer: Writer): void;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=script.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,gDAAgD;AAChD,qBAAa,MAAM;IACR,QAAQ,EAAE,UAAU,CAAC;IAE5B,2DAA2D;gBACxC,QAAQ,CAAC,EAAE,UAAU;IAIxC;;;QAGI;IACG,aAAa,CAAC,MAAM,EAAE,MAAM;CAItC"}
|
package/dist/script.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { writeVarSize } from './io/varsize.js';
|
|
5
|
+
/** A Bitcoin Script locking/unlocking a UTXO */
|
|
6
|
+
export class Script {
|
|
7
|
+
/** Create a new Script with the given bytecode or empty */
|
|
8
|
+
constructor(bytecode) {
|
|
9
|
+
this.bytecode = bytecode ?? new Uint8Array();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Write the script to the writer with the script size as VARINT
|
|
13
|
+
* prepended.
|
|
14
|
+
**/
|
|
15
|
+
writeWithSize(writer) {
|
|
16
|
+
writeVarSize(this.bytecode.length, writer);
|
|
17
|
+
writer.putBytes(this.bytecode);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=script.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"script.js","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG/C,gDAAgD;AAChD,MAAM,OAAO,MAAM;IAGf,2DAA2D;IAC3D,YAAmB,QAAqB;QACpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,IAAI,UAAU,EAAE,CAAC;IACjD,CAAC;IAED;;;QAGI;IACG,aAAa,CAAC,MAAc;QAC/B,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3C,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"script.test.d.ts","sourceRoot":"","sources":["../src/script.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { fromHex, toHex } from './io/hex.js';
|
|
6
|
+
import { WriterBytes } from './io/writerbytes.js';
|
|
7
|
+
import { Script } from './script.js';
|
|
8
|
+
const wrote = (size, fn) => {
|
|
9
|
+
const writer = new WriterBytes(size);
|
|
10
|
+
fn(writer);
|
|
11
|
+
return writer.data;
|
|
12
|
+
};
|
|
13
|
+
describe('Script', () => {
|
|
14
|
+
it('Script.writeWithSize', () => {
|
|
15
|
+
expect(wrote(1, writer => new Script().writeWithSize(writer))).to.deep.equal(fromHex('00'));
|
|
16
|
+
expect(wrote(2, writer => new Script(new Uint8Array([0x42])).writeWithSize(writer))).to.deep.equal(fromHex('0142'));
|
|
17
|
+
expect(wrote(5, writer => new Script(fromHex('deadbeef')).writeWithSize(writer))).to.deep.equal(fromHex('04deadbeef'));
|
|
18
|
+
expect(wrote(9, writer => new Script(fromHex('deadbeef0badcafe')).writeWithSize(writer))).to.deep.equal(fromHex('08deadbeef0badcafe'));
|
|
19
|
+
expect(wrote(26, writer => new Script(fromHex('76a914222222222222222222222222222222222222222288ac')).writeWithSize(writer))).to.deep.equal(fromHex('1976a914222222222222222222222222222222222222222288ac'));
|
|
20
|
+
expect(wrote(24, writer => new Script(fromHex('a914333333333333333333333333333333333333333387')).writeWithSize(writer))).to.deep.equal(fromHex('17a914333333333333333333333333333333333333333387'));
|
|
21
|
+
expect(wrote(0xfc + 1, writer => new Script(new Uint8Array(0xfc)).writeWithSize(writer))).to.deep.equal(fromHex('fc' + '00'.repeat(0xfc)));
|
|
22
|
+
expect(toHex(wrote(0xfd + 3, writer => new Script(new Uint8Array(0xfd)).writeWithSize(writer)))).to.deep.equal('fdfd00' + '00'.repeat(0xfd));
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=script.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"script.test.js","sourceRoot":"","sources":["../src/script.test.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,EAAiC,EAAE,EAAE;IAC9D,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,CAAC;IACrC,EAAE,CAAC,MAAM,CAAC,CAAC;IACX,OAAO,MAAM,CAAC,IAAI,CAAC;AACvB,CAAC,CAAC;AAEF,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAC5B,MAAM,CACF,KAAK,CAAC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AgD,CAAC,CAC5D,CAAC,aAAa,CAAC,MAAM,CAAC,CAC1B,CACJ,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CACX,OAAO,CAAC,kDAAkD,CAAC,CAC9D,CAAC;QAEF,MAAM,CACF,KAAK,CAAC,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,CACrB,IAAI,MAAM,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CACzD,CACJ,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAEnD,MAAM,CACF,KAAK,CACD,KAAK,CAAC,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,CACrB,IAAI,MAAM,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CACzD,CACJ,CACJ,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/dist/tx.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Writer } from './io/writer.js';
|
|
2
|
+
import { Script } from './script.js';
|
|
3
|
+
/** COutPoint, pointing to a coin being spent. */
|
|
4
|
+
export interface OutPoint {
|
|
5
|
+
/**
|
|
6
|
+
* TxId of the output of the coin, either a big-endian hex encoded TxId
|
|
7
|
+
* or a little-endian bytearray.
|
|
8
|
+
**/
|
|
9
|
+
txid: string | Uint8Array;
|
|
10
|
+
/** Index in the outputs of the tx of the coin. */
|
|
11
|
+
outIdx: number;
|
|
12
|
+
}
|
|
13
|
+
/** CTxIn, spending an unspent output. */
|
|
14
|
+
export interface TxInput {
|
|
15
|
+
/** Points to an output being spent. */
|
|
16
|
+
prevOut: OutPoint;
|
|
17
|
+
/** scriptSig unlocking the output. */
|
|
18
|
+
script: Script;
|
|
19
|
+
/** nSequence. */
|
|
20
|
+
sequence: number;
|
|
21
|
+
/** Sign data required to sign an input */
|
|
22
|
+
signData?: SignData;
|
|
23
|
+
}
|
|
24
|
+
/** CTxOut, creating a new output. */
|
|
25
|
+
export interface TxOutput {
|
|
26
|
+
/** Value in satoshis of the output (1 XEC = 100 satoshis) */
|
|
27
|
+
value: number | bigint;
|
|
28
|
+
/** Script locking the output */
|
|
29
|
+
script: Script;
|
|
30
|
+
}
|
|
31
|
+
/** All the data required to sign an input (using BIP143). */
|
|
32
|
+
export interface SignData {
|
|
33
|
+
/** Value of the output being spent */
|
|
34
|
+
value: number | bigint;
|
|
35
|
+
/** Script of the output being spent (not for P2SH) */
|
|
36
|
+
outputScript?: Script;
|
|
37
|
+
/**
|
|
38
|
+
* For P2SH inputs, the preimage of the script hash locking the output being
|
|
39
|
+
* spent.
|
|
40
|
+
**/
|
|
41
|
+
redeemScript?: Script;
|
|
42
|
+
}
|
|
43
|
+
/** CTransaction, a Bitcoin transaction. */
|
|
44
|
+
export declare class Tx {
|
|
45
|
+
/** nVersion of the tx */
|
|
46
|
+
version: number;
|
|
47
|
+
/** vin, tx inputs spending outputs of other txs */
|
|
48
|
+
inputs: TxInput[];
|
|
49
|
+
/** vout, tx outputs created in this tx */
|
|
50
|
+
outputs: TxOutput[];
|
|
51
|
+
/** nLockTime of the tx, specifies when the tx can be mined earliest */
|
|
52
|
+
locktime: number;
|
|
53
|
+
constructor(params?: {
|
|
54
|
+
version?: number;
|
|
55
|
+
inputs?: TxInput[];
|
|
56
|
+
outputs?: TxOutput[];
|
|
57
|
+
locktime?: number;
|
|
58
|
+
});
|
|
59
|
+
/** Serialize the tx to a byte array */
|
|
60
|
+
ser(): Uint8Array;
|
|
61
|
+
/** Calculate the serialized size of the tx */
|
|
62
|
+
serSize(): number;
|
|
63
|
+
/** Write the tx to the given writer */
|
|
64
|
+
write(writer: Writer): void;
|
|
65
|
+
}
|
|
66
|
+
/** Write an outpoint to a Writer */
|
|
67
|
+
export declare function writeOutPoint(outpoint: OutPoint, writer: Writer): void;
|
|
68
|
+
/** Write a TxInput to a Writer */
|
|
69
|
+
export declare function writeTxInput(input: TxInput, writer: Writer): void;
|
|
70
|
+
/** Write a TxOutput to a Writer */
|
|
71
|
+
export declare function writeTxOutput(output: TxOutput, writer: Writer): void;
|
|
72
|
+
//# sourceMappingURL=tx.d.ts.map
|
package/dist/tx.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tx.d.ts","sourceRoot":"","sources":["../src/tx.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,iDAAiD;AACjD,MAAM,WAAW,QAAQ;IACrB;;;QAGI;IACJ,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC;IAC1B,kDAAkD;IAClD,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,yCAAyC;AACzC,MAAM,WAAW,OAAO;IACpB,uCAAuC;IACvC,OAAO,EAAE,QAAQ,CAAC;IAClB,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,iBAAiB;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACvB;AAED,qCAAqC;AACrC,MAAM,WAAW,QAAQ;IACrB,6DAA6D;IAC7D,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,gCAAgC;IAChC,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,6DAA6D;AAC7D,MAAM,WAAW,QAAQ;IACrB,sCAAsC;IACtC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,sDAAsD;IACtD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;QAGI;IACJ,YAAY,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,2CAA2C;AAC3C,qBAAa,EAAE;IACX,yBAAyB;IAClB,OAAO,EAAE,MAAM,CAAC;IACvB,mDAAmD;IAC5C,MAAM,EAAE,OAAO,EAAE,CAAC;IACzB,0CAA0C;IACnC,OAAO,EAAE,QAAQ,EAAE,CAAC;IAC3B,uEAAuE;IAChE,QAAQ,EAAE,MAAM,CAAC;gBAEL,MAAM,CAAC,EAAE;QACxB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;QACnB,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACrB;IAOD,uCAAuC;IAChC,GAAG,IAAI,UAAU;IAMxB,8CAA8C;IACvC,OAAO,IAAI,MAAM;IAMxB,uCAAuC;IAChC,KAAK,CAAC,MAAM,EAAE,MAAM;CAY9B;AAED,oCAAoC;AACpC,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAOtE;AAED,kCAAkC;AAClC,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAIjE;AAED,mCAAmC;AACnC,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAGpE"}
|
package/dist/tx.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { fromHexRev } from './io/hex.js';
|
|
5
|
+
import { writeVarSize } from './io/varsize.js';
|
|
6
|
+
import { WriterBytes } from './io/writerbytes.js';
|
|
7
|
+
import { WriterLength } from './io/writerlength.js';
|
|
8
|
+
/** CTransaction, a Bitcoin transaction. */
|
|
9
|
+
export class Tx {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
this.version = params?.version ?? 1;
|
|
12
|
+
this.inputs = params?.inputs ?? [];
|
|
13
|
+
this.outputs = params?.outputs ?? [];
|
|
14
|
+
this.locktime = params?.locktime ?? 0;
|
|
15
|
+
}
|
|
16
|
+
/** Serialize the tx to a byte array */
|
|
17
|
+
ser() {
|
|
18
|
+
const writerBytes = new WriterBytes(this.serSize());
|
|
19
|
+
this.write(writerBytes);
|
|
20
|
+
return writerBytes.data;
|
|
21
|
+
}
|
|
22
|
+
/** Calculate the serialized size of the tx */
|
|
23
|
+
serSize() {
|
|
24
|
+
const writerLength = new WriterLength();
|
|
25
|
+
this.write(writerLength);
|
|
26
|
+
return writerLength.length;
|
|
27
|
+
}
|
|
28
|
+
/** Write the tx to the given writer */
|
|
29
|
+
write(writer) {
|
|
30
|
+
writer.putU32(this.version);
|
|
31
|
+
writeVarSize(this.inputs.length, writer);
|
|
32
|
+
for (const input of this.inputs) {
|
|
33
|
+
writeTxInput(input, writer);
|
|
34
|
+
}
|
|
35
|
+
writeVarSize(this.outputs.length, writer);
|
|
36
|
+
for (const output of this.outputs) {
|
|
37
|
+
writeTxOutput(output, writer);
|
|
38
|
+
}
|
|
39
|
+
writer.putU32(this.locktime);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Write an outpoint to a Writer */
|
|
43
|
+
export function writeOutPoint(outpoint, writer) {
|
|
44
|
+
const txid = typeof outpoint.txid === 'string'
|
|
45
|
+
? fromHexRev(outpoint.txid)
|
|
46
|
+
: outpoint.txid;
|
|
47
|
+
writer.putBytes(txid);
|
|
48
|
+
writer.putU32(outpoint.outIdx);
|
|
49
|
+
}
|
|
50
|
+
/** Write a TxInput to a Writer */
|
|
51
|
+
export function writeTxInput(input, writer) {
|
|
52
|
+
writeOutPoint(input.prevOut, writer);
|
|
53
|
+
input.script.writeWithSize(writer);
|
|
54
|
+
writer.putU32(input.sequence);
|
|
55
|
+
}
|
|
56
|
+
/** Write a TxOutput to a Writer */
|
|
57
|
+
export function writeTxOutput(output, writer) {
|
|
58
|
+
writer.putU64(output.value);
|
|
59
|
+
output.script.writeWithSize(writer);
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=tx.js.map
|
package/dist/tx.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tx.js","sourceRoot":"","sources":["../src/tx.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AA+CpD,2CAA2C;AAC3C,MAAM,OAAO,EAAE;IAUX,YAAmB,MAKlB;QACG,IAAI,CAAC,OAAO,GAAG,MAAM,EAAE,OAAO,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,OAAO,GAAG,MAAM,EAAE,OAAO,IAAI,EAAE,CAAC;QACrC,IAAI,CAAC,QAAQ,GAAG,MAAM,EAAE,QAAQ,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED,uCAAuC;IAChC,GAAG;QACN,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QACpD,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACxB,OAAO,WAAW,CAAC,IAAI,CAAC;IAC5B,CAAC;IAED,8CAA8C;IACvC,OAAO;QACV,MAAM,YAAY,GAAG,IAAI,YAAY,EAAE,CAAC;QACxC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACzB,OAAO,YA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,aAAa,CAAC,MAAgB,EAAE,MAAc;IAC1D,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tx.test.d.ts","sourceRoot":"","sources":["../src/tx.test.ts"],"names":[],"mappings":""}
|
package/dist/tx.test.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Copyright (c) 2024 The Bitcoin developers
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { fromHex, toHex } from './io/hex.js';
|
|
6
|
+
import { Script } from './script.js';
|
|
7
|
+
import { Tx } from './tx.js';
|
|
8
|
+
const checkTx = (tx, rawHex) => {
|
|
9
|
+
expect(toHex(tx.ser())).to.equal(rawHex);
|
|
10
|
+
expect(tx.serSize()).to.equal(rawHex.length >> 1);
|
|
11
|
+
};
|
|
12
|
+
describe('Tx', () => {
|
|
13
|
+
it('checkTx', () => {
|
|
14
|
+
checkTx(new Tx(), '01000000000000000000');
|
|
15
|
+
checkTx(new Tx({ version: 0xdeadbeef, locktime: 0x12345678 }), 'efbeadde000078563412');
|
|
16
|
+
checkTx(new Tx({
|
|
17
|
+
version: 0xfacefeed,
|
|
18
|
+
inputs: [
|
|
19
|
+
{
|
|
20
|
+
prevOut: {
|
|
21
|
+
txid: '0123456789abcdef99887766554433220000000000000000f1e2d3c4b5a69788',
|
|
22
|
+
outIdx: 0xdeadbeef,
|
|
23
|
+
},
|
|
24
|
+
script: new Script(fromHex('baadcafe')),
|
|
25
|
+
sequence: 0x87654321,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
prevOut: {
|
|
29
|
+
txid: new Uint8Array([...Array(32).keys()]),
|
|
30
|
+
outIdx: 0x76757473,
|
|
31
|
+
},
|
|
32
|
+
script: new Script(fromHex('106758494753')),
|
|
33
|
+
sequence: 0x10605,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
outputs: [
|
|
37
|
+
{
|
|
38
|
+
value: 0x2134,
|
|
39
|
+
script: new Script(fromHex('1133557799')),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
value: 0x8079685746352413n,
|
|
43
|
+
script: new Script(fromHex('564738291092837465')),
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
value: 0,
|
|
47
|
+
script: new Script(fromHex('6a68656c6c6f')),
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
locktime: 0xf00dbabe,
|
|
51
|
+
}), 'edfecefa' + // version
|
|
52
|
+
'02' + // num inputs
|
|
53
|
+
'8897a6b5c4d3e2f100000000000000002233445566778899efcdab8967452301' +
|
|
54
|
+
'efbeadde' + // 0th input outpoint
|
|
55
|
+
'04baadcafe' + // script
|
|
56
|
+
'21436587' + // sequence
|
|
57
|
+
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' +
|
|
58
|
+
'73747576' + // 1st input outpoint
|
|
59
|
+
'06106758494753' + // script
|
|
60
|
+
'05060100' + // sequence
|
|
61
|
+
'03' + // num outputs
|
|
62
|
+
'3421000000000000' + // value
|
|
63
|
+
'051133557799' + // script
|
|
64
|
+
'1324354657687980' + // value
|
|
65
|
+
'09564738291092837465' + // script
|
|
66
|
+
'0000000000000000' + // value
|
|
67
|
+
'066a68656c6c6f' + // script
|
|
68
|
+
'beba0df0');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
//# sourceMappingURL=tx.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tx.test.js","sourceRoot":"","sources":["../src/tx.test.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;AAEtE,OAAO,EAAE,MAAM,EAAE,MA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,CAAC,CAAC;oBAC3C,QAAQ,EAAE,OAAO;iBACpB;aACJ;YACD,OAAO,EAAE;gBACL;oBACI,KAAK,EAAE,MAAM;oBACb,MAAM,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;iBAC5C;gBACD;oBACI,KAAK,EAAE,mBAAmB;oBAC1B,MAAM,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;iBACpD;gBACD;oBACI,KAAK,EAAE,CAAC;oBACR,MAAM,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;iBAC9C;aACJ;YACD,QAAQ,EAAE,UAAU;SACvB,CAAC,EACF,UAAU,GAAG,UAAU;YACnB,IAAI,GAAG,aAAa;YACpB,kEAAkE;YAClE,UAAU,GAAG,qBAAqB;YAClC,YAAY,GAAG,SAAS;YACxB,UAAU,GAAG,WAAW;YACxB,kEAAkE;YAClE,UAAU,GAAG,qBAAqB;YAClC,gBAAgB,GAAG,SAAS;YAC5B,UAAU,GAAG,WAAW;YACxB,IAAI,GAAG,cAAc;YACrB,kBAAkB,GAAG,QAAQ;YAC7B,cAAc,GAAG,SAAS;YAC1B,kBAAkB,GAAG,QAAQ;YAC7B,sBAAsB,GAAG,SAAS;YAClC,kBAAkB,GAAG,QAAQ;YAC7B,gBAAgB,GAAG,SAAS;YAC5B,UAAU,CACjB,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ecash-lib",
|
|
3
|
+
"version": "0.1.0rc",
|
|
4
|
+
"description": "Library for eCash transaction building",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "mocha --import=tsx ./src/*.test.ts ./src/**/*.test.ts"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/Bitcoin-ABC/bitcoin-abc.git",
|
|
14
|
+
"directory": "modules/ecash-lib"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"ecash",
|
|
18
|
+
"xec",
|
|
19
|
+
"bitcoin"
|
|
20
|
+
],
|
|
21
|
+
"author": "Bitcoin ABC",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/Bitcoin-ABC/bitcoin-abc/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Bitcoin-ABC/bitcoin-abc#readme",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/chai": "^4.3.14",
|
|
29
|
+
"@types/mocha": "^10.0.6",
|
|
30
|
+
"chai": "^5.1.0",
|
|
31
|
+
"mocha": "^10.4.0",
|
|
32
|
+
"tsx": "^4.7.2",
|
|
33
|
+
"typescript": "^5.4.3"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Language and Environment */
|
|
4
|
+
"target": "es2020",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
/* Modules */
|
|
7
|
+
"module": "NodeNext",
|
|
8
|
+
"moduleResolution": "NodeNext",
|
|
9
|
+
/* Emit */
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"declarationMap": true,
|
|
12
|
+
"sourceMap": true,
|
|
13
|
+
"outDir": "./dist",
|
|
14
|
+
/* Interop Constraints */
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
/* Type Checking */
|
|
18
|
+
"strict": true
|
|
19
|
+
}
|
|
20
|
+
}
|