@vbyte/btc-dev 1.0.1 → 1.0.2
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/class/signer.js +1 -1
- package/dist/class/tx.js +5 -4
- package/dist/class/txin.js +3 -2
- package/dist/class/witness.js +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.js +8 -6
- package/dist/lib/meta/index.d.ts +3 -1
- package/dist/lib/meta/index.js +3 -1
- package/dist/lib/{tx → meta}/locktime.d.ts +1 -1
- package/dist/lib/{tx → meta}/locktime.js +5 -5
- package/dist/lib/meta/{pointer.d.ts → ref.d.ts} +1 -1
- package/dist/lib/meta/{pointer.js → ref.js} +6 -6
- package/dist/lib/meta/scribe.d.ts +1 -1
- package/dist/lib/meta/scribe.js +5 -5
- package/dist/lib/{tx → meta}/sequence.d.ts +1 -1
- package/dist/lib/{tx → meta}/sequence.js +5 -5
- package/dist/lib/sighash/index.d.ts +0 -2
- package/dist/lib/sighash/index.js +0 -2
- package/dist/lib/sighash/segwit.js +2 -2
- package/dist/lib/sighash/taproot.js +2 -2
- package/dist/lib/signer/index.d.ts +2 -0
- package/dist/lib/signer/index.js +2 -0
- package/dist/lib/{sighash → signer}/sign.js +6 -6
- package/dist/lib/signer/verify.d.ts +4 -0
- package/dist/lib/signer/verify.js +8 -0
- package/dist/lib/taproot/parse.js +1 -1
- package/dist/lib/tx/create.d.ts +4 -4
- package/dist/lib/tx/create.js +1 -1
- package/dist/lib/tx/decode.d.ts +1 -1
- package/dist/lib/tx/decode.js +1 -1
- package/dist/lib/tx/encode.d.ts +1 -1
- package/dist/lib/tx/encode.js +3 -3
- package/dist/lib/tx/index.d.ts +1 -4
- package/dist/lib/tx/index.js +1 -4
- package/dist/lib/tx/parse.d.ts +1 -1
- package/dist/lib/tx/parse.js +5 -5
- package/dist/lib/tx/size.d.ts +1 -2
- package/dist/lib/tx/size.js +5 -12
- package/dist/lib/tx/{meta.js → util.js} +7 -7
- package/dist/lib/tx/validate.d.ts +2 -2
- package/dist/lib/witness/index.d.ts +2 -0
- package/dist/lib/witness/index.js +2 -0
- package/dist/lib/witness/util.d.ts +3 -0
- package/dist/lib/witness/util.js +8 -0
- package/dist/main.cjs +1002 -980
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +993 -973
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +9 -5
- package/dist/script.js +1 -1
- package/dist/script.js.map +1 -1
- package/dist/types/address.d.ts +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/meta.d.ts +19 -0
- package/dist/types/transaction.d.ts +1 -1
- package/dist/types/txdata.d.ts +4 -4
- package/package.json +9 -5
- package/src/class/signer.ts +1 -1
- package/src/class/tx.ts +6 -6
- package/src/class/txin.ts +2 -2
- package/src/class/witness.ts +1 -1
- package/src/index.ts +8 -6
- package/src/lib/meta/index.ts +3 -1
- package/src/lib/{tx → meta}/locktime.ts +1 -1
- package/src/lib/meta/{pointer.ts → ref.ts} +1 -1
- package/src/lib/meta/scribe.ts +1 -1
- package/src/lib/{tx → meta}/sequence.ts +1 -1
- package/src/lib/sighash/index.ts +0 -2
- package/src/lib/sighash/segwit.ts +2 -2
- package/src/lib/sighash/taproot.ts +2 -2
- package/src/lib/signer/index.ts +2 -0
- package/src/lib/{sighash → signer}/sign.ts +6 -6
- package/src/lib/{sighash → signer}/verify.ts +22 -0
- package/src/lib/taproot/parse.ts +1 -1
- package/src/lib/tx/create.ts +5 -5
- package/src/lib/tx/decode.ts +7 -7
- package/src/lib/tx/encode.ts +3 -3
- package/src/lib/tx/index.ts +1 -4
- package/src/lib/tx/parse.ts +5 -5
- package/src/lib/tx/size.ts +7 -16
- package/src/lib/tx/{meta.ts → util.ts} +7 -7
- package/src/lib/tx/validate.ts +2 -2
- package/src/lib/witness/index.ts +2 -0
- package/src/lib/witness/util.ts +12 -0
- package/src/types/address.ts +1 -1
- package/src/types/index.ts +0 -1
- package/src/types/meta.ts +26 -0
- package/src/types/transaction.ts +2 -2
- package/src/types/txdata.ts +4 -4
- package/dist/lib/sighash/verify.d.ts +0 -1
- package/dist/lib/sighash/verify.js +0 -1
- package/dist/types/txmeta.d.ts +0 -19
- package/dist/types/txmeta.js +0 -1
- package/src/types/txmeta.ts +0 -25
- /package/dist/lib/{sighash → signer}/sign.d.ts +0 -0
- /package/dist/lib/tx/{meta.d.ts → util.d.ts} +0 -0
- /package/dist/lib/{tx/witness.d.ts → witness/parse.d.ts} +0 -0
- /package/dist/lib/{tx/witness.js → witness/parse.js} +0 -0
- /package/src/lib/{tx/witness.ts → witness/parse.ts} +0 -0
package/dist/class/signer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
2
|
import { ECC } from '@vbyte/micro-lib';
|
|
3
|
-
import { sign_segwit_tx, sign_taproot_tx } from '../lib/
|
|
3
|
+
import { sign_segwit_tx, sign_taproot_tx } from '../lib/signer/sign.js';
|
|
4
4
|
export class TxSigner {
|
|
5
5
|
constructor(seckey) {
|
|
6
6
|
this._seckey = Buff.bytes(seckey).hex;
|
package/dist/class/tx.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { LocktimeUtil } from '../lib/meta/index.js';
|
|
1
2
|
import { TransactionInput } from './txin.js';
|
|
2
3
|
import { TransactionOutput } from './txout.js';
|
|
3
|
-
import {
|
|
4
|
+
import { decode_tx, get_txid, is_return_script, parse_tx, get_txsize, get_tx_value, get_txhash, encode_tx_locktime, } from '../lib/tx/index.js';
|
|
4
5
|
export class Transaction {
|
|
5
6
|
constructor(txdata) {
|
|
6
7
|
this._tx = (typeof txdata !== 'string')
|
|
7
|
-
?
|
|
8
|
-
:
|
|
8
|
+
? parse_tx(txdata)
|
|
9
|
+
: decode_tx(txdata);
|
|
9
10
|
this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
|
|
10
11
|
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
|
|
11
12
|
this._size = get_txsize(this._tx);
|
|
@@ -32,7 +33,7 @@ export class Transaction {
|
|
|
32
33
|
get locktime() {
|
|
33
34
|
return {
|
|
34
35
|
hex: encode_tx_locktime(this._tx.locktime).hex,
|
|
35
|
-
data:
|
|
36
|
+
data: LocktimeUtil.decode(this._tx.locktime),
|
|
36
37
|
value: this._tx.locktime
|
|
37
38
|
};
|
|
38
39
|
}
|
package/dist/class/txin.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { decode_script } from '../lib/script/index.js';
|
|
2
|
+
import { SequenceUtil } from '../lib/meta/index.js';
|
|
2
3
|
import { TransactionOutput } from './txout.js';
|
|
3
4
|
import { TransactionWitness } from './witness.js';
|
|
4
|
-
import { encode_txin_sequence, get_txin_size,
|
|
5
|
+
import { encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
|
|
5
6
|
export class TransactionInput {
|
|
6
7
|
constructor(txin) {
|
|
7
8
|
this._size = get_txin_size(txin);
|
|
@@ -47,7 +48,7 @@ export class TransactionInput {
|
|
|
47
48
|
get sequence() {
|
|
48
49
|
return {
|
|
49
50
|
hex: encode_txin_sequence(this._txin.sequence).hex,
|
|
50
|
-
data:
|
|
51
|
+
data: SequenceUtil.decode(this._txin.sequence),
|
|
51
52
|
value: this._txin.sequence
|
|
52
53
|
};
|
|
53
54
|
}
|
package/dist/class/witness.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
2
|
import { decode_script } from '../lib/script/index.js';
|
|
3
|
-
import { parse_witness_data, get_witness_size, } from '../lib/
|
|
3
|
+
import { parse_witness_data, get_witness_size, } from '../lib/witness/index.js';
|
|
4
4
|
export class TransactionWitness {
|
|
5
5
|
constructor(witness) {
|
|
6
6
|
this._data = witness.map(e => Buff.bytes(e));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export * from './class/index.js';
|
|
2
|
-
export * as
|
|
3
|
-
export * as
|
|
2
|
+
export * as ADDRESS from './lib/address/index.js';
|
|
3
|
+
export * as META from './lib/meta/index.js';
|
|
4
4
|
export * as PSBT from './lib/psbt/index.js';
|
|
5
|
-
export * as
|
|
6
|
-
export * as
|
|
7
|
-
export * as
|
|
8
|
-
export * as
|
|
5
|
+
export * as SCRIPT from './lib/script/index.js';
|
|
6
|
+
export * as SIGHASH from './lib/sighash/index.js';
|
|
7
|
+
export * as SIGNER from './lib/signer/index.js';
|
|
8
|
+
export * as TAPROOT from './lib/taproot/index.js';
|
|
9
|
+
export * as TX from './lib/tx/index.js';
|
|
10
|
+
export * as WITNESS from './lib/witness/index.js';
|
|
9
11
|
export * as CONST from './const.js';
|
|
10
12
|
export * as SCHEMA from './schema/index.js';
|
|
11
13
|
export type * from './types/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export * from './class/index.js';
|
|
2
|
-
export * as
|
|
3
|
-
export * as
|
|
2
|
+
export * as ADDRESS from './lib/address/index.js';
|
|
3
|
+
export * as META from './lib/meta/index.js';
|
|
4
4
|
export * as PSBT from './lib/psbt/index.js';
|
|
5
|
-
export * as
|
|
6
|
-
export * as
|
|
7
|
-
export * as
|
|
8
|
-
export * as
|
|
5
|
+
export * as SCRIPT from './lib/script/index.js';
|
|
6
|
+
export * as SIGHASH from './lib/sighash/index.js';
|
|
7
|
+
export * as SIGNER from './lib/signer/index.js';
|
|
8
|
+
export * as TAPROOT from './lib/taproot/index.js';
|
|
9
|
+
export * as TX from './lib/tx/index.js';
|
|
10
|
+
export * as WITNESS from './lib/witness/index.js';
|
|
9
11
|
export * as CONST from './const.js';
|
|
10
12
|
export * as SCHEMA from './schema/index.js';
|
package/dist/lib/meta/index.d.ts
CHANGED
package/dist/lib/meta/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Assert } from '@vbyte/micro-lib';
|
|
2
2
|
const LOCKTIME_THRESHOLD = 500000000;
|
|
3
|
-
export var
|
|
4
|
-
(function (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})(
|
|
3
|
+
export var LocktimeUtil;
|
|
4
|
+
(function (LocktimeUtil) {
|
|
5
|
+
LocktimeUtil.encode = encode_locktime;
|
|
6
|
+
LocktimeUtil.decode = decode_locktime;
|
|
7
|
+
})(LocktimeUtil || (LocktimeUtil = {}));
|
|
8
8
|
export function encode_locktime(locktime) {
|
|
9
9
|
switch (locktime.type) {
|
|
10
10
|
case 'timelock':
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
3
|
-
|
|
1
|
+
export var RefEncoder;
|
|
2
|
+
(function (RefEncoder) {
|
|
3
|
+
RefEncoder.outpoint = {
|
|
4
4
|
encode: encode_outpoint,
|
|
5
5
|
decode: decode_outpoint,
|
|
6
6
|
verify: verify_outpoint,
|
|
7
7
|
assert: assert_outpoint,
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
RefEncoder.record_id = {
|
|
10
10
|
encode: encode_inscription_id,
|
|
11
11
|
decode: decode_inscription_id,
|
|
12
12
|
verify: verify_inscription_id,
|
|
13
13
|
assert: assert_inscription_id,
|
|
14
14
|
};
|
|
15
|
-
|
|
15
|
+
RefEncoder.rune_id = {
|
|
16
16
|
encode: encode_rune_id,
|
|
17
17
|
decode: decode_rune_id,
|
|
18
18
|
verify: verify_rune_id,
|
|
19
19
|
assert: assert_rune_id,
|
|
20
20
|
};
|
|
21
|
-
})(
|
|
21
|
+
})(RefEncoder || (RefEncoder = {}));
|
|
22
22
|
function encode_inscription_id(txid, order = 0) {
|
|
23
23
|
return `${txid}i${order}`;
|
|
24
24
|
}
|
package/dist/lib/meta/scribe.js
CHANGED
|
@@ -5,11 +5,11 @@ import { decode_script } from '../../lib/script/decode.js';
|
|
|
5
5
|
const _0n = BigInt(0);
|
|
6
6
|
const _1n = BigInt(1);
|
|
7
7
|
const _26n = BigInt(26);
|
|
8
|
-
export var
|
|
9
|
-
(function (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
})(
|
|
8
|
+
export var ScribeEncoder;
|
|
9
|
+
(function (ScribeEncoder) {
|
|
10
|
+
ScribeEncoder.encode = encode_inscription;
|
|
11
|
+
ScribeEncoder.decode = decode_inscription;
|
|
12
|
+
})(ScribeEncoder || (ScribeEncoder = {}));
|
|
13
13
|
export function decode_inscription(script) {
|
|
14
14
|
const envelopes = parse_envelopes(script);
|
|
15
15
|
return envelopes.map(parse_record);
|
|
@@ -3,11 +3,11 @@ const TIMELOCK_TYPE = 0x00400000;
|
|
|
3
3
|
const TIMELOCK_VALUE_MASK = 0x0000FFFF;
|
|
4
4
|
const TIMELOCK_VALUE_MAX = 0xFFFF;
|
|
5
5
|
const TIMELOCK_GRANULARITY = 512;
|
|
6
|
-
export var
|
|
7
|
-
(function (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
})(
|
|
6
|
+
export var SequenceUtil;
|
|
7
|
+
(function (SequenceUtil) {
|
|
8
|
+
SequenceUtil.encode = encode_sequence;
|
|
9
|
+
SequenceUtil.decode = decode_sequence;
|
|
10
|
+
})(SequenceUtil || (SequenceUtil = {}));
|
|
11
11
|
export function encode_sequence(data) {
|
|
12
12
|
if (data.mode === 'height') {
|
|
13
13
|
const height = parse_height(data.height);
|
|
@@ -3,11 +3,11 @@ import { hash160, hash256 } from '@vbyte/micro-lib/hash';
|
|
|
3
3
|
import { Assert } from '@vbyte/micro-lib';
|
|
4
4
|
import { parse_txinput } from './util.js';
|
|
5
5
|
import { prefix_script_size, decode_script } from '../../lib/script/index.js';
|
|
6
|
-
import { encode_txin_vout, encode_tx_locktime, encode_txin_sequence, encode_txin_txid, encode_vout_value, encode_tx_version,
|
|
6
|
+
import { encode_txin_vout, encode_tx_locktime, encode_txin_sequence, encode_txin_txid, encode_vout_value, encode_tx_version, parse_tx } from '../../lib/tx/index.js';
|
|
7
7
|
import * as CONST from '../../const.js';
|
|
8
8
|
export function hash_segwit_tx(txdata, options = {}) {
|
|
9
9
|
const { sigflag = 0x01, txindex } = options;
|
|
10
|
-
const tx =
|
|
10
|
+
const tx = parse_tx(txdata);
|
|
11
11
|
const is_anypay = (sigflag & 0x80) === 0x80;
|
|
12
12
|
const flag = sigflag % 0x80;
|
|
13
13
|
if (!CONST.SIGHASH_SEGWIT.includes(flag)) {
|
|
@@ -3,13 +3,13 @@ import { Assert } from '@vbyte/micro-lib';
|
|
|
3
3
|
import { hash340, sha256 } from '@vbyte/micro-lib/hash';
|
|
4
4
|
import { prefix_script_size } from '../../lib/script/util.js';
|
|
5
5
|
import { encode_tapscript } from '../../lib/taproot/encode.js';
|
|
6
|
-
import {
|
|
6
|
+
import { parse_tx } from '../../lib/tx/parse.js';
|
|
7
7
|
import * as CONST from '../../const.js';
|
|
8
8
|
import { parse_txinput } from './util.js';
|
|
9
9
|
import { encode_txin_vout, encode_tx_locktime, encode_txin_sequence, encode_txin_txid, encode_vout_value, encode_tx_version } from '../../lib/tx/encode.js';
|
|
10
10
|
export function hash_taproot_tx(template, config = {}) {
|
|
11
11
|
const { script, txindex, sigflag = 0x00, extflag = 0x00, key_version = 0x00, separator_pos = 0xFFFFFFFF } = config;
|
|
12
|
-
const tx =
|
|
12
|
+
const tx = parse_tx(template);
|
|
13
13
|
const { version, vin: input, vout: output, locktime } = tx;
|
|
14
14
|
const txinput = parse_txinput(tx, config);
|
|
15
15
|
const { txid, vout, sequence, witness = [] } = txinput;
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { ECC } from '@vbyte/micro-lib';
|
|
2
|
-
import {
|
|
2
|
+
import { parse_tx } from '../../lib/tx/parse.js';
|
|
3
3
|
import { SIGHASH_DEFAULT } from '../../const.js';
|
|
4
|
-
import { hash_segwit_tx } from '
|
|
5
|
-
import { hash_taproot_tx } from '
|
|
6
|
-
import { format_sigflag } from '
|
|
4
|
+
import { hash_segwit_tx } from '../sighash/segwit.js';
|
|
5
|
+
import { hash_taproot_tx } from '../sighash/taproot.js';
|
|
6
|
+
import { format_sigflag } from '../sighash/util.js';
|
|
7
7
|
export function sign_segwit_tx(seckey, txdata, options) {
|
|
8
|
-
const tx =
|
|
8
|
+
const tx = parse_tx(txdata);
|
|
9
9
|
const msg = hash_segwit_tx(tx, options);
|
|
10
10
|
const sig = ECC.sign_ecdsa(seckey, msg).hex;
|
|
11
11
|
const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT);
|
|
12
12
|
return sig + flag;
|
|
13
13
|
}
|
|
14
14
|
export function sign_taproot_tx(seckey, txdata, options) {
|
|
15
|
-
const tx =
|
|
15
|
+
const tx = parse_tx(txdata);
|
|
16
16
|
const msg = hash_taproot_tx(tx, options);
|
|
17
17
|
const sig = ECC.sign_bip340(seckey, msg).hex;
|
|
18
18
|
const flag = format_sigflag(options.sigflag ?? 0);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Bytes } from '@vbyte/buff';
|
|
2
|
+
import type { SigHashOptions, TxData } from '../../types/index.js';
|
|
3
|
+
export declare function verify_segwit_tx(_txdata: TxData | Bytes, _config?: SigHashOptions): boolean;
|
|
4
|
+
export declare function verify_taproot_tx(_txdata: TxData | Bytes, _config?: SigHashOptions): boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function verify_segwit_tx(_txdata, _config = {}) {
|
|
2
|
+
console.warn('verify_segwit_tx is not implemented');
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
export function verify_taproot_tx(_txdata, _config = {}) {
|
|
6
|
+
console.warn('verify_taproot_tx is not implemented');
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buff, Stream } from '@vbyte/buff';
|
|
2
2
|
import { Assert, ECC } from '@vbyte/micro-lib';
|
|
3
|
-
import { parse_witness_data } from '../../lib/
|
|
3
|
+
import { parse_witness_data } from '../../lib/witness/parse.js';
|
|
4
4
|
import { encode_tapbranch, encode_tapscript, encode_taptweak, } from './encode.js';
|
|
5
5
|
export function parse_taproot_witness(witness) {
|
|
6
6
|
const { cblock, params, script } = parse_witness_data(witness);
|
package/dist/lib/tx/create.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { TxData, TxInputTemplate, TxInput, TxOutput, TxTemplate,
|
|
2
|
-
export declare function create_coinbase_input(config: TxInputTemplate | TxInput):
|
|
3
|
-
export declare function create_spend_input(config: TxInputTemplate | TxInput):
|
|
1
|
+
import type { TxData, TxInputTemplate, TxInput, TxOutput, TxTemplate, TxSpendInput, TxCoinbaseInput } from '../../types/index.js';
|
|
2
|
+
export declare function create_coinbase_input(config: TxInputTemplate | TxInput): TxCoinbaseInput;
|
|
3
|
+
export declare function create_spend_input(config: TxInputTemplate | TxInput): TxSpendInput;
|
|
4
4
|
export declare function create_tx_input(config: TxInputTemplate | TxInput): TxInput;
|
|
5
5
|
export declare function create_tx_output(config: TxOutput): TxOutput;
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function create_tx(config: TxTemplate | TxData): TxData;
|
package/dist/lib/tx/create.js
CHANGED
|
@@ -43,7 +43,7 @@ export function create_tx_output(config) {
|
|
|
43
43
|
const { script_pk, value } = config;
|
|
44
44
|
return { script_pk, value: BigInt(value) };
|
|
45
45
|
}
|
|
46
|
-
export function
|
|
46
|
+
export function create_tx(config) {
|
|
47
47
|
assert_tx_template(config);
|
|
48
48
|
const { vin = [], vout = [] } = config;
|
|
49
49
|
const locktime = config.locktime ?? DEFAULT.LOCKTIME;
|
package/dist/lib/tx/decode.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ interface TxEncoderConfig {
|
|
|
4
4
|
prevouts: TxOutput[];
|
|
5
5
|
segwit: boolean;
|
|
6
6
|
}
|
|
7
|
-
export declare function
|
|
7
|
+
export declare function decode_tx(txbytes: Bytes, options?: Partial<TxEncoderConfig>): TxData;
|
|
8
8
|
export declare function read_script(stream: Stream, varint?: boolean): string | null;
|
|
9
9
|
export {};
|
package/dist/lib/tx/decode.js
CHANGED
|
@@ -6,7 +6,7 @@ const DEFAULT_CONFIG = {
|
|
|
6
6
|
prevouts: [],
|
|
7
7
|
segwit: true
|
|
8
8
|
};
|
|
9
|
-
export function
|
|
9
|
+
export function decode_tx(txbytes, options = {}) {
|
|
10
10
|
const config = { ...DEFAULT_CONFIG, ...options };
|
|
11
11
|
Assert.is_bytes(txbytes, 'txbytes must be hex or a unit array');
|
|
12
12
|
const stream = new Stream(txbytes);
|
package/dist/lib/tx/encode.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
2
|
import { TxInput, TxOutput, TxData } from '../../types/index.js';
|
|
3
|
-
export declare function
|
|
3
|
+
export declare function encode_tx(txdata: TxData, segwit?: boolean): Buff;
|
|
4
4
|
export declare function encode_tx_version(num: number): Buff;
|
|
5
5
|
export declare function encode_txin_txid(txid: string): Buff;
|
|
6
6
|
export declare function encode_txin_vout(vout: number): Buff;
|
package/dist/lib/tx/encode.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
2
|
import { Assert } from '@vbyte/micro-lib';
|
|
3
|
-
import {
|
|
3
|
+
import { parse_tx } from './parse.js';
|
|
4
4
|
import { COINBASE } from '../../const.js';
|
|
5
|
-
export function
|
|
6
|
-
const tx =
|
|
5
|
+
export function encode_tx(txdata, segwit = true) {
|
|
6
|
+
const tx = parse_tx(txdata);
|
|
7
7
|
const { version, vin, vout, locktime } = tx;
|
|
8
8
|
const buffer = [encode_tx_version(version)];
|
|
9
9
|
if (segwit) {
|
package/dist/lib/tx/index.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
export * from './create.js';
|
|
2
2
|
export * from './decode.js';
|
|
3
3
|
export * from './encode.js';
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './meta.js';
|
|
4
|
+
export * from './util.js';
|
|
6
5
|
export * from './parse.js';
|
|
7
|
-
export * from './sequence.js';
|
|
8
6
|
export * from './size.js';
|
|
9
7
|
export * from './validate.js';
|
|
10
|
-
export * from './witness.js';
|
package/dist/lib/tx/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
export * from './create.js';
|
|
2
2
|
export * from './decode.js';
|
|
3
3
|
export * from './encode.js';
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './meta.js';
|
|
4
|
+
export * from './util.js';
|
|
6
5
|
export * from './parse.js';
|
|
7
|
-
export * from './sequence.js';
|
|
8
6
|
export * from './size.js';
|
|
9
7
|
export * from './validate.js';
|
|
10
|
-
export * from './witness.js';
|
package/dist/lib/tx/parse.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { TxData } from '../../types/index.js';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function parse_tx(txdata: unknown): TxData;
|
package/dist/lib/tx/parse.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { decode_tx } from './decode.js';
|
|
2
|
+
import { create_tx } from './create.js';
|
|
3
3
|
import { assert_tx_template } from './validate.js';
|
|
4
|
-
export function
|
|
4
|
+
export function parse_tx(txdata) {
|
|
5
5
|
if (typeof txdata === 'string') {
|
|
6
|
-
return
|
|
6
|
+
return decode_tx(txdata);
|
|
7
7
|
}
|
|
8
8
|
else {
|
|
9
9
|
assert_tx_template(txdata);
|
|
10
|
-
return
|
|
10
|
+
return create_tx(txdata);
|
|
11
11
|
}
|
|
12
12
|
}
|
package/dist/lib/tx/size.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Bytes } from '@vbyte/buff';
|
|
2
|
-
import type { TxData, TxInput, TxOutput, TxSize
|
|
2
|
+
import type { TxData, TxInput, TxOutput, TxSize } from '../../types/index.js';
|
|
3
3
|
export declare function get_vsize(bytes: Bytes): number;
|
|
4
4
|
export declare function get_txsize(txdata: string | TxData): TxSize;
|
|
5
5
|
export declare function get_vin_size(vin: TxInput[]): number;
|
|
@@ -7,4 +7,3 @@ export declare function get_vout_size(vout: TxOutput[]): number;
|
|
|
7
7
|
export declare function get_segwit_size(txinputs: TxInput[]): number;
|
|
8
8
|
export declare function get_txin_size(txinput: TxInput): number;
|
|
9
9
|
export declare function get_txout_size(txoutput: TxOutput): number;
|
|
10
|
-
export declare function get_witness_size(witness: Bytes[]): WitnessSize;
|
package/dist/lib/tx/size.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { parse_tx } from './parse.js';
|
|
3
|
+
import { encode_tx, encode_tx_inputs, encode_tx_outputs, encode_tx_vout, encode_vin, encode_vin_witness } from './encode.js';
|
|
4
4
|
const WIT_FLAG_BYTES = 2;
|
|
5
|
-
const WIT_LENGTH_BYTE = 1;
|
|
6
5
|
export function get_vsize(bytes) {
|
|
7
6
|
const weight = Buff.bytes(bytes).length;
|
|
8
7
|
const remain = (weight % 4 > 0) ? 1 : 0;
|
|
9
8
|
return Math.floor(weight / 4) + remain;
|
|
10
9
|
}
|
|
11
10
|
export function get_txsize(txdata) {
|
|
12
|
-
const json =
|
|
13
|
-
const base =
|
|
14
|
-
const size =
|
|
11
|
+
const json = parse_tx(txdata);
|
|
12
|
+
const base = encode_tx(json, false).length;
|
|
13
|
+
const size = encode_tx(json, true).length;
|
|
15
14
|
const weight = base * 3 + size;
|
|
16
15
|
const remain = (weight % 4 > 0) ? 1 : 0;
|
|
17
16
|
const vsize = Math.floor(weight / 4) + remain;
|
|
@@ -40,9 +39,3 @@ export function get_txout_size(txoutput) {
|
|
|
40
39
|
const bytes = encode_tx_vout(txoutput);
|
|
41
40
|
return bytes.length;
|
|
42
41
|
}
|
|
43
|
-
export function get_witness_size(witness) {
|
|
44
|
-
const stack = witness.map(e => Buff.bytes(e));
|
|
45
|
-
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
46
|
-
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
47
|
-
return { size, vsize };
|
|
48
|
-
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { hash256 } from '@vbyte/micro-lib/hash';
|
|
2
2
|
import { LOCK_SCRIPT_REGEX } from '../../const.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { encode_tx } from './encode.js';
|
|
4
|
+
import { parse_tx } from './parse.js';
|
|
5
5
|
export function is_return_script(script) {
|
|
6
6
|
return script.startsWith('6a');
|
|
7
7
|
}
|
|
@@ -30,17 +30,17 @@ export function get_vout_version(script) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
export function get_txid(txdata) {
|
|
33
|
-
const json =
|
|
34
|
-
const data =
|
|
33
|
+
const json = parse_tx(txdata);
|
|
34
|
+
const data = encode_tx(json, false);
|
|
35
35
|
return hash256(data).reverse().hex;
|
|
36
36
|
}
|
|
37
37
|
export function get_txhash(txdata) {
|
|
38
|
-
const json =
|
|
39
|
-
const data =
|
|
38
|
+
const json = parse_tx(txdata);
|
|
39
|
+
const data = encode_tx(json, true);
|
|
40
40
|
return hash256(data).reverse().hex;
|
|
41
41
|
}
|
|
42
42
|
export function get_tx_value(txdata) {
|
|
43
|
-
const tx =
|
|
43
|
+
const tx = parse_tx(txdata);
|
|
44
44
|
const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n);
|
|
45
45
|
const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n);
|
|
46
46
|
const fees = (vin > vout) ? (vin - vout) : 0n;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TxSpendInput, TxData, TxInput, TxInputTemplate, TxOutput, TxTemplate } from '../../types/index.js';
|
|
2
2
|
export declare function assert_tx_template(txdata: unknown): asserts txdata is TxTemplate;
|
|
3
|
-
export declare function assert_has_prevouts(vin: TxInput[]): asserts vin is
|
|
3
|
+
export declare function assert_has_prevouts(vin: TxInput[]): asserts vin is TxSpendInput[];
|
|
4
4
|
export declare function assert_tx_data(txdata: unknown): asserts txdata is TxData;
|
|
5
5
|
export declare function assert_tx_input(tx_input: unknown): asserts tx_input is TxInput;
|
|
6
6
|
export declare function assert_tx_output(tx_output: unknown): asserts tx_output is TxOutput;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Buff } from '@vbyte/buff';
|
|
2
|
+
const WIT_LENGTH_BYTE = 1;
|
|
3
|
+
export function get_witness_size(witness) {
|
|
4
|
+
const stack = witness.map(e => Buff.bytes(e));
|
|
5
|
+
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
6
|
+
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
7
|
+
return { size, vsize };
|
|
8
|
+
}
|