@vbyte/btc-dev 1.1.5 → 1.1.7

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.
@@ -1,4 +1,4 @@
1
1
  import { Bytes } from '@vbyte/buff';
2
2
  import type { AddressInfo, ChainNetwork } from '../../types/index.js';
3
- export declare function create_address(script: Bytes, network?: ChainNetwork): string;
3
+ export declare function get_address(script: Bytes, network?: ChainNetwork): string;
4
4
  export declare function parse_address(address: string): AddressInfo;
@@ -7,24 +7,24 @@ import { P2SH } from './p2sh.js';
7
7
  import { P2TR } from './p2tr.js';
8
8
  import { P2WPKH } from './p2wpkh.js';
9
9
  import { P2WSH } from './p2wsh.js';
10
- export function create_address(script, network = 'main') {
10
+ export function get_address(script, network = 'main') {
11
11
  const bytes = Buff.bytes(script);
12
12
  const type = get_lock_script_type(bytes);
13
13
  if (type === null)
14
- throw new Error('unrecognized script type: ' + bytes.hex);
14
+ throw new Error('unknown locking script: ' + bytes.hex);
15
15
  switch (type) {
16
16
  case LOCK_SCRIPT_TYPE.P2PKH:
17
- return P2PKH.create_address(script, network);
17
+ return P2PKH.encode_address(script, network);
18
18
  case LOCK_SCRIPT_TYPE.P2SH:
19
- return P2SH.create_address(script, network);
19
+ return P2SH.encode_address(script, network);
20
20
  case LOCK_SCRIPT_TYPE.P2WPKH:
21
- return P2WPKH.create_address(script, network);
21
+ return P2WPKH.encode_address(script, network);
22
22
  case LOCK_SCRIPT_TYPE.P2WSH:
23
- return P2WSH.create_address(script, network);
23
+ return P2WSH.encode_address(script, network);
24
24
  case LOCK_SCRIPT_TYPE.P2TR:
25
- return P2TR.create_address(script, network);
25
+ return P2TR.encode_address(script, network);
26
26
  default:
27
- throw new Error('unrecognized script type: ' + type);
27
+ throw new Error('unknown script type: ' + type);
28
28
  }
29
29
  }
30
30
  export function parse_address(address) {
@@ -6,9 +6,8 @@ export function decode_tx(txdata, use_segwit = true) {
6
6
  Assert.is_bytes(txdata, 'txdata must be hex or bytes');
7
7
  const stream = new Stream(txdata);
8
8
  const version = read_version(stream);
9
- const has_witness = (use_segwit)
10
- ? check_witness_flag(stream)
11
- : false;
9
+ let has_witness = check_witness_flag(stream);
10
+ has_witness = (use_segwit) ? has_witness : false;
12
11
  const vin = read_inputs(stream);
13
12
  const vout = read_outputs(stream);
14
13
  if (has_witness) {
@@ -1,4 +1,6 @@
1
+ import { Buff } from '@vbyte/buff';
1
2
  import type { TxData, TxOutput, TxOutputTemplate, TxValue } from '../../types/index.js';
3
+ export declare function transcode_tx(txdata: string | Uint8Array, use_segwit?: boolean): Buff;
2
4
  export declare function get_txid(txdata: string | Uint8Array | TxData): string;
3
5
  export declare function get_txhash(txdata: string | Uint8Array | TxData): string;
4
6
  export declare function get_tx_value(txdata: string | Uint8Array | TxData): TxValue;
@@ -2,16 +2,34 @@ import { Buff } from '@vbyte/buff';
2
2
  import { Test } from '@vbyte/micro-lib';
3
3
  import { Assert } from '@vbyte/micro-lib/assert';
4
4
  import { hash256 } from '@vbyte/micro-lib/hash';
5
+ import { decode_tx } from './decode.js';
5
6
  import { encode_tx } from './encode.js';
6
7
  import { parse_tx } from './parse.js';
7
8
  import { assert_tx_template } from './validate.js';
8
9
  import { DEFAULT } from '../../const.js';
10
+ export function transcode_tx(txdata, use_segwit = true) {
11
+ console.log('txdata:', txdata);
12
+ const decoded = decode_tx(txdata);
13
+ console.log('decoded:', decoded);
14
+ return encode_tx(decoded, use_segwit);
15
+ }
9
16
  export function get_txid(txdata) {
10
- if (typeof txdata === 'object') {
17
+ let buffer;
18
+ if (txdata instanceof Uint8Array) {
19
+ buffer = transcode_tx(txdata, false);
20
+ }
21
+ else if (typeof txdata === 'object') {
11
22
  assert_tx_template(txdata);
12
- txdata = encode_tx(txdata, false);
23
+ buffer = encode_tx(txdata, false);
13
24
  }
14
- return hash256(txdata).reverse().hex;
25
+ else if (typeof txdata === 'string') {
26
+ Assert.is_hex(txdata);
27
+ buffer = transcode_tx(txdata, false);
28
+ }
29
+ else {
30
+ throw new TypeError('invalid txdata type: ' + typeof txdata);
31
+ }
32
+ return hash256(buffer).reverse().hex;
15
33
  }
16
34
  export function get_txhash(txdata) {
17
35
  if (typeof txdata === 'object') {