mainnet-js 3.1.7 → 4.0.0-next.10
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/module/cache/IndexedDbCache.d.ts +1 -1
- package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
- package/dist/module/cache/MemoryCache.d.ts +1 -1
- package/dist/module/cache/MemoryCache.d.ts.map +1 -1
- package/dist/module/cache/WebStorageCache.d.ts +1 -1
- package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.d.ts +16 -6
- package/dist/module/cache/walletCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.js +92 -34
- package/dist/module/cache/walletCache.js.map +1 -1
- package/dist/module/cli.js +1 -2
- package/dist/module/cli.js.map +1 -1
- package/dist/module/db/index.d.ts +1 -1
- package/dist/module/db/index.d.ts.map +1 -1
- package/dist/module/db/index.js +1 -1
- package/dist/module/db/index.js.map +1 -1
- package/dist/module/enum.d.ts +1 -1
- package/dist/module/enum.d.ts.map +1 -1
- package/dist/module/history/getHistory.d.ts +1 -1
- package/dist/module/history/getHistory.d.ts.map +1 -1
- package/dist/module/history/getHistory.js +3 -3
- package/dist/module/history/getHistory.js.map +1 -1
- package/dist/module/index.d.ts +25 -22
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +33 -26
- package/dist/module/index.js.map +1 -1
- package/dist/module/interface.d.ts +12 -2
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/libauth.d.ts +1 -1
- package/dist/module/libauth.d.ts.map +1 -1
- package/dist/module/libauth.js +1 -1
- package/dist/module/libauth.js.map +1 -1
- package/dist/module/mine/mine.d.ts +2 -7
- package/dist/module/mine/mine.d.ts.map +1 -1
- package/dist/module/mine/mine.js +6 -27
- package/dist/module/mine/mine.js.map +1 -1
- package/dist/module/network/Connection.d.ts +1 -12
- package/dist/module/network/Connection.d.ts.map +1 -1
- package/dist/module/network/Connection.js +24 -33
- package/dist/module/network/Connection.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +91 -93
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/MockNetworkProvider.d.ts +28 -0
- package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
- package/dist/module/network/MockNetworkProvider.js +74 -0
- package/dist/module/network/MockNetworkProvider.js.map +1 -0
- package/dist/module/network/NetworkProvider.d.ts +9 -2
- package/dist/module/network/NetworkProvider.d.ts.map +1 -1
- package/dist/module/network/configuration.d.ts +2 -4
- package/dist/module/network/configuration.d.ts.map +1 -1
- package/dist/module/network/configuration.js +25 -50
- package/dist/module/network/configuration.js.map +1 -1
- package/dist/module/network/constant.d.ts +7 -7
- package/dist/module/network/constant.d.ts.map +1 -1
- package/dist/module/network/constant.js +21 -24
- package/dist/module/network/constant.js.map +1 -1
- package/dist/module/network/default.d.ts +5 -3
- package/dist/module/network/default.d.ts.map +1 -1
- package/dist/module/network/default.js +29 -51
- package/dist/module/network/default.js.map +1 -1
- package/dist/module/network/index.d.ts +4 -4
- package/dist/module/network/index.d.ts.map +1 -1
- package/dist/module/network/index.js +2 -2
- package/dist/module/network/index.js.map +1 -1
- package/dist/module/network/interface.d.ts +0 -6
- package/dist/module/network/interface.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.d.ts +1 -1
- package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.js +13 -7
- package/dist/module/rate/ExchangeRate.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +3 -3
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +4 -4
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/transaction/allocateFee.d.ts +1 -1
- package/dist/module/transaction/allocateFee.d.ts.map +1 -1
- package/dist/module/transaction/allocateFee.js +2 -2
- package/dist/module/transaction/allocateFee.js.map +1 -1
- package/dist/module/util/deriveCashaddr.js.map +1 -1
- package/dist/module/util/deriveNetwork.js +1 -1
- package/dist/module/util/deriveNetwork.js.map +1 -1
- package/dist/module/util/getAddrsByXpubKey.js +1 -1
- package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
- package/dist/module/util/getXPubKey.js +1 -1
- package/dist/module/util/getXPubKey.js.map +1 -1
- package/dist/module/util/hd.d.ts +2 -0
- package/dist/module/util/hd.d.ts.map +1 -1
- package/dist/module/util/hd.js +41 -0
- package/dist/module/util/hd.js.map +1 -1
- package/dist/module/util/index.d.ts +7 -9
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +7 -9
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/transaction.js +1 -1
- package/dist/module/util/transaction.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +13 -9
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +54 -58
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts.map +1 -1
- package/dist/module/wallet/HDWallet.js +19 -38
- package/dist/module/wallet/HDWallet.js.map +1 -1
- package/dist/module/wallet/Util.d.ts +4 -22
- package/dist/module/wallet/Util.d.ts.map +1 -1
- package/dist/module/wallet/Util.js +75 -102
- package/dist/module/wallet/Util.js.map +1 -1
- package/dist/module/wallet/Watch.d.ts +24 -5
- package/dist/module/wallet/Watch.d.ts.map +1 -1
- package/dist/module/wallet/Watch.js +141 -17
- package/dist/module/wallet/Watch.js.map +1 -1
- package/dist/module/wallet/Wif.d.ts +2 -6
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +3 -69
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +1 -1
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +2 -1
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +3 -2
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +2 -1
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +1 -1
- package/dist/module/wallet/model.js.map +1 -1
- package/package.json +17 -29
- package/dist/index.html +0 -9
- package/dist/mainnet-3.1.7.js +0 -2066
- package/dist/module/network/util.d.ts +0 -3
- package/dist/module/network/util.d.ts.map +0 -1
- package/dist/module/network/util.js +0 -27
- package/dist/module/network/util.js.map +0 -1
- package/dist/module/test/expect.d.ts +0 -12
- package/dist/module/test/expect.d.ts.map +0 -1
- package/dist/module/test/expect.js +0 -47
- package/dist/module/test/expect.js.map +0 -1
- package/dist/module/test/fetch.d.ts +0 -3
- package/dist/module/test/fetch.d.ts.map +0 -1
- package/dist/module/test/fetch.js +0 -32
- package/dist/module/test/fetch.js.map +0 -1
- package/dist/module/util/randomBytes.d.ts +0 -2
- package/dist/module/util/randomBytes.d.ts.map +0 -1
- package/dist/module/util/randomBytes.js +0 -13
- package/dist/module/util/randomBytes.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/cache/IndexedDbCache.test.ts +0 -15
- package/src/cache/IndexedDbCache.ts +0 -172
- package/src/cache/MemoryCache.test.ts +0 -15
- package/src/cache/MemoryCache.ts +0 -32
- package/src/cache/WebStorageCache.test.ts +0 -15
- package/src/cache/WebStorageCache.ts +0 -38
- package/src/cache/index.ts +0 -2
- package/src/cache/interface.ts +0 -9
- package/src/cache/walletCache.ts +0 -254
- package/src/chain.ts +0 -3
- package/src/cli.ts +0 -32
- package/src/config.ts +0 -23
- package/src/constant.ts +0 -27
- package/src/db/ExchangeRateProvider.ts +0 -28
- package/src/db/StorageProvider.ts +0 -64
- package/src/db/index.ts +0 -2
- package/src/db/interface.ts +0 -11
- package/src/enum.ts +0 -34
- package/src/history/getHistory.test.ts +0 -290
- package/src/history/getHistory.ts +0 -411
- package/src/history/interface.ts +0 -24
- package/src/index.ts +0 -48
- package/src/interface.ts +0 -72
- package/src/libauth.ts +0 -11
- package/src/message/index.ts +0 -2
- package/src/message/interface.ts +0 -40
- package/src/message/signed.test.ts +0 -309
- package/src/message/signed.ts +0 -201
- package/src/mine/index.ts +0 -1
- package/src/mine/mine.test.ts +0 -10
- package/src/mine/mine.ts +0 -42
- package/src/network/Connection.test.ts +0 -51
- package/src/network/Connection.ts +0 -73
- package/src/network/ElectrumNetworkProvider.ts +0 -657
- package/src/network/NetworkProvider.ts +0 -180
- package/src/network/Rpc.test.ts +0 -130
- package/src/network/configuration.test.ts +0 -59
- package/src/network/configuration.ts +0 -72
- package/src/network/constant.ts +0 -43
- package/src/network/default.ts +0 -120
- package/src/network/electrum.test.ts +0 -28
- package/src/network/getRelayFeeCache.test.ts +0 -15
- package/src/network/getRelayFeeCache.ts +0 -23
- package/src/network/index.ts +0 -14
- package/src/network/interface.ts +0 -80
- package/src/network/util.test.ts +0 -24
- package/src/network/util.ts +0 -30
- package/src/rate/ExchangeRate.test.headless.js +0 -35
- package/src/rate/ExchangeRate.test.ts +0 -51
- package/src/rate/ExchangeRate.ts +0 -142
- package/src/test/expect.ts +0 -59
- package/src/test/fetch.ts +0 -39
- package/src/test/json.test.ts +0 -13
- package/src/transaction/Wif.ts +0 -680
- package/src/transaction/allocateFee.test.ts +0 -298
- package/src/transaction/allocateFee.ts +0 -149
- package/src/util/amountInSatoshi.test.ts +0 -27
- package/src/util/amountInSatoshi.ts +0 -33
- package/src/util/asSendRequestObject.ts +0 -81
- package/src/util/base64.test.ts +0 -39
- package/src/util/base64.ts +0 -12
- package/src/util/browserNotSupported.ts +0 -7
- package/src/util/checkForEmptySeed.ts +0 -9
- package/src/util/checkUtxos.ts +0 -29
- package/src/util/checkWifNetwork.ts +0 -24
- package/src/util/convert.test.ts +0 -46
- package/src/util/convert.ts +0 -50
- package/src/util/delay.ts +0 -3
- package/src/util/deriveCashaddr.test.ts +0 -164
- package/src/util/deriveCashaddr.ts +0 -143
- package/src/util/deriveLockscript.ts +0 -16
- package/src/util/deriveNetwork.ts +0 -19
- package/src/util/derivePublicKeyHash.test.ts +0 -55
- package/src/util/derivePublicKeyHash.ts +0 -64
- package/src/util/floor.test.ts +0 -21
- package/src/util/floor.ts +0 -4
- package/src/util/getAddrsByXpubKey.test.ts +0 -115
- package/src/util/getAddrsByXpubKey.ts +0 -86
- package/src/util/getRuntimePlatform.test.headless.js +0 -40
- package/src/util/getRuntimePlatform.test.ts +0 -5
- package/src/util/getRuntimePlatform.ts +0 -31
- package/src/util/getUsdRate.ts +0 -5
- package/src/util/getXPubKey.ts +0 -39
- package/src/util/hash160.test.ts +0 -18
- package/src/util/hash160.ts +0 -12
- package/src/util/hd.ts +0 -16
- package/src/util/header.test.ts +0 -34
- package/src/util/header.ts +0 -26
- package/src/util/index.ts +0 -33
- package/src/util/randomBytes.ts +0 -13
- package/src/util/randomInt.test.ts +0 -15
- package/src/util/randomInt.ts +0 -4
- package/src/util/sanitizeAddress.ts +0 -10
- package/src/util/sanitizeUnit.ts +0 -11
- package/src/util/satoshiToAmount.test.ts +0 -6
- package/src/util/satoshiToAmount.ts +0 -33
- package/src/util/sumSendRequestAmounts.ts +0 -34
- package/src/util/sumUtxoValue.ts +0 -27
- package/src/util/transaction.ts +0 -10
- package/src/wallet/Base.ts +0 -1563
- package/src/wallet/Cashtokens.test.headless.js +0 -730
- package/src/wallet/Cashtokens.test.ts +0 -1411
- package/src/wallet/HDWallet.test.ts +0 -1086
- package/src/wallet/HDWallet.ts +0 -992
- package/src/wallet/Util.test.ts +0 -134
- package/src/wallet/Util.ts +0 -191
- package/src/wallet/WalletCache.test.ts +0 -45
- package/src/wallet/Watch.ts +0 -441
- package/src/wallet/Wif.bip39.test.ts +0 -48
- package/src/wallet/Wif.test.ts +0 -1189
- package/src/wallet/Wif.ts +0 -687
- package/src/wallet/Wif.watchOnly.test.ts +0 -58
- package/src/wallet/createWallet.ts +0 -238
- package/src/wallet/enum.ts +0 -18
- package/src/wallet/interface.ts +0 -102
- package/src/wallet/model.test.ts +0 -24
- package/src/wallet/model.ts +0 -352
- package/tsconfig.browser.json +0 -11
- package/tsconfig.json +0 -33
- package/webpack.config.cjs +0 -132
package/src/message/signed.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assertSuccess,
|
|
3
|
-
base64ToBin,
|
|
4
|
-
bigIntToCompactUint,
|
|
5
|
-
binToBase64,
|
|
6
|
-
decodeCashAddress,
|
|
7
|
-
encodeCashAddress,
|
|
8
|
-
RecoveryId,
|
|
9
|
-
secp256k1,
|
|
10
|
-
sha256,
|
|
11
|
-
utf8ToBin,
|
|
12
|
-
} from "@bitauth/libauth";
|
|
13
|
-
|
|
14
|
-
import { hash160 } from "../util/hash160.js";
|
|
15
|
-
import {
|
|
16
|
-
SignedMessageI,
|
|
17
|
-
SignedMessageResponseI,
|
|
18
|
-
VerifyMessageResponseI,
|
|
19
|
-
} from "./interface.js";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* message_magic - Add "Magic", per standard bitcoin message signing.
|
|
23
|
-
*
|
|
24
|
-
* In this case, the magic is simply adding the number 24 as binary "\x18", and "Bitcoin Signed Message\n" followed
|
|
25
|
-
* by the size of the message in Bitcoin's "varint" format followed by the message encoded as binary.
|
|
26
|
-
*
|
|
27
|
-
* @param {str} string The string to add the magic syntax to.
|
|
28
|
-
*
|
|
29
|
-
* @returns a promise to the string as binary array with magic syntax
|
|
30
|
-
*/
|
|
31
|
-
// see https://github.com/Electron-Cash/Electron-Cash/blob/49f9f672364f50053a026e4a5cb30e92db2d195d/electroncash/bitcoin.py#L524
|
|
32
|
-
function message_magic(str: string) {
|
|
33
|
-
const msgBytes = utf8ToBin(str);
|
|
34
|
-
const payload = `\x18Bitcoin Signed Message:\n`;
|
|
35
|
-
return new Uint8Array([
|
|
36
|
-
...utf8ToBin(payload),
|
|
37
|
-
...bigIntToCompactUint(BigInt(msgBytes.length)),
|
|
38
|
-
...msgBytes,
|
|
39
|
-
]);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* hash_magic - Return the hash of the string that has undergone standard formatting
|
|
44
|
-
*
|
|
45
|
-
* @param {str} string The string to hash
|
|
46
|
-
*
|
|
47
|
-
* @returns a promise to the hash of the string.
|
|
48
|
-
*/
|
|
49
|
-
export function hash_message(str: string) {
|
|
50
|
-
const h = sha256.hash;
|
|
51
|
-
return h(h(message_magic(str)));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class SignedMessage implements SignedMessageI {
|
|
55
|
-
/**
|
|
56
|
-
* sign - Calculate the recoverable signed checksum of a string message.
|
|
57
|
-
*
|
|
58
|
-
* @param {message} string The
|
|
59
|
-
* @param {privateKey} Uint8Array The private key to sign the message with
|
|
60
|
-
*
|
|
61
|
-
* @returns a promise to signature as a string
|
|
62
|
-
*/
|
|
63
|
-
public sign(message: string, privateKey: Uint8Array): SignedMessageResponseI {
|
|
64
|
-
const messageHash = hash_message(message);
|
|
65
|
-
const rs = secp256k1.signMessageHashRecoverableCompact(
|
|
66
|
-
privateKey,
|
|
67
|
-
messageHash
|
|
68
|
-
);
|
|
69
|
-
if (typeof rs === "string") {
|
|
70
|
-
throw new Error(rs);
|
|
71
|
-
}
|
|
72
|
-
const sigDer = secp256k1.signMessageHashDER(
|
|
73
|
-
privateKey,
|
|
74
|
-
messageHash
|
|
75
|
-
) as Uint8Array;
|
|
76
|
-
const sigSchnorr = secp256k1.signMessageHashSchnorr(
|
|
77
|
-
privateKey,
|
|
78
|
-
messageHash
|
|
79
|
-
) as Uint8Array;
|
|
80
|
-
const electronEncoding = new Uint8Array([
|
|
81
|
-
...[31 + rs.recoveryId],
|
|
82
|
-
...rs.signature,
|
|
83
|
-
]);
|
|
84
|
-
return {
|
|
85
|
-
raw: {
|
|
86
|
-
ecdsa: binToBase64(rs.signature),
|
|
87
|
-
schnorr: binToBase64(sigSchnorr),
|
|
88
|
-
der: binToBase64(sigDer),
|
|
89
|
-
},
|
|
90
|
-
details: {
|
|
91
|
-
recoveryId: rs.recoveryId,
|
|
92
|
-
compressed: true,
|
|
93
|
-
messageHash: binToBase64(messageHash),
|
|
94
|
-
},
|
|
95
|
-
signature: binToBase64(electronEncoding),
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public static sign(message: string, privateKey: Uint8Array) {
|
|
100
|
-
return new this().sign(message, privateKey);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* verify - Validate that the message is valid against a given signature
|
|
104
|
-
*
|
|
105
|
-
* @param {message} string The message to verify as a utf8 string
|
|
106
|
-
* @param {signature} string The signature as a base64 encoded string
|
|
107
|
-
* @param {cashaddr} string The cashaddr to validate the signature against a recoverable signature.
|
|
108
|
-
* @param {publicKey} string If publicKey is not recoverable from the signature type, the publicKey must be passed.
|
|
109
|
-
*
|
|
110
|
-
* @returns a promise to signature as a string
|
|
111
|
-
*/
|
|
112
|
-
public verify(
|
|
113
|
-
message: string,
|
|
114
|
-
signature: string,
|
|
115
|
-
cashaddr?: string,
|
|
116
|
-
publicKey?: Uint8Array
|
|
117
|
-
): VerifyMessageResponseI {
|
|
118
|
-
// Check that the signature is valid for the given message.
|
|
119
|
-
const messageHash = hash_message(message);
|
|
120
|
-
const sig = base64ToBin(signature);
|
|
121
|
-
|
|
122
|
-
let signatureValid = false;
|
|
123
|
-
let keyMatch = false;
|
|
124
|
-
let pkhMatch = false;
|
|
125
|
-
let pkh, signatureType;
|
|
126
|
-
|
|
127
|
-
if (sig.length === 65) {
|
|
128
|
-
const rawSig = sig.length === 65 ? sig.slice(1) : sig;
|
|
129
|
-
const recoveryId = sig.slice(0, 1)[0] - 31;
|
|
130
|
-
const recoveredPk = secp256k1.recoverPublicKeyCompressed(
|
|
131
|
-
rawSig,
|
|
132
|
-
recoveryId as RecoveryId,
|
|
133
|
-
messageHash
|
|
134
|
-
);
|
|
135
|
-
if (typeof recoveredPk === "string") {
|
|
136
|
-
throw new Error(recoveredPk);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
pkh = hash160(recoveredPk);
|
|
140
|
-
signatureType = "recoverable";
|
|
141
|
-
signatureValid = secp256k1.verifySignatureCompact(
|
|
142
|
-
rawSig,
|
|
143
|
-
recoveredPk,
|
|
144
|
-
messageHash
|
|
145
|
-
);
|
|
146
|
-
if (cashaddr) {
|
|
147
|
-
// Validate that the signature actually matches the provided cashaddr
|
|
148
|
-
const decoded = assertSuccess(decodeCashAddress(cashaddr));
|
|
149
|
-
|
|
150
|
-
const resultingCashaddr = encodeCashAddress({
|
|
151
|
-
prefix: decoded.prefix,
|
|
152
|
-
type: decoded.type,
|
|
153
|
-
payload: pkh,
|
|
154
|
-
}).address;
|
|
155
|
-
if (resultingCashaddr === cashaddr) {
|
|
156
|
-
pkhMatch = true;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
} else if (publicKey) {
|
|
160
|
-
if (secp256k1.verifySignatureDER(sig, publicKey, messageHash)) {
|
|
161
|
-
signatureType = "der";
|
|
162
|
-
signatureValid = true;
|
|
163
|
-
keyMatch = true;
|
|
164
|
-
} else if (
|
|
165
|
-
secp256k1.verifySignatureSchnorr(sig, publicKey, messageHash)
|
|
166
|
-
) {
|
|
167
|
-
signatureType = "schnorr";
|
|
168
|
-
signatureValid = true;
|
|
169
|
-
keyMatch = true;
|
|
170
|
-
} else if (
|
|
171
|
-
secp256k1.verifySignatureCompact(sig, publicKey, messageHash)
|
|
172
|
-
) {
|
|
173
|
-
signatureType = "ecdsa";
|
|
174
|
-
signatureValid = true;
|
|
175
|
-
keyMatch = true;
|
|
176
|
-
} else {
|
|
177
|
-
signatureType = "na";
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
valid: signatureValid && (keyMatch || pkhMatch),
|
|
183
|
-
details: {
|
|
184
|
-
signatureValid: signatureValid,
|
|
185
|
-
signatureType: signatureType,
|
|
186
|
-
messageHash: binToBase64(messageHash),
|
|
187
|
-
publicKeyHashMatch: pkhMatch,
|
|
188
|
-
publicKeyMatch: keyMatch,
|
|
189
|
-
},
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
public static verify(
|
|
194
|
-
message: string,
|
|
195
|
-
signature: string,
|
|
196
|
-
cashaddr?: string,
|
|
197
|
-
publicKey?: Uint8Array
|
|
198
|
-
) {
|
|
199
|
-
return new this().verify(message, signature, cashaddr, publicKey);
|
|
200
|
-
}
|
|
201
|
-
}
|
package/src/mine/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./mine.js";
|
package/src/mine/mine.test.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { mine } from "./mine";
|
|
2
|
-
import { RegTestWallet } from "../wallet/Wif";
|
|
3
|
-
|
|
4
|
-
describe(`Test Mining on Regtest`, () => {
|
|
5
|
-
test("Should mine two blocks", async () => {
|
|
6
|
-
const minerWallet = await RegTestWallet.newRandom();
|
|
7
|
-
const response = await mine({ cashaddr: minerWallet.cashaddr!, blocks: 2 });
|
|
8
|
-
expect(response.length).toBe(2);
|
|
9
|
-
});
|
|
10
|
-
});
|
package/src/mine/mine.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { binToBase64, utf8ToBin } from "@bitauth/libauth";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Mine blocks to a regtest address
|
|
5
|
-
*
|
|
6
|
-
* @param cashaddr - the address to mine to
|
|
7
|
-
* @param blocks - the number of blocks to mine
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* This function assumes a local regtest bitcoin node with RPC_* matching the docker configuration
|
|
11
|
-
*/
|
|
12
|
-
export const mine = async ({
|
|
13
|
-
cashaddr,
|
|
14
|
-
blocks,
|
|
15
|
-
}: {
|
|
16
|
-
cashaddr: string;
|
|
17
|
-
blocks: number;
|
|
18
|
-
}): Promise<any> => {
|
|
19
|
-
const response = await fetch(`http://127.0.0.1:${process.env.RPC_PORT}/`, {
|
|
20
|
-
method: "POST",
|
|
21
|
-
headers: {
|
|
22
|
-
"Content-Type": "application/json",
|
|
23
|
-
Authorization:
|
|
24
|
-
"Basic " +
|
|
25
|
-
binToBase64(
|
|
26
|
-
utf8ToBin(`${process.env.RPC_USER}:${process.env.RPC_PASS}`)
|
|
27
|
-
),
|
|
28
|
-
},
|
|
29
|
-
body: JSON.stringify({
|
|
30
|
-
jsonrpc: "2.0",
|
|
31
|
-
id: "0",
|
|
32
|
-
method: "generatetoaddress",
|
|
33
|
-
params: {
|
|
34
|
-
nblocks: blocks,
|
|
35
|
-
address: cashaddr,
|
|
36
|
-
},
|
|
37
|
-
}),
|
|
38
|
-
});
|
|
39
|
-
const json = await response.json();
|
|
40
|
-
|
|
41
|
-
return json.result;
|
|
42
|
-
};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { initProviders, disconnectProviders, Connection } from "./Connection";
|
|
2
|
-
import { RegTestWallet, TestNetWallet, Wallet } from "../wallet/Wif";
|
|
3
|
-
|
|
4
|
-
beforeAll(async () => {
|
|
5
|
-
await initProviders();
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
afterAll(async () => {
|
|
9
|
-
await disconnectProviders();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("Should connect to mainnet", async () => {
|
|
13
|
-
process.setMaxListeners(0);
|
|
14
|
-
|
|
15
|
-
let wallet = await Wallet.newRandom();
|
|
16
|
-
expect(wallet.provider == globalThis.BCH).toBeTruthy();
|
|
17
|
-
expect(await wallet.getBalance()).toBe(0n);
|
|
18
|
-
|
|
19
|
-
let height = await globalThis.BCH.getBlockHeight();
|
|
20
|
-
expect(height).toBeGreaterThan(5000);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("Should use global provider when creating testnet wallet", async () => {
|
|
24
|
-
let wallet = await TestNetWallet.newRandom();
|
|
25
|
-
expect(wallet.provider == globalThis.tBCH).toBeTruthy();
|
|
26
|
-
expect(await wallet.getBalance()).toBe(0n);
|
|
27
|
-
|
|
28
|
-
let height = await globalThis.tBCH.getBlockHeight();
|
|
29
|
-
expect(height).toBeGreaterThan(114);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test.skip("Should lower overhead in creating wallets", async () => {
|
|
33
|
-
process.setMaxListeners(0);
|
|
34
|
-
for (let i = 0; i < 100; i++) {
|
|
35
|
-
let wallet = await RegTestWallet.newRandom();
|
|
36
|
-
expect(wallet.provider == globalThis.rBCH).toBeTruthy();
|
|
37
|
-
expect(await wallet.getBalance()).toBe(0n);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let height = await globalThis.rBCH.getBlockHeight();
|
|
41
|
-
expect(height).toBeGreaterThan(114);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("Should create a new Connection", async () => {
|
|
45
|
-
let conn = new Connection("mainnet", "wss://bch.imaginary.cash:50004");
|
|
46
|
-
await conn.ready();
|
|
47
|
-
expect(conn.networkProvider == globalThis.BCH).toBeFalsy();
|
|
48
|
-
let blockheight = await conn.networkProvider.getBlockHeight();
|
|
49
|
-
expect(blockheight).toBeGreaterThan(10000);
|
|
50
|
-
expect(10001).toBeGreaterThan(10000);
|
|
51
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { default as NetworkProvider } from "./NetworkProvider.js";
|
|
2
|
-
import {
|
|
3
|
-
getNetworkProvider,
|
|
4
|
-
setGlobalProvider,
|
|
5
|
-
getGlobalProvider,
|
|
6
|
-
removeGlobalProvider,
|
|
7
|
-
} from "./default.js";
|
|
8
|
-
import { Network } from "../interface.js";
|
|
9
|
-
import { networkTickerMap } from "./constant.js";
|
|
10
|
-
import { prefixFromNetworkMap } from "../enum.js";
|
|
11
|
-
import { CashAddressNetworkPrefix } from "@bitauth/libauth";
|
|
12
|
-
|
|
13
|
-
export async function initProvider(network: Network) {
|
|
14
|
-
if (!getGlobalProvider(network)) {
|
|
15
|
-
const conn = new Connection(network);
|
|
16
|
-
const provider = (await conn.ready()).networkProvider;
|
|
17
|
-
setGlobalProvider(network, provider);
|
|
18
|
-
return provider;
|
|
19
|
-
}
|
|
20
|
-
return getGlobalProvider(network);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function initProviders(networks?: Network[]) {
|
|
24
|
-
networks = networks ? networks : (Object.keys(networkTickerMap) as Network[]);
|
|
25
|
-
let initPromises = networks.map((n) => initProvider(n));
|
|
26
|
-
await Promise.all(initPromises).catch((e) => {
|
|
27
|
-
console.warn(`Warning, couldn't establish a connection for ${e}`);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function disconnectProvider(network: Network) {
|
|
32
|
-
const provider = getGlobalProvider(network);
|
|
33
|
-
if (provider) {
|
|
34
|
-
await provider.disconnect();
|
|
35
|
-
removeGlobalProvider(network);
|
|
36
|
-
return;
|
|
37
|
-
} else {
|
|
38
|
-
// console.warn(
|
|
39
|
-
// `Ignoring attempt to disconnect non-existent ${network} provider`
|
|
40
|
-
// );
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function disconnectProviders(networks?: Network[]) {
|
|
46
|
-
networks = networks ? networks : (Object.keys(networkTickerMap) as Network[]);
|
|
47
|
-
let disconnectPromises = networks.map((n) => disconnectProvider(n));
|
|
48
|
-
await Promise.all(disconnectPromises);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export class Connection {
|
|
52
|
-
public network: Network;
|
|
53
|
-
public servers?: string[];
|
|
54
|
-
public networkPrefix: CashAddressNetworkPrefix;
|
|
55
|
-
public networkProvider: NetworkProvider;
|
|
56
|
-
|
|
57
|
-
constructor(network?: Network, servers?: string[] | string) {
|
|
58
|
-
this.network = network ? network : "mainnet";
|
|
59
|
-
this.networkPrefix = prefixFromNetworkMap[this.network];
|
|
60
|
-
this.networkProvider = getNetworkProvider(this.network, servers, true);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public async ready() {
|
|
64
|
-
await this.networkProvider.connect();
|
|
65
|
-
await this.networkProvider.ready();
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public async disconnect() {
|
|
70
|
-
await this.networkProvider.disconnect();
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
}
|