mainnet-js 2.7.5 → 2.7.6

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,17 +1,13 @@
1
1
  import {
2
2
  CashAddressNetworkPrefix,
3
- encodeCashAddress,
4
3
  CashAddressType,
5
- secp256k1,
6
- decodeCashAddressFormat,
7
- decodeCashAddressFormatWithoutPrefix,
8
- CashAddressVersionByte,
9
4
  decodeCashAddress,
10
- cashAddressTypeBitsToType,
11
- decodeCashAddressVersionByte,
12
- assertSuccess,
5
+ DecodedCashAddress,
6
+ encodeCashAddress,
7
+ secp256k1,
13
8
  } from "@bitauth/libauth";
14
-
9
+ import { prefixFromNetworkMap } from "../enum.js";
10
+ import { Network } from "../interface.js";
15
11
  import { hash160 } from "./hash160.js";
16
12
 
17
13
  export function isValidAddress(cashaddr: string): boolean {
@@ -75,85 +71,61 @@ export function deriveTokenaddr(
75
71
  }).address;
76
72
  }
77
73
 
78
- export function toCashaddr(tokenaddr: string): string {
79
- let result:
80
- | string
81
- | { payload: Uint8Array; prefix: string; version: number }
82
- | undefined;
83
-
84
- // If the address has a prefix decode it as is
85
- if (tokenaddr.includes(":")) {
86
- result = decodeCashAddressFormat(tokenaddr);
87
- }
88
- // otherwise, derive the network from the tokenaddr without prefix
89
- else {
90
- result = decodeCashAddressFormatWithoutPrefix(tokenaddr);
74
+ function decodeAddress(address: string): DecodedCashAddress {
75
+ const result = decodeCashAddress(address);
76
+ if (typeof result === "string") {
77
+ throw new Error(result);
91
78
  }
79
+ return result;
80
+ }
92
81
 
93
- if (typeof result === "string") throw new Error(result);
82
+ export function toCashaddr(address: string): string {
83
+ const result = decodeAddress(address);
94
84
 
95
85
  return encodeCashAddress({
96
86
  prefix: result.prefix as CashAddressNetworkPrefix,
97
- type: CashAddressType.p2pkh,
87
+ type: result.type.replace("WithTokens", "") as CashAddressType,
98
88
  payload: result.payload,
99
89
  }).address;
100
90
  }
101
91
 
102
- export function toTokenaddr(cashaddr: string): string {
103
- let result:
104
- | string
105
- | { payload: Uint8Array; prefix: string; version: number }
106
- | undefined;
107
-
108
- // If the address has a prefix decode it as is
109
- if (cashaddr.includes(":")) {
110
- result = decodeCashAddressFormat(cashaddr);
111
- }
112
- // otherwise, derive the network from the cashaddr without prefix
113
- else {
114
- result = decodeCashAddressFormatWithoutPrefix(cashaddr);
115
- }
116
-
117
- if (typeof result === "string") throw new Error(result);
92
+ export function toTokenaddr(address: string): string {
93
+ const result = decodeAddress(address);
118
94
 
119
95
  return encodeCashAddress({
120
96
  prefix: result.prefix as CashAddressNetworkPrefix,
121
- type: CashAddressType.p2pkhWithTokens,
97
+ type: (result.type.replace("WithTokens", "") +
98
+ "WithTokens") as CashAddressType,
122
99
  payload: result.payload,
123
100
  }).address;
124
101
  }
125
102
 
126
103
  export function isTokenaddr(address: string): boolean {
127
- let result:
128
- | string
129
- | { payload: Uint8Array; prefix: string; version: number }
130
- | undefined;
131
-
132
- // If the address has a prefix decode it as is
133
- if (address.includes(":")) {
134
- result = decodeCashAddressFormat(address);
135
- } else {
136
- // otherwise, derive the network from the address without prefix
137
- result = decodeCashAddressFormatWithoutPrefix(address);
138
- }
139
-
140
- if (typeof result === "string") throw new Error(result);
104
+ const result = decodeAddress(address);
141
105
 
142
- const info = decodeCashAddressVersionByte(result.version);
143
- if (typeof info === "string") throw new Error(info);
106
+ return result.type.endsWith("WithTokens");
107
+ }
144
108
 
145
- const type = cashAddressTypeBitsToType[
146
- info.typeBits as keyof typeof cashAddressTypeBitsToType
147
- ] as CashAddressType | undefined;
148
- if (type === undefined) {
149
- throw Error("Wrong cashaddress type");
150
- }
109
+ // This function converts a cash address to a cash address of the specified network.
110
+ // If withTokens is true, it will return a token-aware address.
111
+ // If withTokens is false, it will return a non-token-aware address.
112
+ // If withTokens is undefined, it will not change the token-awareness.
113
+ export function convertAddress(
114
+ address: string,
115
+ network: Network = "mainnet",
116
+ withTokens: boolean | undefined = undefined
117
+ ): string {
118
+ const result = decodeAddress(address);
151
119
 
152
- return (
153
- [CashAddressType.p2pkhWithTokens, CashAddressType.p2shWithTokens].indexOf(
154
- type
155
- ) !== -1
156
- );
120
+ return encodeCashAddress({
121
+ prefix: prefixFromNetworkMap[network] as CashAddressNetworkPrefix,
122
+ type:
123
+ withTokens === undefined
124
+ ? result.type
125
+ : ((result.type.replace("WithTokens", "") +
126
+ (withTokens ? "WithTokens" : "")) as CashAddressType),
127
+ payload: result.payload,
128
+ }).address;
157
129
  }
158
130
 
159
131
  export function checkTokenaddr(cashaddr: string, enforce: boolean) {