@vbyte/btc-dev 1.1.3 → 1.1.5

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.
Files changed (66) hide show
  1. package/dist/const.d.ts +16 -0
  2. package/dist/const.js +22 -6
  3. package/dist/lib/address/api.d.ts +4 -0
  4. package/dist/lib/address/api.js +32 -0
  5. package/dist/lib/address/encode.d.ts +3 -3
  6. package/dist/lib/address/index.d.ts +1 -15
  7. package/dist/lib/address/index.js +1 -16
  8. package/dist/lib/address/p2pkh.d.ts +14 -7
  9. package/dist/lib/address/p2pkh.js +34 -16
  10. package/dist/lib/address/p2sh.d.ts +14 -7
  11. package/dist/lib/address/p2sh.js +33 -14
  12. package/dist/lib/address/p2tr.d.ts +14 -5
  13. package/dist/lib/address/p2tr.js +34 -11
  14. package/dist/lib/address/p2wpkh.d.ts +14 -7
  15. package/dist/lib/address/p2wpkh.js +32 -15
  16. package/dist/lib/address/p2wsh.d.ts +14 -7
  17. package/dist/lib/address/p2wsh.js +31 -14
  18. package/dist/lib/address/script.d.ts +2 -5
  19. package/dist/lib/address/script.js +12 -12
  20. package/dist/lib/address/util.d.ts +3 -4
  21. package/dist/lib/address/util.js +10 -14
  22. package/dist/lib/script/decode.d.ts +2 -0
  23. package/dist/lib/script/decode.js +8 -1
  24. package/dist/lib/script/index.d.ts +3 -116
  25. package/dist/lib/script/index.js +3 -6
  26. package/dist/lib/script/lock.d.ts +12 -0
  27. package/dist/lib/script/lock.js +52 -0
  28. package/dist/lib/tx/util.d.ts +1 -6
  29. package/dist/lib/tx/util.js +1 -27
  30. package/dist/lib/witness/parse.js +11 -9
  31. package/dist/main.cjs +345 -194
  32. package/dist/main.cjs.map +1 -1
  33. package/dist/module.mjs +345 -194
  34. package/dist/module.mjs.map +1 -1
  35. package/dist/package.json +1 -1
  36. package/dist/script.js +8 -8
  37. package/dist/script.js.map +1 -1
  38. package/dist/types/address.d.ts +6 -10
  39. package/dist/types/index.d.ts +1 -0
  40. package/dist/types/index.js +1 -0
  41. package/dist/types/meta.d.ts +0 -14
  42. package/dist/types/script.d.ts +15 -0
  43. package/dist/types/script.js +1 -0
  44. package/dist/types/witness.d.ts +7 -8
  45. package/package.json +1 -1
  46. package/src/const.ts +25 -7
  47. package/src/lib/address/api.ts +50 -0
  48. package/src/lib/address/encode.ts +9 -9
  49. package/src/lib/address/index.ts +1 -18
  50. package/src/lib/address/p2pkh.ts +54 -25
  51. package/src/lib/address/p2sh.ts +55 -24
  52. package/src/lib/address/p2tr.ts +59 -19
  53. package/src/lib/address/p2wpkh.ts +53 -26
  54. package/src/lib/address/p2wsh.ts +53 -26
  55. package/src/lib/address/script.ts +14 -14
  56. package/src/lib/address/util.ts +16 -31
  57. package/src/lib/script/decode.ts +11 -1
  58. package/src/lib/script/index.ts +7 -13
  59. package/src/lib/script/lock.ts +73 -0
  60. package/src/lib/tx/util.ts +3 -41
  61. package/src/lib/witness/parse.ts +17 -13
  62. package/src/types/address.ts +7 -11
  63. package/src/types/index.ts +1 -0
  64. package/src/types/meta.ts +0 -18
  65. package/src/types/script.ts +18 -0
  66. package/src/types/witness.ts +8 -8
@@ -1,4 +1,4 @@
1
- import { Buff, Bytes } from '@vbyte/buff'
1
+ 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'
@@ -6,53 +6,15 @@ import { encode_tx } from './encode.js'
6
6
  import { parse_tx } from './parse.js'
7
7
  import { assert_tx_template } from './validate.js'
8
8
 
9
- import { DEFAULT, LOCK_SCRIPT_REGEX } from '@/const.js'
9
+ import { DEFAULT } from '@/const.js'
10
10
 
11
11
  import type {
12
12
  TxData,
13
13
  TxOutput,
14
- TxOutputInfo,
15
14
  TxOutputTemplate,
16
- TxOutputType,
17
- TxValue,
18
- WitnessVersion
15
+ TxValue
19
16
  } from '@/types/index.js'
20
17
 
