@ledgerhq/wallet-btc 0.1.1-nightly.20260721152948
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/.oxlintrc.json +7 -0
- package/.unimportedrc.json +9 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE.txt +21 -0
- package/README.md +82 -0
- package/jest.config.js +32 -0
- package/jest.integ.config.js +22 -0
- package/lib/account.d.ts +27 -0
- package/lib/account.d.ts.map +1 -0
- package/lib/account.js +3 -0
- package/lib/account.js.map +1 -0
- package/lib/crypto/base.d.ts +34 -0
- package/lib/crypto/base.d.ts.map +1 -0
- package/lib/crypto/base.js +186 -0
- package/lib/crypto/base.js.map +1 -0
- package/lib/crypto/bech32m.d.ts +16 -0
- package/lib/crypto/bech32m.d.ts.map +1 -0
- package/lib/crypto/bech32m.js +166 -0
- package/lib/crypto/bech32m.js.map +1 -0
- package/lib/crypto/bip32.d.ts +12 -0
- package/lib/crypto/bip32.d.ts.map +1 -0
- package/lib/crypto/bip32.js +37 -0
- package/lib/crypto/bip32.js.map +1 -0
- package/lib/crypto/bitcoin.d.ts +13 -0
- package/lib/crypto/bitcoin.d.ts.map +1 -0
- package/lib/crypto/bitcoin.js +218 -0
- package/lib/crypto/bitcoin.js.map +1 -0
- package/lib/crypto/bitcoincash.d.ts +13 -0
- package/lib/crypto/bitcoincash.d.ts.map +1 -0
- package/lib/crypto/bitcoincash.js +38 -0
- package/lib/crypto/bitcoincash.js.map +1 -0
- package/lib/crypto/bitcoingold.d.ts +9 -0
- package/lib/crypto/bitcoingold.d.ts.map +1 -0
- package/lib/crypto/bitcoingold.js +16 -0
- package/lib/crypto/bitcoingold.js.map +1 -0
- package/lib/crypto/dash.d.ts +9 -0
- package/lib/crypto/dash.d.ts.map +1 -0
- package/lib/crypto/dash.js +24 -0
- package/lib/crypto/dash.js.map +1 -0
- package/lib/crypto/decred.d.ts +16 -0
- package/lib/crypto/decred.d.ts.map +1 -0
- package/lib/crypto/decred.js +116 -0
- package/lib/crypto/decred.js.map +1 -0
- package/lib/crypto/digibyte.d.ts +10 -0
- package/lib/crypto/digibyte.d.ts.map +1 -0
- package/lib/crypto/digibyte.js +67 -0
- package/lib/crypto/digibyte.js.map +1 -0
- package/lib/crypto/doge.d.ts +9 -0
- package/lib/crypto/doge.d.ts.map +1 -0
- package/lib/crypto/doge.js +18 -0
- package/lib/crypto/doge.js.map +1 -0
- package/lib/crypto/factory.d.ts +3 -0
- package/lib/crypto/factory.d.ts.map +1 -0
- package/lib/crypto/factory.js +122 -0
- package/lib/crypto/factory.js.map +1 -0
- package/lib/crypto/index.d.ts +14 -0
- package/lib/crypto/index.d.ts.map +1 -0
- package/lib/crypto/index.js +34 -0
- package/lib/crypto/index.js.map +1 -0
- package/lib/crypto/komodo.d.ts +9 -0
- package/lib/crypto/komodo.d.ts.map +1 -0
- package/lib/crypto/komodo.js +19 -0
- package/lib/crypto/komodo.js.map +1 -0
- package/lib/crypto/litecoin.d.ts +11 -0
- package/lib/crypto/litecoin.d.ts.map +1 -0
- package/lib/crypto/litecoin.js +65 -0
- package/lib/crypto/litecoin.js.map +1 -0
- package/lib/crypto/qtum.d.ts +9 -0
- package/lib/crypto/qtum.d.ts.map +1 -0
- package/lib/crypto/qtum.js +16 -0
- package/lib/crypto/qtum.js.map +1 -0
- package/lib/crypto/secp256k1.d.ts +16 -0
- package/lib/crypto/secp256k1.d.ts.map +1 -0
- package/lib/crypto/secp256k1.js +39 -0
- package/lib/crypto/secp256k1.js.map +1 -0
- package/lib/crypto/types.d.ts +21 -0
- package/lib/crypto/types.d.ts.map +1 -0
- package/lib/crypto/types.js +3 -0
- package/lib/crypto/types.js.map +1 -0
- package/lib/crypto/zec.d.ts +13 -0
- package/lib/crypto/zec.d.ts.map +1 -0
- package/lib/crypto/zec.js +98 -0
- package/lib/crypto/zec.js.map +1 -0
- package/lib/crypto/zen.d.ts +13 -0
- package/lib/crypto/zen.d.ts.map +1 -0
- package/lib/crypto/zen.js +112 -0
- package/lib/crypto/zen.js.map +1 -0
- package/lib/errors.d.ts +10 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +10 -0
- package/lib/errors.js.map +1 -0
- package/lib/explorer/baseUrl.d.ts +12 -0
- package/lib/explorer/baseUrl.d.ts.map +1 -0
- package/lib/explorer/baseUrl.js +19 -0
- package/lib/explorer/baseUrl.js.map +1 -0
- package/lib/explorer/index.d.ts +64 -0
- package/lib/explorer/index.d.ts.map +1 -0
- package/lib/explorer/index.js +213 -0
- package/lib/explorer/index.js.map +1 -0
- package/lib/explorer/types.d.ts +38 -0
- package/lib/explorer/types.d.ts.map +1 -0
- package/lib/explorer/types.js +3 -0
- package/lib/explorer/types.js.map +1 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -0
- package/lib/operations.d.ts +44 -0
- package/lib/operations.d.ts.map +1 -0
- package/lib/operations.js +134 -0
- package/lib/operations.js.map +1 -0
- package/lib/pickingstrategies/CoinSelect.d.ts +18 -0
- package/lib/pickingstrategies/CoinSelect.d.ts.map +1 -0
- package/lib/pickingstrategies/CoinSelect.js +190 -0
- package/lib/pickingstrategies/CoinSelect.js.map +1 -0
- package/lib/pickingstrategies/Custom.d.ts +8 -0
- package/lib/pickingstrategies/Custom.d.ts.map +1 -0
- package/lib/pickingstrategies/Custom.js +12 -0
- package/lib/pickingstrategies/Custom.js.map +1 -0
- package/lib/pickingstrategies/DeepFirst.d.ts +17 -0
- package/lib/pickingstrategies/DeepFirst.d.ts.map +1 -0
- package/lib/pickingstrategies/DeepFirst.js +96 -0
- package/lib/pickingstrategies/DeepFirst.js.map +1 -0
- package/lib/pickingstrategies/Merge.d.ts +17 -0
- package/lib/pickingstrategies/Merge.d.ts.map +1 -0
- package/lib/pickingstrategies/Merge.js +96 -0
- package/lib/pickingstrategies/Merge.js.map +1 -0
- package/lib/pickingstrategies/types.d.ts +27 -0
- package/lib/pickingstrategies/types.d.ts.map +1 -0
- package/lib/pickingstrategies/types.js +16 -0
- package/lib/pickingstrategies/types.js.map +1 -0
- package/lib/storage/index.d.ts +79 -0
- package/lib/storage/index.d.ts.map +1 -0
- package/lib/storage/index.js +258 -0
- package/lib/storage/index.js.map +1 -0
- package/lib/storage/types.d.ts +76 -0
- package/lib/storage/types.d.ts.map +1 -0
- package/lib/storage/types.js +3 -0
- package/lib/storage/types.js.map +1 -0
- package/lib/types.d.ts +25 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +11 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.d.ts +84 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +388 -0
- package/lib/utils.js.map +1 -0
- package/lib/wallet.d.ts +50 -0
- package/lib/wallet.d.ts.map +1 -0
- package/lib/wallet.js +233 -0
- package/lib/wallet.js.map +1 -0
- package/lib/xpub.d.ts +67 -0
- package/lib/xpub.d.ts.map +1 -0
- package/lib/xpub.js +466 -0
- package/lib/xpub.js.map +1 -0
- package/lib-es/account.d.ts +27 -0
- package/lib-es/account.d.ts.map +1 -0
- package/lib-es/account.js +2 -0
- package/lib-es/account.js.map +1 -0
- package/lib-es/crypto/base.d.ts +34 -0
- package/lib-es/crypto/base.d.ts.map +1 -0
- package/lib-es/crypto/base.js +146 -0
- package/lib-es/crypto/base.js.map +1 -0
- package/lib-es/crypto/bech32m.d.ts +16 -0
- package/lib-es/crypto/bech32m.d.ts.map +1 -0
- package/lib-es/crypto/bech32m.js +163 -0
- package/lib-es/crypto/bech32m.js.map +1 -0
- package/lib-es/crypto/bip32.d.ts +12 -0
- package/lib-es/crypto/bip32.d.ts.map +1 -0
- package/lib-es/crypto/bip32.js +32 -0
- package/lib-es/crypto/bip32.js.map +1 -0
- package/lib-es/crypto/bitcoin.d.ts +13 -0
- package/lib-es/crypto/bitcoin.d.ts.map +1 -0
- package/lib-es/crypto/bitcoin.js +180 -0
- package/lib-es/crypto/bitcoin.js.map +1 -0
- package/lib-es/crypto/bitcoincash.d.ts +13 -0
- package/lib-es/crypto/bitcoincash.d.ts.map +1 -0
- package/lib-es/crypto/bitcoincash.js +33 -0
- package/lib-es/crypto/bitcoincash.js.map +1 -0
- package/lib-es/crypto/bitcoingold.d.ts +9 -0
- package/lib-es/crypto/bitcoingold.d.ts.map +1 -0
- package/lib-es/crypto/bitcoingold.js +11 -0
- package/lib-es/crypto/bitcoingold.js.map +1 -0
- package/lib-es/crypto/dash.d.ts +9 -0
- package/lib-es/crypto/dash.d.ts.map +1 -0
- package/lib-es/crypto/dash.js +19 -0
- package/lib-es/crypto/dash.js.map +1 -0
- package/lib-es/crypto/decred.d.ts +16 -0
- package/lib-es/crypto/decred.d.ts.map +1 -0
- package/lib-es/crypto/decred.js +78 -0
- package/lib-es/crypto/decred.js.map +1 -0
- package/lib-es/crypto/digibyte.d.ts +10 -0
- package/lib-es/crypto/digibyte.d.ts.map +1 -0
- package/lib-es/crypto/digibyte.js +29 -0
- package/lib-es/crypto/digibyte.js.map +1 -0
- package/lib-es/crypto/doge.d.ts +9 -0
- package/lib-es/crypto/doge.d.ts.map +1 -0
- package/lib-es/crypto/doge.js +13 -0
- package/lib-es/crypto/doge.js.map +1 -0
- package/lib-es/crypto/factory.d.ts +3 -0
- package/lib-es/crypto/factory.d.ts.map +1 -0
- package/lib-es/crypto/factory.js +83 -0
- package/lib-es/crypto/factory.js.map +1 -0
- package/lib-es/crypto/index.d.ts +14 -0
- package/lib-es/crypto/index.d.ts.map +1 -0
- package/lib-es/crypto/index.js +14 -0
- package/lib-es/crypto/index.js.map +1 -0
- package/lib-es/crypto/komodo.d.ts +9 -0
- package/lib-es/crypto/komodo.d.ts.map +1 -0
- package/lib-es/crypto/komodo.js +14 -0
- package/lib-es/crypto/komodo.js.map +1 -0
- package/lib-es/crypto/litecoin.d.ts +11 -0
- package/lib-es/crypto/litecoin.d.ts.map +1 -0
- package/lib-es/crypto/litecoin.js +27 -0
- package/lib-es/crypto/litecoin.js.map +1 -0
- package/lib-es/crypto/qtum.d.ts +9 -0
- package/lib-es/crypto/qtum.d.ts.map +1 -0
- package/lib-es/crypto/qtum.js +11 -0
- package/lib-es/crypto/qtum.js.map +1 -0
- package/lib-es/crypto/secp256k1.d.ts +16 -0
- package/lib-es/crypto/secp256k1.d.ts.map +1 -0
- package/lib-es/crypto/secp256k1.js +35 -0
- package/lib-es/crypto/secp256k1.js.map +1 -0
- package/lib-es/crypto/types.d.ts +21 -0
- package/lib-es/crypto/types.d.ts.map +1 -0
- package/lib-es/crypto/types.js +2 -0
- package/lib-es/crypto/types.js.map +1 -0
- package/lib-es/crypto/zec.d.ts +13 -0
- package/lib-es/crypto/zec.d.ts.map +1 -0
- package/lib-es/crypto/zec.js +60 -0
- package/lib-es/crypto/zec.js.map +1 -0
- package/lib-es/crypto/zen.d.ts +13 -0
- package/lib-es/crypto/zen.d.ts.map +1 -0
- package/lib-es/crypto/zen.js +74 -0
- package/lib-es/crypto/zen.js.map +1 -0
- package/lib-es/errors.d.ts +10 -0
- package/lib-es/errors.d.ts.map +1 -0
- package/lib-es/errors.js +7 -0
- package/lib-es/errors.js.map +1 -0
- package/lib-es/explorer/baseUrl.d.ts +12 -0
- package/lib-es/explorer/baseUrl.d.ts.map +1 -0
- package/lib-es/explorer/baseUrl.js +15 -0
- package/lib-es/explorer/baseUrl.js.map +1 -0
- package/lib-es/explorer/index.d.ts +64 -0
- package/lib-es/explorer/index.d.ts.map +1 -0
- package/lib-es/explorer/index.js +208 -0
- package/lib-es/explorer/index.js.map +1 -0
- package/lib-es/explorer/types.d.ts +38 -0
- package/lib-es/explorer/types.d.ts.map +1 -0
- package/lib-es/explorer/types.js +2 -0
- package/lib-es/explorer/types.js.map +1 -0
- package/lib-es/index.d.ts +14 -0
- package/lib-es/index.d.ts.map +1 -0
- package/lib-es/index.js +17 -0
- package/lib-es/index.js.map +1 -0
- package/lib-es/operations.d.ts +44 -0
- package/lib-es/operations.d.ts.map +1 -0
- package/lib-es/operations.js +129 -0
- package/lib-es/operations.js.map +1 -0
- package/lib-es/pickingstrategies/CoinSelect.d.ts +18 -0
- package/lib-es/pickingstrategies/CoinSelect.d.ts.map +1 -0
- package/lib-es/pickingstrategies/CoinSelect.js +150 -0
- package/lib-es/pickingstrategies/CoinSelect.js.map +1 -0
- package/lib-es/pickingstrategies/Custom.d.ts +8 -0
- package/lib-es/pickingstrategies/Custom.d.ts.map +1 -0
- package/lib-es/pickingstrategies/Custom.js +8 -0
- package/lib-es/pickingstrategies/Custom.js.map +1 -0
- package/lib-es/pickingstrategies/DeepFirst.d.ts +17 -0
- package/lib-es/pickingstrategies/DeepFirst.d.ts.map +1 -0
- package/lib-es/pickingstrategies/DeepFirst.js +56 -0
- package/lib-es/pickingstrategies/DeepFirst.js.map +1 -0
- package/lib-es/pickingstrategies/Merge.d.ts +17 -0
- package/lib-es/pickingstrategies/Merge.d.ts.map +1 -0
- package/lib-es/pickingstrategies/Merge.js +56 -0
- package/lib-es/pickingstrategies/Merge.js.map +1 -0
- package/lib-es/pickingstrategies/types.d.ts +27 -0
- package/lib-es/pickingstrategies/types.d.ts.map +1 -0
- package/lib-es/pickingstrategies/types.js +12 -0
- package/lib-es/pickingstrategies/types.js.map +1 -0
- package/lib-es/storage/index.d.ts +79 -0
- package/lib-es/storage/index.d.ts.map +1 -0
- package/lib-es/storage/index.js +253 -0
- package/lib-es/storage/index.js.map +1 -0
- package/lib-es/storage/types.d.ts +76 -0
- package/lib-es/storage/types.d.ts.map +1 -0
- package/lib-es/storage/types.js +2 -0
- package/lib-es/storage/types.js.map +1 -0
- package/lib-es/types.d.ts +25 -0
- package/lib-es/types.d.ts.map +1 -0
- package/lib-es/types.js +8 -0
- package/lib-es/types.js.map +1 -0
- package/lib-es/utils.d.ts +84 -0
- package/lib-es/utils.d.ts.map +1 -0
- package/lib-es/utils.js +337 -0
- package/lib-es/utils.js.map +1 -0
- package/lib-es/wallet.d.ts +50 -0
- package/lib-es/wallet.d.ts.map +1 -0
- package/lib-es/wallet.js +195 -0
- package/lib-es/wallet.js.map +1 -0
- package/lib-es/xpub.d.ts +67 -0
- package/lib-es/xpub.d.ts.map +1 -0
- package/lib-es/xpub.js +461 -0
- package/lib-es/xpub.js.map +1 -0
- package/package.json +110 -0
- package/src/__tests__/bitcoin.test.ts +301 -0
- package/src/__tests__/computeDustAmount.test.ts +102 -0
- package/src/__tests__/explorer.unit.test.ts +173 -0
- package/src/__tests__/fixtures/common.fixtures.ts +83 -0
- package/src/__tests__/operations.unit.test.ts +118 -0
- package/src/__tests__/pickingstrategies.unit.test.ts +136 -0
- package/src/__tests__/storage.unit.test.ts +113 -0
- package/src/__tests__/utils.coverage.unit.test.ts +112 -0
- package/src/__tests__/utils.test.ts +346 -0
- package/src/__tests__/wallet.errorpropagation.integ.test.ts +37 -0
- package/src/__tests__/wallet.estimateMaxSpendable.integ.test.ts +118 -0
- package/src/__tests__/wallet.explorer.integ.test.ts +61 -0
- package/src/__tests__/wallet.explorer.unit.test.ts +438 -0
- package/src/__tests__/wallet.integ.test.ts +54 -0
- package/src/__tests__/wallet.storage.unit.test.ts +707 -0
- package/src/__tests__/wallet.unit.test.ts +855 -0
- package/src/__tests__/xpub.getAddress.integ.test.ts +79 -0
- package/src/__tests__/xpub.pagination.integ.test.ts +38 -0
- package/src/__tests__/xpub.pickingStrategies.integ.test.ts +625 -0
- package/src/__tests__/xpub.reorg.integ.test.ts +134 -0
- package/src/__tests__/xpub.synced.integ.test.ts +73 -0
- package/src/__tests__/xpub.syncing.integ.test.ts +170 -0
- package/src/__tests__/xpub.txs.NP2WPKH.integ.test.ts +181 -0
- package/src/__tests__/xpub.txs.P2WPKH.integ.test.ts +173 -0
- package/src/__tests__/xpub.txs.dogecoin.integ.test.ts +74 -0
- package/src/__tests__/xpub.txs.integ.test.ts +349 -0
- package/src/__tests__/xpub.txs.litecoin.integ.test.ts +157 -0
- package/src/__tests__/xpub.txs.zcash.integ.test.ts +136 -0
- package/src/__tests__/xpub.unit.test.ts +262 -0
- package/src/__tests__/xpub.utxo.unit.test.ts +66 -0
- package/src/account.ts +30 -0
- package/src/crypto/base.ts +180 -0
- package/src/crypto/bech32m.ts +187 -0
- package/src/crypto/bip32.ts +34 -0
- package/src/crypto/bitcoin.ts +201 -0
- package/src/crypto/bitcoincash.ts +44 -0
- package/src/crypto/bitcoingold.ts +13 -0
- package/src/crypto/dash.ts +21 -0
- package/src/crypto/decred.ts +88 -0
- package/src/crypto/digibyte.ts +33 -0
- package/src/crypto/doge.ts +15 -0
- package/src/crypto/factory.ts +84 -0
- package/src/crypto/index.ts +13 -0
- package/src/crypto/komodo.ts +16 -0
- package/src/crypto/litecoin.ts +31 -0
- package/src/crypto/qtum.ts +12 -0
- package/src/crypto/secp256k1.ts +45 -0
- package/src/crypto/types.ts +45 -0
- package/src/crypto/zec.ts +70 -0
- package/src/crypto/zen.ts +85 -0
- package/src/errors.ts +9 -0
- package/src/explorer/baseUrl.ts +17 -0
- package/src/explorer/index.ts +257 -0
- package/src/explorer/types.ts +42 -0
- package/src/index.ts +41 -0
- package/src/operations.ts +174 -0
- package/src/pickingstrategies/CoinSelect.ts +197 -0
- package/src/pickingstrategies/Custom.ts +7 -0
- package/src/pickingstrategies/DeepFirst.ts +80 -0
- package/src/pickingstrategies/Merge.ts +81 -0
- package/src/pickingstrategies/types.ts +44 -0
- package/src/storage/index.ts +321 -0
- package/src/storage/types.ts +63 -0
- package/src/types.ts +28 -0
- package/src/utils.ts +415 -0
- package/src/wallet.ts +299 -0
- package/src/xpub.ts +713 -0
- package/tsconfig.build.json +24 -0
- package/tsconfig.json +14 -0
- package/types/bs58check/base.d.ts +3 -0
- package/types/coininfo/index.d.ts +96 -0
package/.oxlintrc.json
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# @ledgerhq/wallet-btc
|
|
2
|
+
|
|
3
|
+
## 0.1.1-nightly.20260721152948
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#19736](https://github.com/LedgerHQ/ledger-live/pull/19736) [`2eac6a1`](https://github.com/LedgerHQ/ledger-live/commit/2eac6a17b629be3f8ba08cdf650413083b89476e) Thanks [@cted-ledger](https://github.com/cted-ledger)! - Extract the shared UTXO engine (xpub scanning, coin-selection, storage, address crypto) into a standalone `@ledgerhq/wallet-btc` package, dependency-inverted so it no longer imports `@ledgerhq/cryptoassets` or `@ledgerhq/ledger-wallet-framework`: the currency is injected as a typed `WalletBtcCurrency`. Transaction build/sign, RBF fee computation, the device signer, and the `getWalletAccount` resolver stay in `@ledgerhq/coin-bitcoin`. Internal refactor with no behavior change; consumers (`@ledgerhq/live-common`, `ledger-live-desktop`) are rewired to the new import paths.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies []:
|
|
10
|
+
- @ledgerhq/live-network@2.6.8-nightly.20260721152948
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Ledger https://www.ledger.com/
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @ledgerhq/wallet-btc
|
|
2
|
+
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> **Status: STABLE** — Production-proven UTXO engine, previously embedded in `@ledgerhq/coin-bitcoin`; API is considered stable.
|
|
5
|
+
|
|
6
|
+
Shared UTXO wallet engine for Bitcoin and its forks (Bitcoin Cash, Litecoin, Dogecoin, transparent Zcash, …). It owns the chain-agnostic parts of a UTXO wallet:
|
|
7
|
+
|
|
8
|
+
- **xpub scanning** — address derivation and account discovery from an extended public key;
|
|
9
|
+
- **coin-selection** — pluggable picking strategies (`Merge`, `CoinSelect`, `DeepFirst`, `Custom`);
|
|
10
|
+
- **storage** — the transaction / UTXO store used during sync;
|
|
11
|
+
- **address crypto** — per-network address encoding and validation.
|
|
12
|
+
|
|
13
|
+
It deliberately does **not** craft, sign, or broadcast transactions, and it has no knowledge of the Ledger Live account model — those concerns belong to the consuming coin-module (`@ledgerhq/coin-bitcoin`).
|
|
14
|
+
|
|
15
|
+
## Dependency inversion
|
|
16
|
+
|
|
17
|
+
`wallet-btc` does not depend on `@ledgerhq/cryptoassets`, `@ledgerhq/ledger-wallet-framework`, `@ledgerhq/types-*` or `@ledgerhq/live-env`. Everything currency- and network-specific is injected by the caller through a typed descriptor:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
type WalletBtcCurrency = {
|
|
21
|
+
id: string; // e.g. "bitcoin"
|
|
22
|
+
explorerId?: string; // Ledger explorer code, e.g. "btc" (defaults to id)
|
|
23
|
+
explorerEndpoint: string; // e.g. "https://explorers.api.live.ledger.com"
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The caller resolves these values on its side (from `@ledgerhq/cryptoassets` and `@ledgerhq/live-env`) and passes them in. This keeps the engine free of the Ledger Live currency registry, so it can be reused across several coin-modules. Its only `@ledgerhq` dependencies are `coin-module-framework`, `errors`, `live-network` and `logs`.
|
|
28
|
+
|
|
29
|
+
## Main exports
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import wallet, {
|
|
33
|
+
BitcoinLikeWallet,
|
|
34
|
+
DerivationModes,
|
|
35
|
+
Merge,
|
|
36
|
+
CoinSelect,
|
|
37
|
+
DeepFirst,
|
|
38
|
+
Custom,
|
|
39
|
+
isValidAddress,
|
|
40
|
+
isTaprootAddress,
|
|
41
|
+
type Account,
|
|
42
|
+
type Currency,
|
|
43
|
+
type TransactionInfo,
|
|
44
|
+
} from "@ledgerhq/wallet-btc/index";
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- `BitcoinLikeWallet` (and the lazily-created default singleton) — generate, scan, and serialize accounts.
|
|
48
|
+
- Picking strategies — the coin-selection algorithms.
|
|
49
|
+
- `isValidAddress` / `isTaprootAddress` — address validation helpers.
|
|
50
|
+
|
|
51
|
+
Lower-level building blocks are available on subpaths: `@ledgerhq/wallet-btc/xpub`, `/explorer`, `/storage`, `/operations`, `/crypto/*`, `/pickingstrategies/*`.
|
|
52
|
+
|
|
53
|
+
`/operations` exposes the chain-agnostic UTXO accounting shared by consuming coin-modules: `removeReplaced` (RBF/pending deduplication) and `deduplicateOperations`.
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import BitcoinLikeWallet from "@ledgerhq/wallet-btc/wallet";
|
|
59
|
+
import { DerivationModes } from "@ledgerhq/wallet-btc/types";
|
|
60
|
+
|
|
61
|
+
const engine = new BitcoinLikeWallet();
|
|
62
|
+
|
|
63
|
+
const account = await engine.generateAccount(
|
|
64
|
+
{
|
|
65
|
+
xpub,
|
|
66
|
+
path: "44'/0'",
|
|
67
|
+
index: 0,
|
|
68
|
+
currency: "bitcoin",
|
|
69
|
+
network: "mainnet",
|
|
70
|
+
derivationMode: DerivationModes.LEGACY,
|
|
71
|
+
},
|
|
72
|
+
{ id: "bitcoin", explorerId: "btc", explorerEndpoint: "https://explorers.api.live.ledger.com" },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
await engine.syncAccount(account);
|
|
76
|
+
const balance = await engine.getAccountBalance(account);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Tests
|
|
80
|
+
|
|
81
|
+
- `pnpm test` — unit tests.
|
|
82
|
+
- `pnpm test-integ` — integration tests. They hit the live explorer; a few suites require Praline and are skipped by default.
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
passWithNoTests: true,
|
|
3
|
+
collectCoverageFrom: [
|
|
4
|
+
"src/**/*.ts",
|
|
5
|
+
"!src/**/*.test.ts",
|
|
6
|
+
"!src/**/*.spec.ts",
|
|
7
|
+
"!src/__tests__/**/*.ts",
|
|
8
|
+
],
|
|
9
|
+
coverageReporters: ["json", ["lcov", { file: "lcov.info", projectRoot: "../../" }], "text"],
|
|
10
|
+
testEnvironment: "node",
|
|
11
|
+
transform: {
|
|
12
|
+
"^.+\\.(ts|tsx)$": [
|
|
13
|
+
"@swc/jest",
|
|
14
|
+
{
|
|
15
|
+
jsc: {
|
|
16
|
+
target: "esnext",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
moduleNameMapper: {
|
|
22
|
+
"^@ledgerhq/wallet-btc/(.*)$": "<rootDir>/src/$1",
|
|
23
|
+
},
|
|
24
|
+
testPathIgnorePatterns: ["lib/", "lib-es/", ".*\\.(integ|integration)\\.test\\.ts"],
|
|
25
|
+
modulePathIgnorePatterns: ["__tests__/fixtures"],
|
|
26
|
+
reporters: [
|
|
27
|
+
"default",
|
|
28
|
+
...(process.env.CI ? ["github-actions"] : []),
|
|
29
|
+
["jest-sonar", { outputName: "sonar-executionTests-report.xml", reportedFilePath: "absolute" }],
|
|
30
|
+
],
|
|
31
|
+
setupFilesAfterEnv: ["@ledgerhq/disable-network-setup"],
|
|
32
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** @type {import('jest').Config} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
testEnvironment: "node",
|
|
4
|
+
testRegex: ".integ.test.ts$",
|
|
5
|
+
testPathIgnorePatterns: ["lib/", "lib-es/"],
|
|
6
|
+
testTimeout: 60_000,
|
|
7
|
+
forceExit: true,
|
|
8
|
+
passWithNoTests: true,
|
|
9
|
+
moduleNameMapper: {
|
|
10
|
+
"^@ledgerhq/wallet-btc/(.*)$": "<rootDir>/src/$1",
|
|
11
|
+
},
|
|
12
|
+
transform: {
|
|
13
|
+
"^.+\\.(t|j)sx?$": [
|
|
14
|
+
"@swc/jest",
|
|
15
|
+
{
|
|
16
|
+
jsc: {
|
|
17
|
+
target: "esnext",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
};
|
package/lib/account.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DerivationModes } from "./types";
|
|
2
|
+
import { Currency } from "./crypto/types";
|
|
3
|
+
import Xpub from "./xpub";
|
|
4
|
+
export interface Account {
|
|
5
|
+
params: {
|
|
6
|
+
path: string;
|
|
7
|
+
index: number;
|
|
8
|
+
currency: Currency;
|
|
9
|
+
network: "mainnet" | "testnet";
|
|
10
|
+
derivationMode: DerivationModes;
|
|
11
|
+
};
|
|
12
|
+
xpub: Xpub;
|
|
13
|
+
}
|
|
14
|
+
export interface SerializedAccount {
|
|
15
|
+
params: {
|
|
16
|
+
path: string;
|
|
17
|
+
index: number;
|
|
18
|
+
currency: Currency;
|
|
19
|
+
network: "mainnet" | "testnet";
|
|
20
|
+
derivationMode: DerivationModes;
|
|
21
|
+
};
|
|
22
|
+
xpub: {
|
|
23
|
+
xpub: string;
|
|
24
|
+
data: unknown;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,IAAI,MAAM,QAAQ,CAAC;AAE1B,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,QAAQ,CAAC;QACnB,OAAO,EAAE,SAAS,GAAG,SAAS,CAAC;QAC/B,cAAc,EAAE,eAAe,CAAC;KACjC,CAAC;IAEF,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,QAAQ,CAAC;QACnB,OAAO,EAAE,SAAS,GAAG,SAAS,CAAC;QAC/B,cAAc,EAAE,eAAe,CAAC;KACjC,CAAC;IAEF,IAAI,EAAE;QACJ,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,OAAO,CAAC;KACf,CAAC;CACH"}
|
package/lib/account.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { BitcoinJS } from "coininfo";
|
|
2
|
+
import { ICrypto } from "./types";
|
|
3
|
+
export declare const OP_RETURN_DATA_SIZE_LIMIT = 83;
|
|
4
|
+
export declare function fallbackValidateAddress(address: string): boolean;
|
|
5
|
+
declare class Base implements ICrypto {
|
|
6
|
+
network: BitcoinJS;
|
|
7
|
+
private static readonly bip32Cache;
|
|
8
|
+
static addressCache: Record<string, Promise<string>>;
|
|
9
|
+
constructor({ network }: {
|
|
10
|
+
network: BitcoinJS;
|
|
11
|
+
});
|
|
12
|
+
getPubkeyAt(xpub: string, account: number, index: number): Promise<Buffer>;
|
|
13
|
+
protected getLegacyAddress(xpub: string, account: number, index: number): Promise<string>;
|
|
14
|
+
private getNativeSegWitAddress;
|
|
15
|
+
private getSegWitAddress;
|
|
16
|
+
getAddress(derivationMode: string, xpub: string, account: number, index: number): Promise<string>;
|
|
17
|
+
customGetAddress(derivationMode: string, xpub: string, account: number, index: number): Promise<string>;
|
|
18
|
+
/**
|
|
19
|
+
* The output script sets the conditions under which the amount can be spent.
|
|
20
|
+
* It is a crucial part of Bitcoin's scripting system, ensuring that only the rightful owner can spend the bitcoins
|
|
21
|
+
* This function returns the output script for a given address
|
|
22
|
+
* see https://developer.bitcoin.org/devguide/transactions.html for more details
|
|
23
|
+
*/
|
|
24
|
+
toOutputScript(address: string): Buffer;
|
|
25
|
+
/**
|
|
26
|
+
* OutputScript to with OP_RETURN data
|
|
27
|
+
* see https://developer.bitcoin.org/reference/transactions.html?highlight=op_return for more details
|
|
28
|
+
*/
|
|
29
|
+
toOpReturnOutputScript(data: Buffer): Buffer;
|
|
30
|
+
validateAddress(address: string): boolean;
|
|
31
|
+
isTaprootAddress(_address: string): boolean;
|
|
32
|
+
}
|
|
33
|
+
export default Base;
|
|
34
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/crypto/base.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAMlC,eAAO,MAAM,yBAAyB,KAAK,CAAC;AAE5C,wBA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resses.ts
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.OP_RETURN_DATA_SIZE_LIMIT = void 0;
|
|
41
|
+
exports.fallbackValidateAddress = fallbackValidateAddress;
|
|
42
|
+
const bjs = __importStar(require("bitcoinjs-lib"));
|
|
43
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
44
|
+
const bs58check_1 = __importDefault(require("bs58check"));
|
|
45
|
+
const types_1 = require("../types");
|
|
46
|
+
const bs58_1 = __importDefault(require("bs58"));
|
|
47
|
+
const bech32_1 = __importDefault(require("bech32"));
|
|
48
|
+
const bip32_1 = __importDefault(require("./bip32"));
|
|
49
|
+
// https://developer.bitcoin.org/devguide/transactions.html#null-data
|
|
50
|
+
exports.OP_RETURN_DATA_SIZE_LIMIT = 83; // bytes
|
|
51
|
+
function fallbackValidateAddress(address) {
|
|
52
|
+
try {
|
|
53
|
+
bjs.address.fromBase58Check(address);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Not a valid Base58 address
|
|
57
|
+
try {
|
|
58
|
+
bjs.address.fromBech32(address);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Not a valid Bech32 address either
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
class Base {
|
|
68
|
+
network;
|
|
69
|
+
static bip32Cache = new Map(); // xpub + account + index to publicKey
|
|
70
|
+
static addressCache = {}; // derivationMode + xpub + account + index to address
|
|
71
|
+
constructor({ network }) {
|
|
72
|
+
this.network = network;
|
|
73
|
+
this.network.dustThreshold = 3000;
|
|
74
|
+
this.network.dustPolicy = "PER_KBYTE";
|
|
75
|
+
this.network.usesTimestampedTransaction = false;
|
|
76
|
+
}
|
|
77
|
+
async getPubkeyAt(xpub, account, index) {
|
|
78
|
+
// a cache is stored in Base.bip32Cache to optimize the calculation
|
|
79
|
+
// at each step, we make sure the level has been calculated and calc if necessary
|
|
80
|
+
// 0: root level
|
|
81
|
+
const keyRoot = `${this.network.name}-${xpub}`;
|
|
82
|
+
let rootLevel = Base.bip32Cache.get(keyRoot);
|
|
83
|
+
if (!rootLevel) {
|
|
84
|
+
const buffer = bs58_1.default.decode(xpub);
|
|
85
|
+
const depth = buffer[4];
|
|
86
|
+
const i = buffer.readUInt32BE(9);
|
|
87
|
+
const chainCode = buffer.slice(13, 45);
|
|
88
|
+
const publicKey = buffer.slice(45, 78);
|
|
89
|
+
rootLevel = Promise.resolve(new bip32_1.default(publicKey, chainCode, this.network, depth, i));
|
|
90
|
+
Base.bip32Cache.set(keyRoot, rootLevel);
|
|
91
|
+
}
|
|
92
|
+
// 1: account level
|
|
93
|
+
const keyAccount = `${keyRoot}-${account}`;
|
|
94
|
+
let accountLevelP = Base.bip32Cache.get(keyAccount); // it's stored as promise
|
|
95
|
+
if (!accountLevelP) {
|
|
96
|
+
accountLevelP = rootLevel.then(root => root.derive(account));
|
|
97
|
+
// accountLevelP = await rootLevel.derive(account);
|
|
98
|
+
Base.bip32Cache.set(keyAccount, accountLevelP);
|
|
99
|
+
}
|
|
100
|
+
// 2: index level
|
|
101
|
+
const keyIndex = `${keyAccount}-${index}`;
|
|
102
|
+
let indexLevelP = Base.bip32Cache.get(keyIndex); // it's stored as promise
|
|
103
|
+
if (!indexLevelP) {
|
|
104
|
+
indexLevelP = accountLevelP.then(a => a.derive(index));
|
|
105
|
+
// indexLevelP = await accountLevelP.derive(index);
|
|
106
|
+
Base.bip32Cache.set(keyIndex, indexLevelP);
|
|
107
|
+
}
|
|
108
|
+
// We can finally return the publicKey. in most case, indexLevelP will be "resolved"
|
|
109
|
+
return (await indexLevelP).publicKey;
|
|
110
|
+
}
|
|
111
|
+
// derive legacy address at account and index positions
|
|
112
|
+
async getLegacyAddress(xpub, account, index) {
|
|
113
|
+
const publicKeyBuffer = await this.getPubkeyAt(xpub, account, index);
|
|
114
|
+
const publicKeyHash160 = bjs.crypto.hash160(publicKeyBuffer);
|
|
115
|
+
return bjs.address.toBase58Check(publicKeyHash160, this.network.pubKeyHash);
|
|
116
|
+
}
|
|
117
|
+
// derive native SegWit at account and index positions
|
|
118
|
+
async getNativeSegWitAddress(xpub, account, index) {
|
|
119
|
+
const publicKeyBuffer = await this.getPubkeyAt(xpub, account, index);
|
|
120
|
+
const publicKeyHash160 = bjs.crypto.hash160(publicKeyBuffer);
|
|
121
|
+
const words = bech32_1.default.toWords(publicKeyHash160);
|
|
122
|
+
words.unshift(0x00);
|
|
123
|
+
return bech32_1.default.encode(this.network.bech32, words);
|
|
124
|
+
}
|
|
125
|
+
// derive SegWit at account and index positions
|
|
126
|
+
async getSegWitAddress(xpub, account, index) {
|
|
127
|
+
const publicKeyBuffer = await this.getPubkeyAt(xpub, account, index);
|
|
128
|
+
const redeemOutput = bjs.script.compile([0, bjs.crypto.hash160(publicKeyBuffer)]);
|
|
129
|
+
const publicKeyHash160 = bjs.crypto.hash160(redeemOutput);
|
|
130
|
+
const payload = Buffer.allocUnsafe(21);
|
|
131
|
+
payload.writeUInt8(this.network.scriptHash, 0);
|
|
132
|
+
publicKeyHash160.copy(payload, 1);
|
|
133
|
+
return bs58check_1.default.encode(payload);
|
|
134
|
+
}
|
|
135
|
+
// get address given an address type
|
|
136
|
+
async getAddress(derivationMode, xpub, account, index) {
|
|
137
|
+
const addressCacheKey = `${this.network.name}-${derivationMode}-${xpub}-${account}-${index}`;
|
|
138
|
+
if (addressCacheKey in Base.addressCache) {
|
|
139
|
+
return Base.addressCache[addressCacheKey];
|
|
140
|
+
}
|
|
141
|
+
const res = this.customGetAddress(derivationMode, xpub, account, index);
|
|
142
|
+
Base.addressCache[addressCacheKey] = res;
|
|
143
|
+
return res;
|
|
144
|
+
}
|
|
145
|
+
async customGetAddress(derivationMode, xpub, account, index) {
|
|
146
|
+
switch (derivationMode) {
|
|
147
|
+
case types_1.DerivationModes.LEGACY:
|
|
148
|
+
return await this.getLegacyAddress(xpub, account, index);
|
|
149
|
+
case types_1.DerivationModes.SEGWIT:
|
|
150
|
+
return await this.getSegWitAddress(xpub, account, index);
|
|
151
|
+
case types_1.DerivationModes.NATIVE_SEGWIT:
|
|
152
|
+
return await this.getNativeSegWitAddress(xpub, account, index);
|
|
153
|
+
default:
|
|
154
|
+
throw new Error(`Invalid derivation Mode: ${derivationMode}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The output script sets the conditions under which the amount can be spent.
|
|
159
|
+
* It is a crucial part of Bitcoin's scripting system, ensuring that only the rightful owner can spend the bitcoins
|
|
160
|
+
* This function returns the output script for a given address
|
|
161
|
+
* see https://developer.bitcoin.org/devguide/transactions.html for more details
|
|
162
|
+
*/
|
|
163
|
+
toOutputScript(address) {
|
|
164
|
+
return bitcoinjs_lib_1.address.toOutputScript(address, this.network);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* OutputScript to with OP_RETURN data
|
|
168
|
+
* see https://developer.bitcoin.org/reference/transactions.html?highlight=op_return for more details
|
|
169
|
+
*/
|
|
170
|
+
toOpReturnOutputScript(data) {
|
|
171
|
+
const script = bjs.payments.embed({ data: [data] });
|
|
172
|
+
return script.output;
|
|
173
|
+
}
|
|
174
|
+
validateAddress(address) {
|
|
175
|
+
// bs58 address
|
|
176
|
+
const res = bs58check_1.default.decodeUnsafe(address);
|
|
177
|
+
if (!res)
|
|
178
|
+
return false;
|
|
179
|
+
return (res.length > 3 && (res[0] === this.network.pubKeyHash || res[0] === this.network.scriptHash));
|
|
180
|
+
}
|
|
181
|
+
isTaprootAddress(_address) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.default = Base;
|
|
186
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/crypto/base.ts"],"names":[],"mappings":";AAAA,wFAAwF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAexF,0DAaC;AA1BD,mDAAqC;AACrC,iDAAsD;AACtD,0DAAk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: ArrayLike<number>, LIMIT?: number): string;
|
|
2
|
+
declare function decodeUnsafe(str: string, LIMIT?: number): any;
|
|
3
|
+
declare function decode(str: string, LIMIT?: number): any;
|
|
4
|
+
declare function toWords(bytes: ArrayLike<number>): number[];
|
|
5
|
+
declare function fromWordsUnsafe(words: ArrayLike<number>): number[] | undefined;
|
|
6
|
+
declare function fromWords(words: ArrayLike<number>): number[];
|
|
7
|
+
export declare const bech32m: {
|
|
8
|
+
decodeUnsafe: typeof decodeUnsafe;
|
|
9
|
+
decode: typeof decode;
|
|
10
|
+
encode: typeof encode;
|
|
11
|
+
toWords: typeof toWords;
|
|
12
|
+
fromWordsUnsafe: typeof fromWordsUnsafe;
|
|
13
|
+
fromWords: typeof fromWords;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=bech32m.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bech32m.d.ts","sourceRoot":"","sources":["../../src/crypto/bech32m.ts"],"names":[],"mappings":"AAyCA,iBAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CA8BhF;AAwCD,iBAAS,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,GAAG,CAGtD;AAED,iBAAS,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,GAAG,CAKhD;AA0CD,iBAAS,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,CAEnD;AAED,iBAAS,eAAe,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,GAAG,SAAS,CAGvE;AAED,iBAAS,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,CAKrD;AAED,eAAO,MAAM,OAAO;;;;;;;CAOnB,CAAC"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ported from https://github.com/bitcoinjs/bech32/tree/605655d6b37a782345549cd1388db688a96ad56f
|
|
3
|
+
// until we can use bech32 2.0.0
|
|
4
|
+
// We can't directly use bech32 2.0.0 because many of our dependencies are still using bech32 ^1.1.3
|
|
5
|
+
// which conflict on mobile (root cause of https://ledgerhq.atlassian.net/browse/LL-7930)
|
|
6
|
+
// FIXME Remove that file and use bech32 2.0.0 when all dependencies also uses it.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.bech32m = void 0;
|
|
9
|
+
const ENCODING_CONST = 0x2bc830a3;
|
|
10
|
+
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
|
11
|
+
const ALPHABET_MAP = {};
|
|
12
|
+
for (let z = 0; z < ALPHABET.length; z++) {
|
|
13
|
+
const x = ALPHABET.charAt(z);
|
|
14
|
+
ALPHABET_MAP[x] = z;
|
|
15
|
+
}
|
|
16
|
+
function prefixChk(prefix) {
|
|
17
|
+
let chk = 1;
|
|
18
|
+
for (let i = 0; i < prefix.length; ++i) {
|
|
19
|
+
const c = prefix.charCodeAt(i);
|
|
20
|
+
if (c < 33 || c > 126)
|
|
21
|
+
return "Invalid prefix (" + prefix + ")";
|
|
22
|
+
chk = polymodStep(chk) ^ (c >> 5);
|
|
23
|
+
}
|
|
24
|
+
chk = polymodStep(chk);
|
|
25
|
+
for (let i = 0; i < prefix.length; ++i) {
|
|
26
|
+
const v = prefix.charCodeAt(i);
|
|
27
|
+
chk = polymodStep(chk) ^ (v & 0x1f);
|
|
28
|
+
}
|
|
29
|
+
return chk;
|
|
30
|
+
}
|
|
31
|
+
function polymodStep(pre) {
|
|
32
|
+
const b = pre >> 25;
|
|
33
|
+
return (((pre & 0x1ffffff) << 5) ^
|
|
34
|
+
(-((b >> 0) & 1) & 0x3b6a57b2) ^
|
|
35
|
+
(-((b >> 1) & 1) & 0x26508e6d) ^
|
|
36
|
+
(-((b >> 2) & 1) & 0x1ea119fa) ^
|
|
37
|
+
(-((b >> 3) & 1) & 0x3d4233dd) ^
|
|
38
|
+
(-((b >> 4) & 1) & 0x2a1462b3));
|
|
39
|
+
}
|
|
40
|
+
function encode(prefix, words, LIMIT) {
|
|
41
|
+
LIMIT = LIMIT || 90;
|
|
42
|
+
if (prefix.length + 7 + words.length > LIMIT)
|
|
43
|
+
throw new TypeError("Exceeds length limit");
|
|
44
|
+
prefix = prefix.toLowerCase();
|
|
45
|
+
// determine chk mod
|
|
46
|
+
let chk = prefixChk(prefix);
|
|
47
|
+
if (typeof chk === "string")
|
|
48
|
+
throw new Error(chk);
|
|
49
|
+
let result = prefix + "1";
|
|
50
|
+
for (let i = 0; i < words.length; ++i) {
|
|
51
|
+
const x = words[i];
|
|
52
|
+
if (x >> 5 !== 0)
|
|
53
|
+
throw new Error("Non 5-bit word");
|
|
54
|
+
chk = polymodStep(chk) ^ x;
|
|
55
|
+
result += ALPHABET.charAt(x);
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < 6; ++i) {
|
|
58
|
+
chk = polymodStep(chk);
|
|
59
|
+
}
|
|
60
|
+
chk ^= ENCODING_CONST;
|
|
61
|
+
for (let i = 0; i < 6; ++i) {
|
|
62
|
+
const v = (chk >> ((5 - i) * 5)) & 0x1f;
|
|
63
|
+
result += ALPHABET.charAt(v);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
function __decode(str, LIMIT) {
|
|
68
|
+
LIMIT = LIMIT || 90;
|
|
69
|
+
if (str.length < 8)
|
|
70
|
+
return str + " too short";
|
|
71
|
+
if (str.length > LIMIT)
|
|
72
|
+
return "Exceeds length limit";
|
|
73
|
+
// don't allow mixed case
|
|
74
|
+
const lowered = str.toLowerCase();
|
|
75
|
+
const uppered = str.toUpperCase();
|
|
76
|
+
if (str !== lowered && str !== uppered)
|
|
77
|
+
return "Mixed-case string " + str;
|
|
78
|
+
str = lowered;
|
|
79
|
+
const split = str.lastIndexOf("1");
|
|
80
|
+
if (split === -1)
|
|
81
|
+
return "No separator character for " + str;
|
|
82
|
+
if (split === 0)
|
|
83
|
+
return "Missing prefix for " + str;
|
|
84
|
+
const prefix = str.slice(0, split);
|
|
85
|
+
const wordChars = str.slice(split + 1);
|
|
86
|
+
if (wordChars.length < 6)
|
|
87
|
+
return "Data too short";
|
|
88
|
+
let chk = prefixChk(prefix);
|
|
89
|
+
if (typeof chk === "string")
|
|
90
|
+
return chk;
|
|
91
|
+
const words = [];
|
|
92
|
+
for (let i = 0; i < wordChars.length; ++i) {
|
|
93
|
+
const c = wordChars.charAt(i);
|
|
94
|
+
const v = ALPHABET_MAP[c];
|
|
95
|
+
if (v === undefined)
|
|
96
|
+
return "Unknown character " + c;
|
|
97
|
+
chk = polymodStep(chk) ^ v;
|
|
98
|
+
// not in the checksum?
|
|
99
|
+
if (i + 6 >= wordChars.length)
|
|
100
|
+
continue;
|
|
101
|
+
words.push(v);
|
|
102
|
+
}
|
|
103
|
+
if (chk !== ENCODING_CONST)
|
|
104
|
+
return "Invalid checksum for " + str;
|
|
105
|
+
return { prefix, words };
|
|
106
|
+
}
|
|
107
|
+
function decodeUnsafe(str, LIMIT) {
|
|
108
|
+
const res = __decode(str, LIMIT);
|
|
109
|
+
if (typeof res === "object")
|
|
110
|
+
return res;
|
|
111
|
+
}
|
|
112
|
+
function decode(str, LIMIT) {
|
|
113
|
+
const res = __decode(str, LIMIT);
|
|
114
|
+
if (typeof res === "object")
|
|
115
|
+
return res;
|
|
116
|
+
throw new Error(res);
|
|
117
|
+
}
|
|
118
|
+
function convert(data, inBits, outBits, pad) {
|
|
119
|
+
let value = 0;
|
|
120
|
+
let bits = 0;
|
|
121
|
+
const maxV = (1 << outBits) - 1;
|
|
122
|
+
const result = [];
|
|
123
|
+
for (let i = 0; i < data.length; ++i) {
|
|
124
|
+
value = (value << inBits) | data[i];
|
|
125
|
+
bits += inBits;
|
|
126
|
+
while (bits >= outBits) {
|
|
127
|
+
bits -= outBits;
|
|
128
|
+
result.push((value >> bits) & maxV);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (pad) {
|
|
132
|
+
if (bits > 0) {
|
|
133
|
+
result.push((value << (outBits - bits)) & maxV);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
if (bits >= inBits)
|
|
138
|
+
return "Excess padding";
|
|
139
|
+
if ((value << (outBits - bits)) & maxV)
|
|
140
|
+
return "Non-zero padding";
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
function toWords(bytes) {
|
|
145
|
+
return convert(bytes, 8, 5, true);
|
|
146
|
+
}
|
|
147
|
+
function fromWordsUnsafe(words) {
|
|
148
|
+
const res = convert(words, 5, 8, false);
|
|
149
|
+
if (Array.isArray(res))
|
|
150
|
+
return res;
|
|
151
|
+
}
|
|
152
|
+
function fromWords(words) {
|
|
153
|
+
const res = convert(words, 5, 8, false);
|
|
154
|
+
if (Array.isArray(res))
|
|
155
|
+
return res;
|
|
156
|
+
throw new Error(res);
|
|
157
|
+
}
|
|
158
|
+
exports.bech32m = {
|
|
159
|
+
decodeUnsafe,
|
|
160
|
+
decode,
|
|
161
|
+
encode,
|
|
162
|
+
toWords,
|
|
163
|
+
fromWordsUnsafe,
|
|
164
|
+
fromWords,
|
|
165
|
+
};
|
|
166
|
+
//# sourceMappingURL=bech32m.js.map
|