mainnet-js 2.7.4 → 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,5 +1,6 @@
1
1
  import { RegTestWallet, Wallet } from "../wallet/Wif";
2
2
  import {
3
+ convertAddress,
3
4
  deriveCashaddr,
4
5
  deriveTokenaddr,
5
6
  isTokenaddr,
@@ -8,6 +9,46 @@ import {
8
9
  toTokenaddr,
9
10
  } from "./deriveCashaddr";
10
11
 
12
+ const p2pkhAddress = "bitcoincash:qpttdv3qg2usm4nm7talhxhl05mlhms3ystlwcm8h4";
13
+ const p2pkhTokenAddress =
14
+ "bitcoincash:zpttdv3qg2usm4nm7talhxhl05mlhms3ysv4ax4pgx";
15
+
16
+ const testnetP2pkhAddress =
17
+ "bchtest:qpttdv3qg2usm4nm7talhxhl05mlhms3ys0d2lessf";
18
+ const testnetP2pkhTokenAddress =
19
+ "bchtest:zpttdv3qg2usm4nm7talhxhl05mlhms3ysg8ephk06";
20
+
21
+ const regtestP2pkhAddress = "bchreg:qpttdv3qg2usm4nm7talhxhl05mlhms3ys43u76rn0";
22
+ const regtestP2pkhTokenAddress =
23
+ "bchreg:zpttdv3qg2usm4nm7talhxhl05mlhms3ysjm0q59vu";
24
+
25
+ const p2shAddress = "bitcoincash:ppvamrzvn62c85tv0y4ncdxyy77csxk7qgcjrpwp98";
26
+ const p2shTokenAddress =
27
+ "bitcoincash:rpvamrzvn62c85tv0y4ncdxyy77csxk7qglcslq865";
28
+
29
+ const testnetP2shAddress = "bchtest:ppvamrzvn62c85tv0y4ncdxyy77csxk7qguq8xvkzm";
30
+ const testnetP2shTokenAddress =
31
+ "bchtest:rpvamrzvn62c85tv0y4ncdxyy77csxk7qgm25czsag";
32
+
33
+ const regtestP2shAddress = "bchreg:ppvamrzvn62c85tv0y4ncdxyy77csxk7qgxu3809pa";
34
+ const regtestP2shTokenAddress =
35
+ "bchreg:rpvamrzvn62c85tv0y4ncdxyy77csxk7qgpkzepr7w";
36
+
37
+ const p2sh32Address =
38
+ "bitcoincash:pww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xx5auz6k7z";
39
+ const p2sh32TokenAddress =
40
+ "bitcoincash:rww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xxxwqrrh8f";
41
+
42
+ const testnetP2sh32Address =
43
+ "bchtest:pww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xxh6duz9ts";
44
+ const testnetP2sh32TokenAddress =
45
+ "bchtest:rww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xx9f3amyjm";
46
+
47
+ const regtestP2sh32Address =
48
+ "bchreg:pww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xxzslk9juc";
49
+ const regtestP2sh32TokenAddress =
50
+ "bchreg:rww87w869tuuzw4avtyxtlwhq32l25xkyy9n3xhzfelhj0d3vm8xxsrrhun9n";
51
+
11
52
  test("Should derive cashaddr", async () => {
12
53
  const wallet = await Wallet.newRandom();
13
54
  expect(deriveCashaddr(wallet.privateKey!, wallet.networkPrefix)).toBe(
@@ -29,23 +70,95 @@ test("Should derive cashaddr", async () => {
29
70
 
30
71
  test("Test address conversion", async () => {
31
72
  const wallet = await RegTestWallet.watchOnly(process.env.ADDRESS!);
32
- expect(toTokenaddr(wallet.cashaddr!)).toBe(wallet.tokenaddr);
33
- expect(toCashaddr(wallet.tokenaddr!)).toBe(wallet.cashaddr);
34
73
 
35
- expect(toCashaddr(wallet.tokenaddr!)).toBe(
74
+ // p2pkh
75
+ expect(toTokenaddr(wallet.cashaddr)).toBe(wallet.tokenaddr);
76
+ expect(toCashaddr(wallet.cashaddr)).toBe(wallet.cashaddr);
77
+
78
+ // p2pkh with tokens
79
+ expect(toCashaddr(wallet.tokenaddr)).toBe(wallet.cashaddr);
80
+ expect(toTokenaddr(wallet.tokenaddr)).toBe(wallet.tokenaddr);
81
+
82
+ // p2sh
83
+ expect(toCashaddr(p2shAddress)).toBe(p2shAddress);
84
+ expect(toTokenaddr(p2shAddress)).toBe(p2shTokenAddress);
85
+
86
+ // p2sh with tokens
87
+ expect(toCashaddr(p2shTokenAddress)).toBe(p2shAddress);
88
+ expect(toTokenaddr(p2shTokenAddress)).toBe(p2shTokenAddress);
89
+
90
+ // p2sh32
91
+ expect(toCashaddr(p2sh32Address)).toBe(p2sh32Address);
92
+ expect(toTokenaddr(p2sh32Address)).toBe(p2sh32TokenAddress);
93
+
94
+ // p2sh32 with tokens
95
+ expect(toCashaddr(p2sh32TokenAddress)).toBe(p2sh32Address);
96
+ expect(toTokenaddr(p2sh32TokenAddress)).toBe(p2sh32TokenAddress);
97
+
98
+ expect(toCashaddr(wallet.tokenaddr)).toBe(
36
99
  "bchreg:qpttdv3qg2usm4nm7talhxhl05mlhms3ys43u76rn0"
37
100
  );
38
- expect(toTokenaddr(wallet.cashaddr!)).toBe(
101
+ expect(toTokenaddr(wallet.cashaddr)).toBe(
39
102
  "bchreg:zpttdv3qg2usm4nm7talhxhl05mlhms3ysjm0q59vu"
40
103
  );
41
104
 
42
- expect(isTokenaddr(wallet.cashaddr!)).toBe(false);
43
- expect(isTokenaddr(wallet.tokenaddr!)).toBe(true);
105
+ expect(isTokenaddr(wallet.cashaddr)).toBe(false);
106
+ expect(isTokenaddr(wallet.tokenaddr)).toBe(true);
107
+ });
108
+
109
+ test("Test address network conversion", async () => {
110
+ expect(convertAddress(p2pkhAddress, "mainnet")).toBe(p2pkhAddress);
111
+ expect(convertAddress(p2pkhAddress, "testnet")).toBe(testnetP2pkhAddress);
112
+ expect(convertAddress(p2pkhAddress, "regtest")).toBe(regtestP2pkhAddress);
113
+
114
+ expect(convertAddress(p2pkhTokenAddress, "mainnet")).toBe(p2pkhTokenAddress);
115
+ expect(convertAddress(p2pkhTokenAddress, "testnet")).toBe(
116
+ testnetP2pkhTokenAddress
117
+ );
118
+ expect(convertAddress(p2pkhTokenAddress, "regtest")).toBe(
119
+ regtestP2pkhTokenAddress
120
+ );
121
+
122
+ expect(convertAddress(p2shAddress, "mainnet")).toBe(p2shAddress);
123
+ expect(convertAddress(p2shAddress, "testnet")).toBe(testnetP2shAddress);
124
+ expect(convertAddress(p2shAddress, "regtest")).toBe(regtestP2shAddress);
125
+
126
+ expect(convertAddress(p2shTokenAddress, "mainnet")).toBe(p2shTokenAddress);
127
+ expect(convertAddress(p2shTokenAddress, "testnet")).toBe(
128
+ testnetP2shTokenAddress
129
+ );
130
+ expect(convertAddress(p2shTokenAddress, "regtest")).toBe(
131
+ regtestP2shTokenAddress
132
+ );
133
+
134
+ expect(convertAddress(p2sh32Address, "mainnet")).toBe(p2sh32Address);
135
+ expect(convertAddress(p2sh32Address, "testnet")).toBe(testnetP2sh32Address);
136
+ expect(convertAddress(p2sh32Address, "regtest")).toBe(regtestP2sh32Address);
137
+
138
+ expect(convertAddress(p2sh32TokenAddress, "mainnet")).toBe(
139
+ p2sh32TokenAddress
140
+ );
141
+ expect(convertAddress(p2sh32TokenAddress, "testnet")).toBe(
142
+ testnetP2sh32TokenAddress
143
+ );
144
+ expect(convertAddress(p2sh32TokenAddress, "regtest")).toBe(
145
+ regtestP2sh32TokenAddress
146
+ );
147
+
148
+ // change token awareness
149
+ expect(convertAddress(p2pkhAddress, "mainnet", true)).toBe(p2pkhTokenAddress);
150
+ expect(convertAddress(p2pkhAddress, "mainnet", false)).toBe(p2pkhAddress);
151
+ expect(convertAddress(p2pkhTokenAddress, "mainnet", true)).toBe(
152
+ p2pkhTokenAddress
153
+ );
154
+ expect(convertAddress(p2pkhTokenAddress, "mainnet", false)).toBe(
155
+ p2pkhAddress
156
+ );
44
157
  });
45
158
 
46
159
  test("Test isValidAddress", async () => {
47
160
  const wallet = await RegTestWallet.watchOnly(process.env.ADDRESS!);
48
- expect(isValidAddress(wallet.cashaddr!)).toBe(true);
49
- expect(isValidAddress(wallet.tokenaddr!)).toBe(true);
161
+ expect(isValidAddress(wallet.cashaddr)).toBe(true);
162
+ expect(isValidAddress(wallet.tokenaddr)).toBe(true);
50
163
  expect(isValidAddress("asdf")).toBe(false);
51
164
  });
@@ -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) {
@@ -1,21 +0,0 @@
1
- /*
2
- * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
3
- * This devtool is neither made for production nor for readable output files.
4
- * It uses "eval()" calls to create a separate source file in the browser devtools.
5
- * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
6
- * or disable the default devtool with "devtool: false".
7
- * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
8
- */
9
- (self["webpackChunkmainnet_js"] = self["webpackChunkmainnet_js"] || []).push([["_67af"],{
10
-
11
- /***/ "?67af":
12
- /*!*******************************!*\
13
- !*** child_process (ignored) ***!
14
- \*******************************/
15
- /***/ (() => {
16
-
17
- eval("/* (ignored) */\n\n//# sourceURL=webpack://mainnet-js/child_process_(ignored)?");
18
-
19
- /***/ })
20
-
21
- }])