21
- export function is_return_script (script : Bytes) : boolean {
22
- const bytes = Buff.bytes(script)
23
- return bytes.at(0) === 0x6a
24
- }
25
-
26
- export function get_vout_script_info (script : Bytes) : TxOutputInfo {
27
- return {
28
- type : get_vout_script_type(script),
29
- version : get_vout_script_version(script)
30
- }
31
- }
32
-
33
- export function get_vout_script_type (script : Bytes) : TxOutputType | null {
34
- // Get the hex string of the script.
35
- const hex = Buff.bytes(script).hex
36
- // Iterate over the lock script regexes.
37
- for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
38
- // If the script matches the regex, return the type.
39
- if (regex.test(hex)) return type as TxOutputType
40
- }
41
- // If the script does not match any regex, return null.
42
- return null
43
- }
44
-
45
- export function get_vout_script_version (script : Bytes) : WitnessVersion | null {
46
- // Get the version of the script.
47
- const version = Buff.bytes(script)
48
- // Return the version of the script.
49
- switch (version.at(0)) {
50
- case 0x00 : return 0
51
- case 0x51 : return 1
52
- default : return null
53
- }
54
- }
55
-
56
18
  export function get_txid (txdata : string | Uint8Array | TxData) : string {
57
19
  // If the transaction data is an object,
58
20
  if (typeof txdata === 'object') {
@@ -4,7 +4,8 @@ import { TAPLEAF_VERSIONS } from '@/const.js'
4
4
 
5
5
  import type {
6
6
  WitnessData,
7
- WitnessType
7
+ SpendScriptType,
8
+ WitnessVersion
8
9
  } from '@/types/index.js'
9
10
 
10
11
  export function parse_witness (
@@ -64,13 +65,13 @@ function parse_cblock_data (
64
65
 
65
66
  function parse_witness_script (
66
67
  elems : Uint8Array[],
67
- type : WitnessType
68
+ type : SpendScriptType | null
68
69
  ) {
69
70
  let script : Uint8Array | undefined
70
71
  switch (type) {
71
- case 'p2tr-ts':
72
+ case 'p2ts':
72
73
  script = elems.at(-1)
73
- case 'p2w-sh':
74
+ case 'p2wsh':
74
75
  script = elems.at(-1)
75
76
  }
76
77
  return (script !== undefined) ? new Buff(script).hex : null
@@ -79,14 +80,14 @@ function parse_witness_script (
79
80
  function parse_witness_type (
80
81
  elems : Uint8Array[],
81
82
  cblock : string | null
82
- ) : WitnessType {
83
+ ) : SpendScriptType | null{
83
84
  // Get the important elements of the witness.
84
85
  let param_0 = elems.at(0),
85
86
  param_1 = elems.at(1),
86
87
  param_x = elems.at(-1)
87
88
  // If the cblock is present and the last element exists:
88
89
  if (cblock !== null && param_x !== undefined) {
89
- return 'p2tr-ts'
90
+ return 'p2ts'
90
91
  // If the witness elements match the profile of a p2w-pkh:
91
92
  } else if (
92
93
  elems.length === 2 &&
@@ -95,29 +96,32 @@ function parse_witness_type (
95
96
  param_0.length >= 64 &&
96
97
  param_1.length === 33
97
98
  ) {
98
- return 'p2w-pkh'
99
+ return 'p2wpkh'
99
100
  // If the witness elements match the profile of a p2tr-pk:
100
101
  } else if (
101
102
  elems.length === 1 &&
102
103
  param_0 !== undefined &&
103
104
  param_0.length === 64
104
105
  ) {
105
- return 'p2tr-pk'
106
+ return 'p2tr'
106
107
  // If there is at least two witness elements:
107
108
  } else if (
108
109
  elems.length > 1 &&
109
110
  param_x !== undefined &&
110
111
  is_valid_script(param_x)
111
112
  ) {
112
- return 'p2w-sh'
113
+ return 'p2wsh'
113
114
  // If the witness elements don't match any known profile:
114
115
  } else {
115
- return 'unknown'
116
+ return null
116
117
  }
117
118
  }
118
119
 
119
- function parse_witness_version (type : WitnessType) : number | null {
120
- if (type.startsWith('p2tr')) return 1
121
- if (type.startsWith('p2w')) return 0
120
+ function parse_witness_version (
121
+ type : SpendScriptType | null
122
+ ) : WitnessVersion | null {
123
+ if (type === null) return null
124
+ if (type.startsWith('p2w')) return 0
125
+ if (type.startsWith('p2t')) return 1
122
126
  return null
123
127
  }
@@ -1,7 +1,8 @@
1
+ import type { ScriptInfo } from './script.js'
2
+
1
3
  export type AddressFormat = 'base58' | 'bech32' | 'bech32m'
2
- export type AddressType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr'
4
+ export type AddressType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr'
3
5
  export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string
4
- export type AddressData = AddressContext & ScriptData
5
6
 
6
7
  export type AddressConfigEntry = [
7
8
  prefix : string,
@@ -12,7 +13,7 @@ export type AddressConfigEntry = [
12
13
  version : number
13
14
  ]
14
15
 
15
- export interface DecodedAddress {
16
+ export interface EncoderConfig {
16
17
  format : AddressFormat
17
18
  data : Uint8Array
18
19
  prefix? : string
@@ -28,12 +29,7 @@ export interface AddressConfig {
28
29
  version : number
29
30
  }
30
31
 
31
- export interface AddressContext extends AddressConfig {
32
- data : Uint8Array
33
- hex : string
34
- }
35
-
36
- export interface ScriptData {
37
- script_asm : string[]
38
- script_hex : string
32
+ export interface AddressInfo extends AddressConfig {
33
+ data : string
34
+ script : ScriptInfo
39
35
  }
@@ -1,6 +1,7 @@
1
1
  export * from './address.js'
2
2
  export * from './meta.js'
3
3
  export * from './psbt.js'
4
+ export * from './script.js'
4
5
  export * from './sighash.js'
5
6
  export * from './taproot.js'
6
7
  export * from './txdata.js'
package/src/types/meta.ts CHANGED
@@ -1,19 +1,6 @@
1
- import type{ WitnessType, WitnessVersion } from './witness.js'
2
-
3
1
  export type LocktimeData = LocktimeStamp | LocktimeHeight
4
2
  export type SequenceConfig = Partial<SequenceData>
5
3
  export type SequenceData = SequenceHeightLock | SequenceStampLock
6
- export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn'
7
-
8
- export interface TxOutputInfo {
9
- type : TxOutputType | null
10
- version : WitnessVersion | null
11
- }
12
-
13
- export interface TxInputInfo {
14
- type : WitnessType
15
- version : WitnessVersion
16
- }
17
4
 
18
5
  export interface LocktimeStamp {
19
6
  type : 'timelock' // Discriminator for timelock type
@@ -49,11 +36,6 @@ export interface SequenceField {
49
36
  value : number
50
37
  }
51
38
 
52
- export interface ScriptField {
53
- asm : string[]
54
- hex : string
55
- }
56
-
57
39
  export interface InscriptionData {
58
40
  content ?: string
59
41
  delegate ?: string
@@ -0,0 +1,18 @@
1
+ export type LockScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'opreturn'
2
+ export type SpendScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'p2ts'
3
+ export type WitnessVersion = 0 | 1 | null
4
+
5
+ export interface ScriptInfo {
6
+ asm : string[]
7
+ hex : string
8
+ }
9
+
10
+ export interface LockScriptInfo {
11
+ type : LockScriptType | null
12
+ version : WitnessVersion | null
13
+ }
14
+
15
+ export interface SpendScriptInfo {
16
+ type : SpendScriptType
17
+ version : WitnessVersion
18
+ }
@@ -1,6 +1,6 @@
1
+ import type { SpendScriptType, WitnessVersion } from './script.js'
2
+
1
3
  export type WitnessContext = WitnessData | TaprootScript | SegwitScript | TaprootSpend | SegwitSpend
2
- export type WitnessVersion = number | null
3
- export type WitnessType = 'p2w-pkh' | 'p2w-sh' | 'p2tr-pk' | 'p2tr-ts' | 'unknown'
4
4
 
5
5
  export interface WitnessSize {
6
6
  total : number
@@ -13,30 +13,30 @@ export interface WitnessData {
13
13
  params : string[]
14
14
  script : string | null
15
15
  stack : string[]
16
- type : WitnessType
17
- version : WitnessVersion
16
+ type : SpendScriptType | null
17
+ version : WitnessVersion | null
18
18
  }
19
19
 
20
20
  export interface TaprootScript extends WitnessData {
21
21
  cblock : string
22
22
  script : string
23
- type : 'p2tr-ts'
23
+ type : 'p2ts'
24
24
  version : 1
25
25
  }
26
26
 
27
27
  export interface SegwitScript extends WitnessData {
28
28
  script : string
29
- type : 'p2w-sh'
29
+ type : 'p2wsh'
30
30
  version : 0
31
31
  }
32
32
 
33
33
  export interface TaprootSpend extends WitnessData {
34
- type : 'p2tr-pk'
34
+ type : 'p2tr'
35
35
  version : 1
36
36
  }
37
37
 
38
38
  export interface SegwitSpend extends WitnessData {
39
- type : 'p2w-pkh'
39
+ type : 'p2wpkh'
40
40
  version : 0
41
41
  }
42
42