beignet 0.0.1
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/.eslintignore +4 -0
- package/.eslintrc +40 -0
- package/.prettierrc +9 -0
- package/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/electrum/constants.js +31 -0
- package/dist/electrum/constants.js.map +1 -0
- package/dist/electrum/index.js +552 -0
- package/dist/electrum/index.js.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/shapes/electrum.js +14 -0
- package/dist/shapes/electrum.js.map +1 -0
- package/dist/shapes/index.js +19 -0
- package/dist/shapes/index.js.map +1 -0
- package/dist/shapes/wallet.js +115 -0
- package/dist/shapes/wallet.js.map +1 -0
- package/dist/transaction/index.js +692 -0
- package/dist/transaction/index.js.map +1 -0
- package/dist/types/electrum/constants.d.ts +7 -0
- package/dist/types/electrum/index.d.ts +85 -0
- package/dist/types/electrum.js +9 -0
- package/dist/types/electrum.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +20 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/shapes/electrum.d.ts +5 -0
- package/dist/types/shapes/index.d.ts +2 -0
- package/dist/types/shapes/wallet.d.ts +10 -0
- package/dist/types/transaction/index.d.ts +92 -0
- package/dist/types/transaction.js +12 -0
- package/dist/types/transaction.js.map +1 -0
- package/dist/types/types/electrum.d.ts +113 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/transaction.d.ts +51 -0
- package/dist/types/types/wallet.d.ts +400 -0
- package/dist/types/utils/conversion.d.ts +10 -0
- package/dist/types/utils/derivation-path.d.ts +19 -0
- package/dist/types/utils/electrum.d.ts +10 -0
- package/dist/types/utils/exchange-rate.d.ts +30 -0
- package/dist/types/utils/helpers.d.ts +23 -0
- package/dist/types/utils/index.d.ts +7 -0
- package/dist/types/utils/result.d.ts +16 -0
- package/dist/types/utils/transaction.d.ts +21 -0
- package/dist/types/utils/wallet.d.ts +47 -0
- package/dist/types/wallet/constants.d.ts +12 -0
- package/dist/types/wallet/index.d.ts +214 -0
- package/dist/types/wallet.js +36 -0
- package/dist/types/wallet.js.map +1 -0
- package/dist/utils/conversion.js +52 -0
- package/dist/utils/conversion.js.map +1 -0
- package/dist/utils/derivation-path.js +104 -0
- package/dist/utils/derivation-path.js.map +1 -0
- package/dist/utils/electrum.js +149 -0
- package/dist/utils/electrum.js.map +1 -0
- package/dist/utils/exchange-rate.js +49 -0
- package/dist/utils/exchange-rate.js.map +1 -0
- package/dist/utils/helpers.js +137 -0
- package/dist/utils/helpers.js.map +1 -0
- package/dist/utils/index.js +24 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/result.js +36 -0
- package/dist/utils/result.js.map +1 -0
- package/dist/utils/transaction.js +327 -0
- package/dist/utils/transaction.js.map +1 -0
- package/dist/utils/wallet.js +227 -0
- package/dist/utils/wallet.js.map +1 -0
- package/dist/wallet/constants.js +15 -0
- package/dist/wallet/constants.js.map +1 -0
- package/dist/wallet/index.js +2113 -0
- package/dist/wallet/index.js.map +1 -0
- package/docs/html/.nojekyll +1 -0
- package/docs/html/assets/highlight.css +22 -0
- package/docs/html/assets/main.js +58 -0
- package/docs/html/assets/search.js +1 -0
- package/docs/html/assets/style.css +1367 -0
- package/docs/html/classes/Electrum.html +812 -0
- package/docs/html/classes/Transaction.html +687 -0
- package/docs/html/classes/Wallet.html +2030 -0
- package/docs/html/enums/EAddressType.html +227 -0
- package/docs/html/enums/EAvailableNetworks.html +255 -0
- package/docs/html/enums/EBoostType.html +220 -0
- package/docs/html/enums/EFeeId.html +241 -0
- package/docs/html/enums/EPaymentType.html +220 -0
- package/docs/html/enums/EProtocol.html +220 -0
- package/docs/html/enums/EUnit.html +227 -0
- package/docs/html/functions/availableNetworks.html +200 -0
- package/docs/html/functions/constructByteCountParam.html +210 -0
- package/docs/html/functions/decodeOpReturnMessage.html +202 -0
- package/docs/html/functions/decodeRawTransaction.html +210 -0
- package/docs/html/functions/err.html +214 -0
- package/docs/html/functions/formatKeyDerivationPath.html +219 -0
- package/docs/html/functions/formatPeerData.html +207 -0
- package/docs/html/functions/generateMnemonic.html +223 -0
- package/docs/html/functions/generateWalletId.html +202 -0
- package/docs/html/functions/getAddressFromScriptPubKey.html +209 -0
- package/docs/html/functions/getAddressTypeFromPath.html +206 -0
- package/docs/html/functions/getByteCount.html +206 -0
- package/docs/html/functions/getDataFallback.html +207 -0
- package/docs/html/functions/getDefaultPort.html +209 -0
- package/docs/html/functions/getDefaultWalletData.html +200 -0
- package/docs/html/functions/getDefaultWalletDataKeys.html +200 -0
- package/docs/html/functions/getExchangeRates.html +200 -0
- package/docs/html/functions/getHighestUsedIndexFromTxHashes.html +216 -0
- package/docs/html/functions/getKeyDerivationPath.html +211 -0
- package/docs/html/functions/getKeyDerivationPathObject.html +216 -0
- package/docs/html/functions/getKeyDerivationPathString.html +220 -0
- package/docs/html/functions/getKeyValue.html +206 -0
- package/docs/html/functions/getPeers.html +211 -0
- package/docs/html/functions/getProtocolForPort.html +209 -0
- package/docs/html/functions/getScriptHash.html +210 -0
- package/docs/html/functions/getSeed.html +209 -0
- package/docs/html/functions/getSeedHash.html +206 -0
- package/docs/html/functions/getSha256.html +206 -0
- package/docs/html/functions/getStorageKeyValues.html +209 -0
- package/docs/html/functions/getTxFee.html +210 -0
- package/docs/html/functions/getWalletDataStorageKey.html +206 -0
- package/docs/html/functions/isValidBech32mEncodedString.html +211 -0
- package/docs/html/functions/objectKeys-1.html +211 -0
- package/docs/html/functions/objectsMatch.html +210 -0
- package/docs/html/functions/ok.html +214 -0
- package/docs/html/functions/parseOnChainPaymentRequest.html +204 -0
- package/docs/html/functions/reduceValue.html +215 -0
- package/docs/html/functions/removeDustOutputs.html +206 -0
- package/docs/html/functions/setReplaceByFee.html +210 -0
- package/docs/html/functions/shuffleArray.html +211 -0
- package/docs/html/functions/validateAddress.html +216 -0
- package/docs/html/functions/validateMnemonic.html +206 -0
- package/docs/html/functions/validateTransaction.html +206 -0
- package/docs/html/index.html +360 -0
- package/docs/html/interfaces/IAddInput.html +231 -0
- package/docs/html/interfaces/IAddress.html +247 -0
- package/docs/html/interfaces/IAddressData.html +231 -0
- package/docs/html/interfaces/IAddressType.html +199 -0
- package/docs/html/interfaces/IAddressTypeData.html +252 -0
- package/docs/html/interfaces/IAddresses.html +199 -0
- package/docs/html/interfaces/IBoostedTransaction.html +238 -0
- package/docs/html/interfaces/IBoostedTransactions.html +199 -0
- package/docs/html/interfaces/ICreateTransaction.html +224 -0
- package/docs/html/interfaces/ICustomGetAddress.html +231 -0
- package/docs/html/interfaces/ICustomGetScriptHash.html +224 -0
- package/docs/html/interfaces/IElectrumGetAddressBalanceRes.html +235 -0
- package/docs/html/interfaces/IExchangeRates.html +210 -0
- package/docs/html/interfaces/IFormattedPeerData.html +245 -0
- package/docs/html/interfaces/IFormattedTransaction.html +343 -0
- package/docs/html/interfaces/IFormattedTransactions.html +199 -0
- package/docs/html/interfaces/IGenerateAddresses.html +252 -0
- package/docs/html/interfaces/IGenerateAddressesResponse.html +224 -0
- package/docs/html/interfaces/IGetAddress.html +231 -0
- package/docs/html/interfaces/IGetAddressBalanceRes.html +226 -0
- package/docs/html/interfaces/IGetAddressByPath.html +224 -0
- package/docs/html/interfaces/IGetAddressHistoryResponse.html +269 -0
- package/docs/html/interfaces/IGetAddressResponse.html +231 -0
- package/docs/html/interfaces/IGetAddressScriptHashesHistoryResponse.html +245 -0
- package/docs/html/interfaces/IGetDerivationPath.html +259 -0
- package/docs/html/interfaces/IGetFeeEstimatesResponse.html +238 -0
- package/docs/html/interfaces/IGetHeaderResponse.html +245 -0
- package/docs/html/interfaces/IGetNextAvailableAddressResponse.html +238 -0
- package/docs/html/interfaces/IGetTransactions.html +245 -0
- package/docs/html/interfaces/IGetTransactionsFromInputs.html +245 -0
- package/docs/html/interfaces/IGetUtxosResponse.html +224 -0
- package/docs/html/interfaces/IHeader.html +231 -0
- package/docs/html/interfaces/IIndexes.html +238 -0
- package/docs/html/interfaces/IKeyDerivationPath.html +247 -0
- package/docs/html/interfaces/IKeyDerivationPathData.html +224 -0
- package/docs/html/interfaces/INewBlock.html +224 -0
- package/docs/html/interfaces/IOnchainFees.html +245 -0
- package/docs/html/interfaces/IOutput.html +231 -0
- package/docs/html/interfaces/IPeerData.html +231 -0
- package/docs/html/interfaces/IRbfData.html +252 -0
- package/docs/html/interfaces/ISend.html +224 -0
- package/docs/html/interfaces/ISendTransaction.html +322 -0
- package/docs/html/interfaces/ISendTx.html +231 -0
- package/docs/html/interfaces/ISetupTransaction.html +238 -0
- package/docs/html/interfaces/ISubscribeToAddress.html +247 -0
- package/docs/html/interfaces/ISubscribeToHeader.html +245 -0
- package/docs/html/interfaces/ITargets.html +238 -0
- package/docs/html/interfaces/ITransaction.html +281 -0
- package/docs/html/interfaces/ITxHash.html +217 -0
- package/docs/html/interfaces/ITxHashes.html +235 -0
- package/docs/html/interfaces/IUtxo.html +275 -0
- package/docs/html/interfaces/IVin.html +252 -0
- package/docs/html/interfaces/IVout.html +246 -0
- package/docs/html/interfaces/IWallet.html +350 -0
- package/docs/html/interfaces/IWalletData.html +343 -0
- package/docs/html/types/ElectrumConnectionPubSub.html +233 -0
- package/docs/html/types/ElectrumConnectionSubscription.html +204 -0
- package/docs/html/types/InputData.html +203 -0
- package/docs/html/types/ObjectKeys.html +198 -0
- package/docs/html/types/Result.html +201 -0
- package/docs/html/types/TAddressIndexInfo.html +204 -0
- package/docs/html/types/TAddressLabel.html +193 -0
- package/docs/html/types/TAddressType.html +193 -0
- package/docs/html/types/TAddressTypeContent.html +198 -0
- package/docs/html/types/TAddressTypes.html +193 -0
- package/docs/html/types/TAvailableNetworks.html +193 -0
- package/docs/html/types/TConnectToElectrumRes.html +193 -0
- package/docs/html/types/TDecodeRawTx.html +214 -0
- package/docs/html/types/TElectrumNetworks.html +193 -0
- package/docs/html/types/TGetAddressHistory.html +200 -0
- package/docs/html/types/TGetByteCountInput.html +193 -0
- package/docs/html/types/TGetByteCountInputs.html +193 -0
- package/docs/html/types/TGetByteCountOutput.html +193 -0
- package/docs/html/types/TGetByteCountOutputs.html +193 -0
- package/docs/html/types/TGetData.html +211 -0
- package/docs/html/types/TGetTotalFeeObj.html +204 -0
- package/docs/html/types/TKeyDerivationAccount.html +193 -0
- package/docs/html/types/TKeyDerivationChange.html +193 -0
- package/docs/html/types/TKeyDerivationCoinType.html +193 -0
- package/docs/html/types/TKeyDerivationIndex.html +193 -0
- package/docs/html/types/TKeyDerivationPurpose.html +193 -0
- package/docs/html/types/TMessageDataMap.html +210 -0
- package/docs/html/types/TMessageKeys.html +193 -0
- package/docs/html/types/TOnMessage.html +213 -0
- package/docs/html/types/TProcessUnconfirmedTransactions.html +202 -0
- package/docs/html/types/TProtocol.html +193 -0
- package/docs/html/types/TServer.html +204 -0
- package/docs/html/types/TSetData.html +213 -0
- package/docs/html/types/TSetupTransactionResponse.html +193 -0
- package/docs/html/types/TStorage.html +200 -0
- package/docs/html/types/TSubscribedReceive.html +193 -0
- package/docs/html/types/TTransactionMessage.html +198 -0
- package/docs/html/types/TTxResponse.html +206 -0
- package/docs/html/types/TTxResult.html +200 -0
- package/docs/html/types/TUnspentAddressScriptHashData.html +198 -0
- package/docs/html/types/TWalletDataKeys.html +193 -0
- package/docs/html/variables/defaultElectrumPorts.html +193 -0
- package/docs/html/variables/electrumConnection.html +199 -0
- package/docs/html/variables/mostUsedExchangeTickers.html +241 -0
- package/docs/markdown/.nojekyll +1 -0
- package/docs/markdown/README.md +1875 -0
- package/docs/markdown/classes/Electrum.md +718 -0
- package/docs/markdown/classes/Transaction.md +536 -0
- package/docs/markdown/classes/Wallet.md +2189 -0
- package/docs/markdown/enums/EAddressType.md +41 -0
- package/docs/markdown/enums/EAvailableNetworks.md +85 -0
- package/docs/markdown/enums/EBoostType.md +30 -0
- package/docs/markdown/enums/EFeeId.md +63 -0
- package/docs/markdown/enums/EPaymentType.md +30 -0
- package/docs/markdown/enums/EProtocol.md +30 -0
- package/docs/markdown/enums/EUnit.md +41 -0
- package/docs/markdown/interfaces/IAddInput.md +41 -0
- package/docs/markdown/interfaces/IAddress.md +69 -0
- package/docs/markdown/interfaces/IAddressData.md +41 -0
- package/docs/markdown/interfaces/IAddressType.md +7 -0
- package/docs/markdown/interfaces/IAddressTypeData.md +74 -0
- package/docs/markdown/interfaces/IAddresses.md +7 -0
- package/docs/markdown/interfaces/IBoostedTransaction.md +52 -0
- package/docs/markdown/interfaces/IBoostedTransactions.md +7 -0
- package/docs/markdown/interfaces/ICreateTransaction.md +30 -0
- package/docs/markdown/interfaces/ICustomGetAddress.md +41 -0
- package/docs/markdown/interfaces/ICustomGetScriptHash.md +30 -0
- package/docs/markdown/interfaces/IElectrumGetAddressBalanceRes.md +55 -0
- package/docs/markdown/interfaces/IExchangeRates.md +7 -0
- package/docs/markdown/interfaces/IFormattedPeerData.md +63 -0
- package/docs/markdown/interfaces/IFormattedTransaction.md +217 -0
- package/docs/markdown/interfaces/IFormattedTransactions.md +7 -0
- package/docs/markdown/interfaces/IGenerateAddresses.md +74 -0
- package/docs/markdown/interfaces/IGenerateAddressesResponse.md +30 -0
- package/docs/markdown/interfaces/IGetAddress.md +41 -0
- package/docs/markdown/interfaces/IGetAddressBalanceRes.md +36 -0
- package/docs/markdown/interfaces/IGetAddressByPath.md +30 -0
- package/docs/markdown/interfaces/IGetAddressHistoryResponse.md +121 -0
- package/docs/markdown/interfaces/IGetAddressResponse.md +41 -0
- package/docs/markdown/interfaces/IGetAddressScriptHashesHistoryResponse.md +63 -0
- package/docs/markdown/interfaces/IGetDerivationPath.md +100 -0
- package/docs/markdown/interfaces/IGetFeeEstimatesResponse.md +52 -0
- package/docs/markdown/interfaces/IGetHeaderResponse.md +63 -0
- package/docs/markdown/interfaces/IGetNextAvailableAddressResponse.md +52 -0
- package/docs/markdown/interfaces/IGetTransactions.md +63 -0
- package/docs/markdown/interfaces/IGetTransactionsFromInputs.md +63 -0
- package/docs/markdown/interfaces/IGetUtxosResponse.md +30 -0
- package/docs/markdown/interfaces/IHeader.md +41 -0
- package/docs/markdown/interfaces/IIndexes.md +52 -0
- package/docs/markdown/interfaces/IKeyDerivationPath.md +69 -0
- package/docs/markdown/interfaces/IKeyDerivationPathData.md +30 -0
- package/docs/markdown/interfaces/INewBlock.md +30 -0
- package/docs/markdown/interfaces/IOnchainFees.md +63 -0
- package/docs/markdown/interfaces/IOutput.md +41 -0
- package/docs/markdown/interfaces/IPeerData.md +41 -0
- package/docs/markdown/interfaces/IRbfData.md +74 -0
- package/docs/markdown/interfaces/ISend.md +30 -0
- package/docs/markdown/interfaces/ISendTransaction.md +184 -0
- package/docs/markdown/interfaces/ISendTx.md +41 -0
- package/docs/markdown/interfaces/ISetupTransaction.md +52 -0
- package/docs/markdown/interfaces/ISubscribeToAddress.md +60 -0
- package/docs/markdown/interfaces/ISubscribeToHeader.md +59 -0
- package/docs/markdown/interfaces/ITargets.md +52 -0
- package/docs/markdown/interfaces/ITransaction.md +88 -0
- package/docs/markdown/interfaces/ITxHash.md +19 -0
- package/docs/markdown/interfaces/ITxHashes.md +55 -0
- package/docs/markdown/interfaces/IUtxo.md +112 -0
- package/docs/markdown/interfaces/IVin.md +70 -0
- package/docs/markdown/interfaces/IVout.md +52 -0
- package/docs/markdown/interfaces/IWallet.md +209 -0
- package/docs/markdown/interfaces/IWalletData.md +217 -0
- package/example/helpers.ts +76 -0
- package/example/index.ts +77 -0
- package/package.json +69 -0
- package/src/electrum/constants.ts +28 -0
- package/src/electrum/index.ts +838 -0
- package/src/index.ts +6 -0
- package/src/shapes/electrum.ts +13 -0
- package/src/shapes/index.ts +2 -0
- package/src/shapes/wallet.ts +133 -0
- package/src/transaction/index.ts +1078 -0
- package/src/types/electrum.ts +139 -0
- package/src/types/index.ts +3 -0
- package/src/types/transaction.ts +58 -0
- package/src/types/wallet.ts +503 -0
- package/src/utils/conversion.ts +88 -0
- package/src/utils/derivation-path.ts +171 -0
- package/src/utils/electrum.ts +188 -0
- package/src/utils/exchange-rate.ts +70 -0
- package/src/utils/helpers.ts +191 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/result.ts +79 -0
- package/src/utils/transaction.ts +417 -0
- package/src/utils/wallet.ts +351 -0
- package/src/wallet/constants.ts +24 -0
- package/src/wallet/index.ts +3304 -0
- package/tests/constants.ts +9 -0
- package/tests/derivation.test.ts +146 -0
- package/tests/electrum.test.ts +137 -0
- package/tests/storage.test.ts +222 -0
- package/tests/transaction.test.ts +187 -0
- package/tests/wallet.test.ts +307 -0
- package/tsconfig.json +32 -0
- package/typedoc.json +5 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { defaultWalletData } from '../shapes';
|
|
2
|
+
import cloneDeep from 'lodash.clonedeep';
|
|
3
|
+
import {
|
|
4
|
+
EAddressType,
|
|
5
|
+
EAvailableNetworks,
|
|
6
|
+
IAddress,
|
|
7
|
+
IAddresses,
|
|
8
|
+
IIndexes,
|
|
9
|
+
IKeyDerivationPath,
|
|
10
|
+
IKeyDerivationPathData,
|
|
11
|
+
ITxHashes,
|
|
12
|
+
IWalletData,
|
|
13
|
+
ObjectKeys,
|
|
14
|
+
TGetData,
|
|
15
|
+
TKeyDerivationPurpose
|
|
16
|
+
} from '../types';
|
|
17
|
+
import { err, ok, Result } from './result';
|
|
18
|
+
import {
|
|
19
|
+
getKeyDerivationPathObject,
|
|
20
|
+
getKeyDerivationPathString
|
|
21
|
+
} from './derivation-path';
|
|
22
|
+
import { bech32m } from 'bech32';
|
|
23
|
+
import * as bip39 from 'bip39';
|
|
24
|
+
import * as bitcoin from 'bitcoinjs-lib';
|
|
25
|
+
import {
|
|
26
|
+
WALLET_ID_PREFIX,
|
|
27
|
+
BITKIT_WALLET_SEED_HASH_PREFIX
|
|
28
|
+
} from '../wallet/constants';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the default wallet data object.
|
|
32
|
+
* @returns {IWalletData}
|
|
33
|
+
*/
|
|
34
|
+
export const getDefaultWalletData = (): IWalletData => {
|
|
35
|
+
return cloneDeep(defaultWalletData);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the keys from the default wallet data object.
|
|
40
|
+
* @returns {(keyof IWalletData)[]}
|
|
41
|
+
*/
|
|
42
|
+
export const getDefaultWalletDataKeys = (): (keyof IWalletData)[] => {
|
|
43
|
+
return Object.keys(getDefaultWalletData()) as (keyof IWalletData)[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns last value between hyphens in a string.
|
|
48
|
+
* @param {string} key
|
|
49
|
+
* @returns {string}
|
|
50
|
+
*/
|
|
51
|
+
export const getKeyValue = (key: string): string => {
|
|
52
|
+
const parts = key.split('-');
|
|
53
|
+
return parts[parts.length - 1];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns the keys of a given object.
|
|
58
|
+
* @type {<Type extends object>(value: Type) => Array<ObjectKeys<Type>>}
|
|
59
|
+
* @param {Type} value
|
|
60
|
+
* @returns {Array<ObjectKeys<Type>>}
|
|
61
|
+
*/
|
|
62
|
+
export const objectKeys = Object.keys as <Type extends object>(
|
|
63
|
+
value: Type
|
|
64
|
+
) => Array<ObjectKeys<Type>>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Formats and returns the provided derivation path string and object.
|
|
68
|
+
* @param {IKeyDerivationPath} path
|
|
69
|
+
* @param {TKeyDerivationPurpose | string} [purpose]
|
|
70
|
+
* @param {boolean} [changeAddress]
|
|
71
|
+
* @param {string} [addressIndex]
|
|
72
|
+
* @param {EAvailableNetworks} [network]
|
|
73
|
+
* @returns {Result<{IKeyDerivationPathData}>} Derivation Path Data
|
|
74
|
+
*/
|
|
75
|
+
export const formatKeyDerivationPath = ({
|
|
76
|
+
path,
|
|
77
|
+
purpose,
|
|
78
|
+
network,
|
|
79
|
+
changeAddress = false,
|
|
80
|
+
index = '0',
|
|
81
|
+
addressType = EAddressType.p2wpkh
|
|
82
|
+
}: {
|
|
83
|
+
path: IKeyDerivationPath | string;
|
|
84
|
+
purpose?: TKeyDerivationPurpose;
|
|
85
|
+
network: EAvailableNetworks;
|
|
86
|
+
changeAddress?: boolean;
|
|
87
|
+
index?: string;
|
|
88
|
+
addressType?: EAddressType;
|
|
89
|
+
}): Result<IKeyDerivationPathData> => {
|
|
90
|
+
try {
|
|
91
|
+
if (typeof path === 'string') {
|
|
92
|
+
const derivationPathResponse = getKeyDerivationPathObject({
|
|
93
|
+
path,
|
|
94
|
+
purpose,
|
|
95
|
+
network,
|
|
96
|
+
changeAddress,
|
|
97
|
+
index
|
|
98
|
+
});
|
|
99
|
+
if (derivationPathResponse.isErr()) {
|
|
100
|
+
return err(derivationPathResponse.error.message);
|
|
101
|
+
}
|
|
102
|
+
path = derivationPathResponse.value;
|
|
103
|
+
}
|
|
104
|
+
const pathObject = path;
|
|
105
|
+
|
|
106
|
+
const pathStringResponse = getKeyDerivationPathString({
|
|
107
|
+
addressType,
|
|
108
|
+
path: pathObject,
|
|
109
|
+
purpose,
|
|
110
|
+
network,
|
|
111
|
+
changeAddress,
|
|
112
|
+
index
|
|
113
|
+
});
|
|
114
|
+
if (pathStringResponse.isErr()) {
|
|
115
|
+
return err(pathStringResponse.error.message);
|
|
116
|
+
}
|
|
117
|
+
const pathString = pathStringResponse.value;
|
|
118
|
+
return ok({ pathObject, pathString });
|
|
119
|
+
} catch (e) {
|
|
120
|
+
return err(e);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Returns the highest used index from the provided txHashes.
|
|
126
|
+
* @param {ITxHashes[]} txHashes
|
|
127
|
+
* @param {IAddresses} addresses
|
|
128
|
+
* @param {IAddresses} changeAddresses
|
|
129
|
+
* @param {IAddress} addressIndex
|
|
130
|
+
* @param {IAddress} changeAddressIndex
|
|
131
|
+
* @returns {Result<IIndexes>}
|
|
132
|
+
*/
|
|
133
|
+
export const getHighestUsedIndexFromTxHashes = ({
|
|
134
|
+
txHashes,
|
|
135
|
+
addresses,
|
|
136
|
+
changeAddresses,
|
|
137
|
+
addressIndex,
|
|
138
|
+
changeAddressIndex
|
|
139
|
+
}: {
|
|
140
|
+
txHashes: ITxHashes[];
|
|
141
|
+
addresses: IAddresses;
|
|
142
|
+
changeAddresses: IAddresses;
|
|
143
|
+
addressIndex: IAddress;
|
|
144
|
+
changeAddressIndex: IAddress;
|
|
145
|
+
}): Result<IIndexes> => {
|
|
146
|
+
try {
|
|
147
|
+
let foundAddressIndex = false;
|
|
148
|
+
let foundChangeAddressIndex = false;
|
|
149
|
+
|
|
150
|
+
txHashes = txHashes.flat();
|
|
151
|
+
txHashes.forEach(({ scriptHash }) => {
|
|
152
|
+
if (
|
|
153
|
+
scriptHash in addresses &&
|
|
154
|
+
addresses[scriptHash].index >= addressIndex.index
|
|
155
|
+
) {
|
|
156
|
+
foundAddressIndex = true;
|
|
157
|
+
addressIndex = addresses[scriptHash];
|
|
158
|
+
} else if (
|
|
159
|
+
scriptHash in changeAddresses &&
|
|
160
|
+
changeAddresses[scriptHash].index >= changeAddressIndex.index
|
|
161
|
+
) {
|
|
162
|
+
foundChangeAddressIndex = true;
|
|
163
|
+
changeAddressIndex = changeAddresses[scriptHash];
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const data = {
|
|
168
|
+
addressIndex,
|
|
169
|
+
changeAddressIndex,
|
|
170
|
+
foundAddressIndex,
|
|
171
|
+
foundChangeAddressIndex
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return ok(data);
|
|
175
|
+
} catch (e) {
|
|
176
|
+
return err(e);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns if the provided string is a valid Bech32m encoded string (taproot/p2tr address).
|
|
182
|
+
* @param {string} address
|
|
183
|
+
* @returns { isValid: boolean; network: EAvailableNetworks }
|
|
184
|
+
*/
|
|
185
|
+
export const isValidBech32mEncodedString = (
|
|
186
|
+
address: string
|
|
187
|
+
): { isValid: boolean; network: EAvailableNetworks } => {
|
|
188
|
+
try {
|
|
189
|
+
const decoded = bech32m.decode(address);
|
|
190
|
+
if (decoded.prefix === 'bc') {
|
|
191
|
+
return { isValid: true, network: EAvailableNetworks.bitcoin };
|
|
192
|
+
} else if (decoded.prefix === 'tb') {
|
|
193
|
+
return { isValid: true, network: EAvailableNetworks.bitcoinTestnet };
|
|
194
|
+
} else if (decoded.prefix === 'bcrt') {
|
|
195
|
+
return { isValid: true, network: EAvailableNetworks.bitcoinRegtest };
|
|
196
|
+
}
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return { isValid: false, network: EAvailableNetworks.bitcoin };
|
|
199
|
+
}
|
|
200
|
+
return { isValid: false, network: EAvailableNetworks.bitcoin };
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Returns an array of all available networks from the networks object.
|
|
205
|
+
* @returns {EAvailableNetworks[]}
|
|
206
|
+
*/
|
|
207
|
+
export const availableNetworks = (): EAvailableNetworks[] =>
|
|
208
|
+
Object.values(EAvailableNetworks);
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Sum a specific value in an array of objects.
|
|
212
|
+
* @param {T[]} arr
|
|
213
|
+
* @param {keyof T} value
|
|
214
|
+
* @returns {Result<number>}
|
|
215
|
+
*/
|
|
216
|
+
export const reduceValue = <T>({
|
|
217
|
+
arr,
|
|
218
|
+
value
|
|
219
|
+
}: {
|
|
220
|
+
arr: T[];
|
|
221
|
+
value: keyof T;
|
|
222
|
+
}): Result<number> => {
|
|
223
|
+
try {
|
|
224
|
+
if (!value) {
|
|
225
|
+
return err('No value specified.');
|
|
226
|
+
}
|
|
227
|
+
return ok(
|
|
228
|
+
arr.reduce((acc, cur) => {
|
|
229
|
+
return acc + Number(cur[value]);
|
|
230
|
+
}, 0) || 0
|
|
231
|
+
);
|
|
232
|
+
} catch (e) {
|
|
233
|
+
return err(e);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Shuffles a given array.
|
|
239
|
+
* @param {T[]} array
|
|
240
|
+
* @returns {T[]}
|
|
241
|
+
*/
|
|
242
|
+
export const shuffleArray = <T>(array: T[]): T[] => {
|
|
243
|
+
const newArray = [...array];
|
|
244
|
+
for (let i = newArray.length - 1; i > 0; i--) {
|
|
245
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
246
|
+
[newArray[i], newArray[j]] = [newArray[j], newArray[i]];
|
|
247
|
+
}
|
|
248
|
+
return newArray;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export const getDataFallback: TGetData = async <K extends keyof IWalletData>(
|
|
252
|
+
key: string
|
|
253
|
+
): Promise<Result<IWalletData[K]>> => {
|
|
254
|
+
try {
|
|
255
|
+
return ok(cloneDeep(defaultWalletData[getKeyValue(key)]));
|
|
256
|
+
} catch (e) {
|
|
257
|
+
return ok(getDefaultWalletData()[key]);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
//Returns an array of messages from an OP_RETURN message
|
|
262
|
+
export const decodeOpReturnMessage = (opReturn = ''): string[] => {
|
|
263
|
+
const messages: string[] = [];
|
|
264
|
+
try {
|
|
265
|
+
//Remove OP_RETURN from the string & trim the string.
|
|
266
|
+
if (opReturn.includes('OP_RETURN')) {
|
|
267
|
+
opReturn = opReturn.replace('OP_RETURN', '');
|
|
268
|
+
opReturn = opReturn.trim();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const regex = /[0-9A-Fa-f]{6}/g;
|
|
272
|
+
//Separate the string into an array based upon a space and insert each message into an array to be returned
|
|
273
|
+
const data = opReturn.split(' ');
|
|
274
|
+
data.forEach((msg) => {
|
|
275
|
+
try {
|
|
276
|
+
//Ensure the message is in fact a hex
|
|
277
|
+
if (regex.test(msg)) {
|
|
278
|
+
const message = Buffer.from(msg, 'hex').toString();
|
|
279
|
+
messages.push(message);
|
|
280
|
+
}
|
|
281
|
+
} catch {}
|
|
282
|
+
});
|
|
283
|
+
return messages;
|
|
284
|
+
} catch {
|
|
285
|
+
return messages;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Returns the seed for a given mnemonic and passphrase.
|
|
291
|
+
* @param {string} mnemonic
|
|
292
|
+
* @param {string} bip39Passphrase
|
|
293
|
+
* @returns {Buffer}
|
|
294
|
+
*/
|
|
295
|
+
export const getSeed = (mnemonic, bip39Passphrase): Buffer => {
|
|
296
|
+
return bip39.mnemonicToSeedSync(mnemonic, bip39Passphrase);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Returns the seed hash for a given mnemonic and passphrase.
|
|
301
|
+
* @param {Buffer} seed
|
|
302
|
+
* @returns {string}
|
|
303
|
+
*/
|
|
304
|
+
export const getSeedHash = (seed: Buffer): string => {
|
|
305
|
+
return bitcoin.crypto
|
|
306
|
+
.sha256(Buffer.concat([BITKIT_WALLET_SEED_HASH_PREFIX, seed]))
|
|
307
|
+
.toString('hex');
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
export const generateWalletId = (seed: Buffer): string => {
|
|
311
|
+
return bitcoin.crypto
|
|
312
|
+
.sha256(Buffer.concat([WALLET_ID_PREFIX, seed]))
|
|
313
|
+
.toString('hex');
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export const getWalletDataStorageKey = (
|
|
317
|
+
name: string,
|
|
318
|
+
network: EAvailableNetworks,
|
|
319
|
+
key: keyof IWalletData
|
|
320
|
+
): string => {
|
|
321
|
+
return `${name}-${network}-${key}`;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
export const getStorageKeyValues = (
|
|
325
|
+
storageKey: string
|
|
326
|
+
): {
|
|
327
|
+
walletName: string;
|
|
328
|
+
network: EAvailableNetworks;
|
|
329
|
+
value: keyof IWalletData;
|
|
330
|
+
} => {
|
|
331
|
+
const [walletName, network, value] = storageKey.split('-');
|
|
332
|
+
return {
|
|
333
|
+
walletName,
|
|
334
|
+
network: network as EAvailableNetworks,
|
|
335
|
+
value: value as keyof IWalletData
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Returns the total fee in sats for a transaction at a given size (transactionByteCount) times the desired satsPerByte.
|
|
341
|
+
* @param {number} satsPerByte
|
|
342
|
+
* @param {number} transactionByteCount
|
|
343
|
+
* @returns {number}
|
|
344
|
+
*/
|
|
345
|
+
export const getTxFee = ({
|
|
346
|
+
satsPerByte,
|
|
347
|
+
transactionByteCount
|
|
348
|
+
}: {
|
|
349
|
+
satsPerByte: number;
|
|
350
|
+
transactionByteCount: number;
|
|
351
|
+
}): number => transactionByteCount * satsPerByte;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const BITKIT_WALLET_SEED_HASH_PREFIX = Buffer.from(
|
|
2
|
+
'@Bitkit/wallet-uuid'
|
|
3
|
+
);
|
|
4
|
+
|
|
5
|
+
export const WALLET_ID_PREFIX = Buffer.from('@Beignet/wallet-id');
|
|
6
|
+
|
|
7
|
+
//How many addresses to generate when more are needed.
|
|
8
|
+
export const GENERATE_ADDRESS_AMOUNT = 5;
|
|
9
|
+
|
|
10
|
+
// TODO: Add this as a settings for users to adjust when needed.
|
|
11
|
+
export const GAP_LIMIT = 20;
|
|
12
|
+
|
|
13
|
+
// TODO: remove chunk logic and move it to rn-electrum library
|
|
14
|
+
export const CHUNK_LIMIT = 15;
|
|
15
|
+
|
|
16
|
+
//How much of the users funds we allow to be used for Lightning.
|
|
17
|
+
export const SPENDING_LIMIT_RATIO = 0.8;
|
|
18
|
+
|
|
19
|
+
export const TRANSACTION_DEFAULTS = {
|
|
20
|
+
recommendedBaseFee: 256, // Total recommended tx base fee in sats
|
|
21
|
+
dustLimit: 546 // Minimum value in sats for an output. Outputs below the dust limit may not be processed because the fees required to include them in a block would be greater than the value of the transaction itself.
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const BLOCKTANK_HOST = 'https://api.stag.blocktank.to';
|