@vbyte/btc-dev 1.0.0 → 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 -6
- 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 -6
- 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/types/address.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type AddressFormat = 'base58' | 'bech32' | 'bech32m';
|
|
2
2
|
export type AddressType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr';
|
|
3
|
-
export type ChainNetwork = 'main' | 'testnet' | 'regtest';
|
|
3
|
+
export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string;
|
|
4
4
|
export type AddressData = AddressContext & ScriptData;
|
|
5
5
|
export type AddressConfigEntry = [
|
|
6
6
|
prefix: string,
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
package/dist/types/meta.d.ts
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
export type LocktimeInfo = LocktimeStamp | LocktimeHeight;
|
|
2
|
+
export type SequenceConfig = Partial<SequenceInfo>;
|
|
3
|
+
export type SequenceInfo = SequenceHeightLock | SequenceStampLock;
|
|
4
|
+
export interface LocktimeStamp {
|
|
5
|
+
type: 'timelock';
|
|
6
|
+
stamp: number;
|
|
7
|
+
}
|
|
8
|
+
export interface LocktimeHeight {
|
|
9
|
+
type: 'heightlock';
|
|
10
|
+
height: number;
|
|
11
|
+
}
|
|
12
|
+
export interface SequenceStampLock {
|
|
13
|
+
stamp: number;
|
|
14
|
+
mode: 'stamp';
|
|
15
|
+
}
|
|
16
|
+
export interface SequenceHeightLock {
|
|
17
|
+
height: number;
|
|
18
|
+
mode: 'height';
|
|
19
|
+
}
|
|
1
20
|
export interface InscriptionData {
|
|
2
21
|
content?: string;
|
|
3
22
|
delegate?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { LocktimeInfo, SequenceInfo } from './meta.js';
|
|
1
2
|
import { TxOutput } from './txdata.js';
|
|
2
|
-
import { LocktimeInfo, SequenceInfo } from './txmeta.js';
|
|
3
3
|
import type { WitnessType, WitnessVersion } from './witness.js';
|
|
4
4
|
export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn';
|
|
5
5
|
export interface TxOutputInfo {
|
package/dist/types/txdata.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type TxInput =
|
|
1
|
+
export type TxInput = TxCoinbaseInput | TxSpendInput | TxVirtualInput;
|
|
2
2
|
export interface TxOutpoint {
|
|
3
3
|
txid: string;
|
|
4
4
|
vout: number;
|
|
@@ -10,21 +10,21 @@ export interface TxInputTemplate extends TxOutpoint {
|
|
|
10
10
|
sequence?: number;
|
|
11
11
|
witness?: string[];
|
|
12
12
|
}
|
|
13
|
-
export interface
|
|
13
|
+
export interface TxCoinbaseInput extends TxOutpoint {
|
|
14
14
|
coinbase: string;
|
|
15
15
|
prevout: null;
|
|
16
16
|
script_sig: null;
|
|
17
17
|
sequence: number;
|
|
18
18
|
witness: string[];
|
|
19
19
|
}
|
|
20
|
-
export interface
|
|
20
|
+
export interface TxVirtualInput extends TxOutpoint {
|
|
21
21
|
coinbase: null;
|
|
22
22
|
prevout: TxOutput | null;
|
|
23
23
|
script_sig: string | null;
|
|
24
24
|
sequence: number;
|
|
25
25
|
witness: string[];
|
|
26
26
|
}
|
|
27
|
-
export interface
|
|
27
|
+
export interface TxSpendInput extends TxVirtualInput {
|
|
28
28
|
prevout: TxOutput;
|
|
29
29
|
}
|
|
30
30
|
export interface TxOutput {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vbyte/btc-dev",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Batteries-included toolset for plebian bitcoin development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -35,17 +35,12 @@
|
|
|
35
35
|
".": {
|
|
36
36
|
"types": "./dist/types/index.d.ts",
|
|
37
37
|
"import": "./dist/index.js",
|
|
38
|
-
"require": "./dist/main.cjs",
|
|
39
38
|
"default": "./dist/index.js"
|
|
40
39
|
},
|
|
41
40
|
"./address": {
|
|
42
41
|
"types": "./dist/lib/address/index.d.ts",
|
|
43
42
|
"import": "./dist/lib/address/index.js"
|
|
44
43
|
},
|
|
45
|
-
"./class": {
|
|
46
|
-
"types": "./dist/class/index.d.ts",
|
|
47
|
-
"import": "./dist/class/index.js"
|
|
48
|
-
},
|
|
49
44
|
"./meta": {
|
|
50
45
|
"types": "./dist/lib/meta.d.ts",
|
|
51
46
|
"import": "./dist/lib/meta.js"
|
|
@@ -62,6 +57,10 @@
|
|
|
62
57
|
"types": "./dist/lib/sighash/index.d.ts",
|
|
63
58
|
"import": "./dist/lib/sighash/index.js"
|
|
64
59
|
},
|
|
60
|
+
"./signer": {
|
|
61
|
+
"types": "./dist/lib/signer/index.d.ts",
|
|
62
|
+
"import": "./dist/lib/signer/index.js"
|
|
63
|
+
},
|
|
65
64
|
"./taproot": {
|
|
66
65
|
"types": "./dist/lib/taproot/index.d.ts",
|
|
67
66
|
"import": "./dist/lib/taproot/index.js"
|
|
@@ -69,6 +68,10 @@
|
|
|
69
68
|
"./tx": {
|
|
70
69
|
"types": "./dist/lib/tx/index.d.ts",
|
|
71
70
|
"import": "./dist/lib/tx/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./witness": {
|
|
73
|
+
"types": "./dist/lib/witness/index.d.ts",
|
|
74
|
+
"import": "./dist/lib/witness/index.js"
|
|
72
75
|
}
|
|
73
76
|
},
|
|
74
77
|
"scripts": {
|
package/src/class/signer.ts
CHANGED
package/src/class/tx.ts
CHANGED
|
@@ -1,12 +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
4
|
|
|
4
5
|
import {
|
|
5
|
-
|
|
6
|
+
decode_tx,
|
|
6
7
|
get_txid,
|
|
7
8
|
is_return_script,
|
|
8
|
-
|
|
9
|
-
Locktime,
|
|
9
|
+
parse_tx,
|
|
10
10
|
get_txsize,
|
|
11
11
|
get_tx_value,
|
|
12
12
|
get_txhash,
|
|
@@ -32,8 +32,8 @@ export class Transaction {
|
|
|
32
32
|
|
|
33
33
|
constructor (txdata : string | TxData | TxTemplate) {
|
|
34
34
|
this._tx = (typeof txdata !== 'string')
|
|
35
|
-
?
|
|
36
|
-
:
|
|
35
|
+
? parse_tx(txdata)
|
|
36
|
+
: decode_tx(txdata)
|
|
37
37
|
|
|
38
38
|
this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
|
|
39
39
|
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
|
|
@@ -65,7 +65,7 @@ export class Transaction {
|
|
|
65
65
|
get locktime () {
|
|
66
66
|
return {
|
|
67
67
|
hex : encode_tx_locktime(this._tx.locktime).hex,
|
|
68
|
-
data :
|
|
68
|
+
data : LocktimeUtil.decode(this._tx.locktime),
|
|
69
69
|
value : this._tx.locktime
|
|
70
70
|
}
|
|
71
71
|
}
|
package/src/class/txin.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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
5
|
|
|
5
6
|
import {
|
|
6
7
|
encode_txin_sequence,
|
|
7
8
|
get_txin_size,
|
|
8
|
-
Sequence
|
|
9
9
|
} from '@/lib/tx/index.js'
|
|
10
10
|
|
|
11
11
|
import type {
|
|
@@ -69,7 +69,7 @@ export class TransactionInput {
|
|
|
69
69
|
get sequence () {
|
|
70
70
|
return {
|
|
71
71
|
hex : encode_txin_sequence(this._txin.sequence).hex,
|
|
72
|
-
data :
|
|
72
|
+
data : SequenceUtil.decode(this._txin.sequence),
|
|
73
73
|
value : this._txin.sequence
|
|
74
74
|
}
|
|
75
75
|
}
|
package/src/class/witness.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export * from './class/index.js'
|
|
2
2
|
|
|
3
|
-
export * as
|
|
4
|
-
export * as
|
|
3
|
+
export * as ADDRESS from './lib/address/index.js'
|
|
4
|
+
export * as META from './lib/meta/index.js'
|
|
5
5
|
export * as PSBT from './lib/psbt/index.js'
|
|
6
|
-
export * as
|
|
7
|
-
export * as
|
|
8
|
-
export * as
|
|
9
|
-
export * as
|
|
6
|
+
export * as SCRIPT from './lib/script/index.js'
|
|
7
|
+
export * as SIGHASH from './lib/sighash/index.js'
|
|
8
|
+
export * as SIGNER from './lib/signer/index.js'
|
|
9
|
+
export * as TAPROOT from './lib/taproot/index.js'
|
|
10
|
+
export * as TX from './lib/tx/index.js'
|
|
11
|
+
export * as WITNESS from './lib/witness/index.js'
|
|
10
12
|
|
|
11
13
|
export * as CONST from './const.js'
|
|
12
14
|
export * as SCHEMA from './schema/index.js'
|
package/src/lib/meta/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { LocktimeInfo } from '@/types/index.js'
|
|
|
5
5
|
// The threshold between block height and timestamp.
|
|
6
6
|
const LOCKTIME_THRESHOLD = 500000000
|
|
7
7
|
|
|
8
|
-
export namespace
|
|
8
|
+
export namespace LocktimeUtil {
|
|
9
9
|
export const encode = encode_locktime
|
|
10
10
|
export const decode = decode_locktime
|
|
11
11
|
}
|
package/src/lib/meta/scribe.ts
CHANGED
|
@@ -23,7 +23,7 @@ const TIMELOCK_GRANULARITY = 512 // Seconds per timestamp unit (BIP-68 s
|
|
|
23
23
|
|
|
24
24
|
/* ===== [ API ] ============================================================ */
|
|
25
25
|
|
|
26
|
-
export namespace
|
|
26
|
+
export namespace SequenceUtil {
|
|
27
27
|
export const encode = encode_sequence
|
|
28
28
|
export const decode = decode_sequence
|
|
29
29
|
}
|
package/src/lib/sighash/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
encode_txin_txid,
|
|
16
16
|
encode_vout_value,
|
|
17
17
|
encode_tx_version,
|
|
18
|
-
|
|
18
|
+
parse_tx
|
|
19
19
|
} from '@/lib/tx/index.js'
|
|
20
20
|
|
|
21
21
|
import {
|
|
@@ -34,7 +34,7 @@ export function hash_segwit_tx (
|
|
|
34
34
|
// Unpack the sigflag from our config object.
|
|
35
35
|
const { sigflag = 0x01, txindex } = options
|
|
36
36
|
// Normalize the tx into JSON format.
|
|
37
|
-
const tx =
|
|
37
|
+
const tx = parse_tx(txdata)
|
|
38
38
|
// Check if the ANYONECANPAY flag is set.
|
|
39
39
|
const is_anypay = (sigflag & 0x80) === 0x80
|
|
40
40
|
// Save a normalized version of the sigflag.
|
|
@@ -3,7 +3,7 @@ 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
|
|
|
@@ -37,7 +37,7 @@ export function hash_taproot_tx (
|
|
|
37
37
|
separator_pos = 0xFFFFFFFF
|
|
38
38
|
} = config
|
|
39
39
|
// Normalize the txdata object.
|
|
40
|
-
const tx =
|
|
40
|
+
const tx = parse_tx(template)
|
|
41
41
|
// Unpack the txdata object.
|
|
42
42
|
const { version, vin: input, vout: output, locktime } = tx
|
|
43
43
|
// Parse the input we are signing from the config.
|
|
@@ -1,9 +1,9 @@
|
|
|
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
|
|
|
8
8
|
import type {
|
|
9
9
|
SigHashOptions,
|
|
@@ -15,7 +15,7 @@ export function sign_segwit_tx (
|
|
|
15
15
|
txdata : TxData,
|
|
16
16
|
options : SigHashOptions,
|
|
17
17
|
) {
|
|
18
|
-
const tx =
|
|
18
|
+
const tx = parse_tx(txdata)
|
|
19
19
|
const msg = hash_segwit_tx(tx, options)
|
|
20
20
|
const sig = ECC.sign_ecdsa(seckey, msg).hex
|
|
21
21
|
const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT)
|
|
@@ -27,7 +27,7 @@ export function sign_taproot_tx (
|
|
|
27
27
|
txdata : TxData,
|
|
28
28
|
options : SigHashOptions,
|
|
29
29
|
) {
|
|
30
|
-
const tx =
|
|
30
|
+
const tx = parse_tx(txdata)
|
|
31
31
|
const msg = hash_taproot_tx(tx, options)
|
|
32
32
|
const sig = ECC.sign_bip340(seckey, msg).hex
|
|
33
33
|
const flag = format_sigflag(options.sigflag ?? 0)
|
|
@@ -7,6 +7,28 @@
|
|
|
7
7
|
// import { HashConfig, TxData } from '@/types/index.js'
|
|
8
8
|
// import { TxTemplate } from '@/schema/types.js'
|
|
9
9
|
// import { hashTx } from './segwit.js'
|
|
10
|
+
import { Bytes } from '@vbyte/buff'
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
SigHashOptions,
|
|
14
|
+
TxData
|
|
15
|
+
} from '@/types/index.js'
|
|
16
|
+
|
|
17
|
+
export function verify_segwit_tx (
|
|
18
|
+
_txdata : TxData | Bytes,
|
|
19
|
+
_config : SigHashOptions = {}
|
|
20
|
+
) : boolean {
|
|
21
|
+
console.warn('verify_segwit_tx is not implemented')
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function verify_taproot_tx (
|
|
26
|
+
_txdata : TxData | Bytes,
|
|
27
|
+
_config : SigHashOptions = {}
|
|
28
|
+
) : boolean {
|
|
29
|
+
console.warn('verify_taproot_tx is not implemented')
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
10
32
|
|
|
11
33
|
// export function verify_signature (
|
|
12
34
|
// txdata : TxData | Bytes,
|
package/src/lib/taproot/parse.ts
CHANGED
package/src/lib/tx/create.ts
CHANGED
|
@@ -13,13 +13,13 @@ import type {
|
|
|
13
13
|
TxInput,
|
|
14
14
|
TxOutput,
|
|
15
15
|
TxTemplate,
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
TxSpendInput,
|
|
17
|
+
TxCoinbaseInput
|
|
18
18
|
} from '@/types/index.js'
|
|
19
19
|
|
|
20
20
|
export function create_coinbase_input (
|
|
21
21
|
config : TxInputTemplate | TxInput
|
|
22
|
-
) :
|
|
22
|
+
) : TxCoinbaseInput {
|
|
23
23
|
assert_vin_template(config)
|
|
24
24
|
Assert.exists(config.coinbase, 'coinbase is required')
|
|
25
25
|
const coinbase = config.coinbase
|
|
@@ -34,7 +34,7 @@ export function create_coinbase_input (
|
|
|
34
34
|
|
|
35
35
|
export function create_spend_input (
|
|
36
36
|
config : TxInputTemplate | TxInput
|
|
37
|
-
) :
|
|
37
|
+
) : TxSpendInput {
|
|
38
38
|
assert_vin_template(config)
|
|
39
39
|
Assert.exists(config.prevout, 'prevout is required')
|
|
40
40
|
const prevout = config.prevout
|
|
@@ -69,7 +69,7 @@ export function create_tx_output (
|
|
|
69
69
|
return { script_pk, value : BigInt(value) }
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
export function
|
|
72
|
+
export function create_tx (
|
|
73
73
|
config: TxTemplate | TxData
|
|
74
74
|
) : TxData {
|
|
75
75
|
assert_tx_template(config)
|
package/src/lib/tx/decode.ts
CHANGED
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
TxData,
|
|
8
8
|
TxInput,
|
|
9
9
|
TxOutput,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
TxCoinbaseInput,
|
|
11
|
+
TxVirtualInput,
|
|
12
|
+
TxSpendInput
|
|
13
13
|
} from '@/types/index.js'
|
|
14
14
|
|
|
15
15
|
interface TxEncoderConfig {
|
|
@@ -22,7 +22,7 @@ const DEFAULT_CONFIG : TxEncoderConfig = {
|
|
|
22
22
|
segwit : true
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export function
|
|
25
|
+
export function decode_tx (
|
|
26
26
|
txbytes : Bytes,
|
|
27
27
|
options : Partial<TxEncoderConfig> = {}
|
|
28
28
|
) : TxData {
|
|
@@ -87,11 +87,11 @@ function read_vin (stream : Stream, prevout : TxOutput | null = null) : TxInput
|
|
|
87
87
|
const sequence = stream.read(4).reverse().num
|
|
88
88
|
const witness : string[] = []
|
|
89
89
|
if (txid === COINBASE.TXID && vout === COINBASE.VOUT) {
|
|
90
|
-
return { coinbase : script_sig, prevout: null, script_sig : null, sequence, txid, vout, witness } as
|
|
90
|
+
return { coinbase : script_sig, prevout: null, script_sig : null, sequence, txid, vout, witness } as TxCoinbaseInput
|
|
91
91
|
} else if (prevout !== null) {
|
|
92
|
-
return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as
|
|
92
|
+
return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as TxSpendInput
|
|
93
93
|
} else {
|
|
94
|
-
return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as
|
|
94
|
+
return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as TxVirtualInput
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
|
package/src/lib/tx/encode.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
5
|
|
|
6
6
|
import {
|
|
@@ -9,11 +9,11 @@ import {
|
|
|
9
9
|
TxData
|
|
10
10
|
} from '@/types/index.js'
|
|
11
11
|
|
|
12
|
-
export function
|
|
12
|
+
export function encode_tx (
|
|
13
13
|
txdata : TxData,
|
|
14
14
|
segwit = true
|
|
15
15
|
) : Buff {
|
|
16
|
-
const tx =
|
|
16
|
+
const tx = parse_tx(txdata)
|
|
17
17
|
// Unpack the transaction data.
|
|
18
18
|
const { version, vin, vout, locktime } = tx
|
|
19
19
|
// Create a buffer for the transaction.
|
package/src/lib/tx/index.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/src/lib/tx/parse.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
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
4
|
|
|
5
5
|
import type { TxData } from '@/types/index.js'
|
|
6
6
|
|
|
7
|
-
export function
|
|
7
|
+
export function parse_tx (
|
|
8
8
|
txdata : unknown
|
|
9
9
|
) : TxData {
|
|
10
10
|
if (typeof txdata === 'string') {
|
|
11
|
-
return
|
|
11
|
+
return decode_tx(txdata)
|
|
12
12
|
} else {
|
|
13
13
|
assert_tx_template(txdata)
|
|
14
|
-
return
|
|
14
|
+
return create_tx(txdata)
|
|
15
15
|
}
|
|
16
16
|
}
|
package/src/lib/tx/size.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
-
import {
|
|
2
|
+
import { parse_tx } from './parse.js'
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
5
|
+
encode_tx,
|
|
6
6
|
encode_tx_inputs,
|
|
7
7
|
encode_tx_outputs,
|
|
8
8
|
encode_tx_vout,
|
|
@@ -14,12 +14,10 @@ import type {
|
|
|
14
14
|
TxData,
|
|
15
15
|
TxInput,
|
|
16
16
|
TxOutput,
|
|
17
|
-
TxSize
|
|
18
|
-
WitnessSize,
|
|
17
|
+
TxSize
|
|
19
18
|
} from '@/types/index.js'
|
|
20
19
|
|
|
21
|
-
const WIT_FLAG_BYTES
|
|
22
|
-
const WIT_LENGTH_BYTE = 1
|
|
20
|
+
const WIT_FLAG_BYTES = 2
|
|
23
21
|
|
|
24
22
|
export function get_vsize (
|
|
25
23
|
bytes : Bytes
|
|
@@ -32,9 +30,9 @@ export function get_vsize (
|
|
|
32
30
|
export function get_txsize (
|
|
33
31
|
txdata : string | TxData
|
|
34
32
|
) : TxSize {
|
|
35
|
-
const json =
|
|
36
|
-
const base =
|
|
37
|
-
const size =
|
|
33
|
+
const json = parse_tx(txdata)
|
|
34
|
+
const base = encode_tx(json, false).length
|
|
35
|
+
const size = encode_tx(json, true).length
|
|
38
36
|
const weight = base * 3 + size
|
|
39
37
|
const remain = (weight % 4 > 0) ? 1 : 0
|
|
40
38
|
const vsize = Math.floor(weight / 4) + remain
|
|
@@ -68,10 +66,3 @@ export function get_txout_size (txoutput : TxOutput) : number {
|
|
|
68
66
|
const bytes = encode_tx_vout(txoutput)
|
|
69
67
|
return bytes.length
|
|
70
68
|
}
|
|
71
|
-
|
|
72
|
-
export function get_witness_size (witness : Bytes[]) : WitnessSize {
|
|
73
|
-
const stack = witness.map(e => Buff.bytes(e))
|
|
74
|
-
const size = stack.reduce((prev, next) => prev + next.length, 0)
|
|
75
|
-
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4)
|
|
76
|
-
return { size, vsize }
|
|
77
|
-
}
|
|
@@ -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
|
|
|
6
6
|
import type {
|
|
7
7
|
TxData,
|
|
@@ -49,23 +49,23 @@ export function get_vout_version (
|
|
|
49
49
|
export function get_txid (
|
|
50
50
|
txdata : string | TxData
|
|
51
51
|
) : string {
|
|
52
|
-
const json =
|
|
53
|
-
const data =
|
|
52
|
+
const json = parse_tx(txdata)
|
|
53
|
+
const data = encode_tx(json, false)
|
|
54
54
|
return hash256(data).reverse().hex
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export function get_txhash (
|
|
58
58
|
txdata : string | TxData
|
|
59
59
|
) : string {
|
|
60
|
-
const json =
|
|
61
|
-
const data =
|
|
60
|
+
const json = parse_tx(txdata)
|
|
61
|
+
const data = encode_tx(json, true)
|
|
62
62
|
return hash256(data).reverse().hex
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export function get_tx_value (
|
|
66
66
|
txdata : string | TxData
|
|
67
67
|
) : TxValue {
|
|
68
|
-
const tx =
|
|
68
|
+
const tx = parse_tx(txdata)
|
|
69
69
|
const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
|
|
70
70
|
const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n)
|
|
71
71
|
const fees = (vin > vout) ? (vin - vout) : 0n
|
package/src/lib/tx/validate.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as Schema from '@/schema/index.js'
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
TxSpendInput,
|
|
5
5
|
TxData,
|
|
6
6
|
TxInput,
|
|
7
7
|
TxInputTemplate,
|
|
@@ -13,7 +13,7 @@ export function assert_tx_template (txdata : unknown) : asserts txdata is TxTemp
|
|
|
13
13
|
Schema.tx.tx_template.parse(txdata)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export function assert_has_prevouts (vin : TxInput[]) : asserts vin is
|
|
16
|
+
export function assert_has_prevouts (vin : TxInput[]) : asserts vin is TxSpendInput[] {
|
|
17
17
|
if (vin.some(txin => txin.prevout === null)) {
|
|
18
18
|
throw new Error('transaction missing prevouts')
|
|
19
19
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
|
|
3
|
+
import type { WitnessSize } from '@/types/index.js'
|
|
4
|
+
|
|
5
|
+
const WIT_LENGTH_BYTE = 1
|
|
6
|
+
|
|
7
|
+
export function get_witness_size (witness : Bytes[]) : WitnessSize {
|
|
8
|
+
const stack = witness.map(e => Buff.bytes(e))
|
|
9
|
+
const size = stack.reduce((prev, next) => prev + next.length, 0)
|
|
10
|
+
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4)
|
|
11
|
+
return { size, vsize }
|
|
12
|
+
}
|