@did-btcr2/method 0.13.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/LICENSE +373 -0
- package/README.md +7 -0
- package/dist/browser.js +2364 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2364 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/bitcoin/constants.js +20 -0
- package/dist/cjs/bitcoin/constants.js.map +1 -0
- package/dist/cjs/bitcoin/errors.js +11 -0
- package/dist/cjs/bitcoin/errors.js.map +1 -0
- package/dist/cjs/bitcoin/index.js +95 -0
- package/dist/cjs/bitcoin/index.js.map +1 -0
- package/dist/cjs/bitcoin/interface.js +2 -0
- package/dist/cjs/bitcoin/interface.js.map +1 -0
- package/dist/cjs/bitcoin/network.js +17 -0
- package/dist/cjs/bitcoin/network.js.map +1 -0
- package/dist/cjs/bitcoin/rest-client.js +289 -0
- package/dist/cjs/bitcoin/rest-client.js.map +1 -0
- package/dist/cjs/bitcoin/rpc-client.js +722 -0
- package/dist/cjs/bitcoin/rpc-client.js.map +1 -0
- package/dist/cjs/bitcoin/taproot.js +219 -0
- package/dist/cjs/bitcoin/taproot.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/coordinator.js +120 -0
- package/dist/cjs/btcr2/beacon/aggregation/coordinator.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js +24 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/base.js +37 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/base.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js +8 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js +7 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/participant.js +2 -0
- package/dist/cjs/btcr2/beacon/aggregation/participant.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js +2 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
- package/dist/cjs/btcr2/beacon/cid-aggregate.js +116 -0
- package/dist/cjs/btcr2/beacon/cid-aggregate.js.map +1 -0
- package/dist/cjs/btcr2/beacon/factory.js +30 -0
- package/dist/cjs/btcr2/beacon/factory.js.map +1 -0
- package/dist/cjs/btcr2/beacon/singleton.js +220 -0
- package/dist/cjs/btcr2/beacon/singleton.js.map +1 -0
- package/dist/cjs/btcr2/beacon/smt-aggregate.js +126 -0
- package/dist/cjs/btcr2/beacon/smt-aggregate.js.map +1 -0
- package/dist/cjs/btcr2/crud/create.js +102 -0
- package/dist/cjs/btcr2/crud/create.js.map +1 -0
- package/dist/cjs/btcr2/crud/deactivate.js +14 -0
- package/dist/cjs/btcr2/crud/deactivate.js.map +1 -0
- package/dist/cjs/btcr2/crud/read.js +686 -0
- package/dist/cjs/btcr2/crud/read.js.map +1 -0
- package/dist/cjs/btcr2/crud/update.js +195 -0
- package/dist/cjs/btcr2/crud/update.js.map +1 -0
- package/dist/cjs/btcr2/key-manager/index.js +290 -0
- package/dist/cjs/btcr2/key-manager/index.js.map +1 -0
- package/dist/cjs/btcr2/key-manager/interface.js +2 -0
- package/dist/cjs/btcr2/key-manager/interface.js.map +1 -0
- package/dist/cjs/did-btcr2.js +222 -0
- package/dist/cjs/did-btcr2.js.map +1 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/interfaces/beacon.js +41 -0
- package/dist/cjs/interfaces/beacon.js.map +1 -0
- package/dist/cjs/interfaces/crud.js +2 -0
- package/dist/cjs/interfaces/crud.js.map +1 -0
- package/dist/cjs/interfaces/ibeacon.js +2 -0
- package/dist/cjs/interfaces/ibeacon.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/bitcoin.js +62 -0
- package/dist/cjs/types/bitcoin.js.map +1 -0
- package/dist/cjs/types/crud.js +2 -0
- package/dist/cjs/types/crud.js.map +1 -0
- package/dist/cjs/utils/appendix.js +221 -0
- package/dist/cjs/utils/appendix.js.map +1 -0
- package/dist/cjs/utils/beacons.js +206 -0
- package/dist/cjs/utils/beacons.js.map +1 -0
- package/dist/cjs/utils/did-document-builder.js +61 -0
- package/dist/cjs/utils/did-document-builder.js.map +1 -0
- package/dist/cjs/utils/did-document.js +380 -0
- package/dist/cjs/utils/did-document.js.map +1 -0
- package/dist/cjs/utils/general.js +195 -0
- package/dist/cjs/utils/general.js.map +1 -0
- package/dist/cjs/utils/identifier.js +238 -0
- package/dist/cjs/utils/identifier.js.map +1 -0
- package/dist/esm/bitcoin/constants.js +20 -0
- package/dist/esm/bitcoin/constants.js.map +1 -0
- package/dist/esm/bitcoin/errors.js +11 -0
- package/dist/esm/bitcoin/errors.js.map +1 -0
- package/dist/esm/bitcoin/index.js +95 -0
- package/dist/esm/bitcoin/index.js.map +1 -0
- package/dist/esm/bitcoin/interface.js +2 -0
- package/dist/esm/bitcoin/interface.js.map +1 -0
- package/dist/esm/bitcoin/network.js +17 -0
- package/dist/esm/bitcoin/network.js.map +1 -0
- package/dist/esm/bitcoin/rest-client.js +289 -0
- package/dist/esm/bitcoin/rest-client.js.map +1 -0
- package/dist/esm/bitcoin/rpc-client.js +722 -0
- package/dist/esm/bitcoin/rpc-client.js.map +1 -0
- package/dist/esm/bitcoin/taproot.js +219 -0
- package/dist/esm/bitcoin/taproot.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/coordinator.js +120 -0
- package/dist/esm/btcr2/beacon/aggregation/coordinator.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/advert.js +24 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/base.js +37 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/base.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js +8 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/sign.js +7 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/participant.js +2 -0
- package/dist/esm/btcr2/beacon/aggregation/participant.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/service.js +2 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
- package/dist/esm/btcr2/beacon/cid-aggregate.js +116 -0
- package/dist/esm/btcr2/beacon/cid-aggregate.js.map +1 -0
- package/dist/esm/btcr2/beacon/factory.js +30 -0
- package/dist/esm/btcr2/beacon/factory.js.map +1 -0
- package/dist/esm/btcr2/beacon/singleton.js +220 -0
- package/dist/esm/btcr2/beacon/singleton.js.map +1 -0
- package/dist/esm/btcr2/beacon/smt-aggregate.js +126 -0
- package/dist/esm/btcr2/beacon/smt-aggregate.js.map +1 -0
- package/dist/esm/btcr2/crud/create.js +102 -0
- package/dist/esm/btcr2/crud/create.js.map +1 -0
- package/dist/esm/btcr2/crud/deactivate.js +14 -0
- package/dist/esm/btcr2/crud/deactivate.js.map +1 -0
- package/dist/esm/btcr2/crud/read.js +686 -0
- package/dist/esm/btcr2/crud/read.js.map +1 -0
- package/dist/esm/btcr2/crud/update.js +195 -0
- package/dist/esm/btcr2/crud/update.js.map +1 -0
- package/dist/esm/btcr2/key-manager/index.js +290 -0
- package/dist/esm/btcr2/key-manager/index.js.map +1 -0
- package/dist/esm/btcr2/key-manager/interface.js +2 -0
- package/dist/esm/btcr2/key-manager/interface.js.map +1 -0
- package/dist/esm/did-btcr2.js +222 -0
- package/dist/esm/did-btcr2.js.map +1 -0
- package/dist/esm/index.js +27 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/interfaces/beacon.js +41 -0
- package/dist/esm/interfaces/beacon.js.map +1 -0
- package/dist/esm/interfaces/crud.js +2 -0
- package/dist/esm/interfaces/crud.js.map +1 -0
- package/dist/esm/interfaces/ibeacon.js +2 -0
- package/dist/esm/interfaces/ibeacon.js.map +1 -0
- package/dist/esm/types/bitcoin.js +62 -0
- package/dist/esm/types/bitcoin.js.map +1 -0
- package/dist/esm/types/crud.js +2 -0
- package/dist/esm/types/crud.js.map +1 -0
- package/dist/esm/utils/appendix.js +221 -0
- package/dist/esm/utils/appendix.js.map +1 -0
- package/dist/esm/utils/beacons.js +206 -0
- package/dist/esm/utils/beacons.js.map +1 -0
- package/dist/esm/utils/did-document-builder.js +61 -0
- package/dist/esm/utils/did-document-builder.js.map +1 -0
- package/dist/esm/utils/did-document.js +380 -0
- package/dist/esm/utils/did-document.js.map +1 -0
- package/dist/esm/utils/general.js +195 -0
- package/dist/esm/utils/general.js.map +1 -0
- package/dist/esm/utils/identifier.js +238 -0
- package/dist/esm/utils/identifier.js.map +1 -0
- package/dist/types/bitcoin/constants.d.ts +19 -0
- package/dist/types/bitcoin/constants.d.ts.map +1 -0
- package/dist/types/bitcoin/errors.d.ts +5 -0
- package/dist/types/bitcoin/errors.d.ts.map +1 -0
- package/dist/types/bitcoin/index.d.ts +75 -0
- package/dist/types/bitcoin/index.d.ts.map +1 -0
- package/dist/types/bitcoin/interface.d.ts +86 -0
- package/dist/types/bitcoin/interface.d.ts.map +1 -0
- package/dist/types/bitcoin/network.d.ts +2 -0
- package/dist/types/bitcoin/network.d.ts.map +1 -0
- package/dist/types/bitcoin/rest-client.d.ts +268 -0
- package/dist/types/bitcoin/rest-client.d.ts.map +1 -0
- package/dist/types/bitcoin/rpc-client.d.ts +506 -0
- package/dist/types/bitcoin/rpc-client.d.ts.map +1 -0
- package/dist/types/bitcoin/taproot.d.ts +34 -0
- package/dist/types/bitcoin/taproot.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts +74 -0
- package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts +22 -0
- package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts +36 -0
- package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts +23 -0
- package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts +6 -0
- package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts +22 -0
- package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts +5 -0
- package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts +77 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts +7 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/participant.d.ts +1 -0
- package/dist/types/btcr2/beacon/aggregation/participant.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts +36 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts +6 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/cid-aggregate.d.ts +103 -0
- package/dist/types/btcr2/beacon/cid-aggregate.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/factory.d.ts +17 -0
- package/dist/types/btcr2/beacon/factory.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/singleton.d.ts +93 -0
- package/dist/types/btcr2/beacon/singleton.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/smt-aggregate.d.ts +112 -0
- package/dist/types/btcr2/beacon/smt-aggregate.d.ts.map +1 -0
- package/dist/types/btcr2/crud/create.d.ts +92 -0
- package/dist/types/btcr2/crud/create.d.ts.map +1 -0
- package/dist/types/btcr2/crud/deactivate.d.ts +13 -0
- package/dist/types/btcr2/crud/deactivate.d.ts.map +1 -0
- package/dist/types/btcr2/crud/read.d.ts +341 -0
- package/dist/types/btcr2/crud/read.d.ts.map +1 -0
- package/dist/types/btcr2/crud/update.d.ts +83 -0
- package/dist/types/btcr2/crud/update.d.ts.map +1 -0
- package/dist/types/btcr2/key-manager/index.d.ts +145 -0
- package/dist/types/btcr2/key-manager/index.d.ts.map +1 -0
- package/dist/types/btcr2/key-manager/interface.d.ts +113 -0
- package/dist/types/btcr2/key-manager/interface.d.ts.map +1 -0
- package/dist/types/did-btcr2.d.ts +117 -0
- package/dist/types/did-btcr2.d.ts.map +1 -0
- package/dist/types/index.d.ts +26 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/interfaces/beacon.d.ts +57 -0
- package/dist/types/interfaces/beacon.d.ts.map +1 -0
- package/dist/types/interfaces/crud.d.ts +35 -0
- package/dist/types/interfaces/crud.d.ts.map +1 -0
- package/dist/types/interfaces/ibeacon.d.ts +66 -0
- package/dist/types/interfaces/ibeacon.d.ts.map +1 -0
- package/dist/types/types/bitcoin.d.ts +827 -0
- package/dist/types/types/bitcoin.d.ts.map +1 -0
- package/dist/types/types/crud.d.ts +38 -0
- package/dist/types/types/crud.d.ts.map +1 -0
- package/dist/types/utils/appendix.d.ts +118 -0
- package/dist/types/utils/appendix.d.ts.map +1 -0
- package/dist/types/utils/beacons.d.ts +156 -0
- package/dist/types/utils/beacons.d.ts.map +1 -0
- package/dist/types/utils/did-document-builder.d.ts +13 -0
- package/dist/types/utils/did-document-builder.d.ts.map +1 -0
- package/dist/types/utils/did-document.d.ts +211 -0
- package/dist/types/utils/did-document.d.ts.map +1 -0
- package/dist/types/utils/general.d.ts +85 -0
- package/dist/types/utils/general.d.ts.map +1 -0
- package/dist/types/utils/identifier.d.ts +59 -0
- package/dist/types/utils/identifier.d.ts.map +1 -0
- package/package.json +137 -0
- package/src/bitcoin/constants.ts +19 -0
- package/src/bitcoin/errors.ts +10 -0
- package/src/bitcoin/index.ts +154 -0
- package/src/bitcoin/interface.ts +160 -0
- package/src/bitcoin/network.ts +17 -0
- package/src/bitcoin/rest-client.ts +415 -0
- package/src/bitcoin/rpc-client.ts +888 -0
- package/src/bitcoin/taproot.ts +237 -0
- package/src/btcr2/beacon/aggregation/coordinator.ts +135 -0
- package/src/btcr2/beacon/aggregation/messages/advert.ts +36 -0
- package/src/btcr2/beacon/aggregation/messages/base.ts +59 -0
- package/src/btcr2/beacon/aggregation/messages/cohort-set.ts +37 -0
- package/src/btcr2/beacon/aggregation/messages/keygen.ts +8 -0
- package/src/btcr2/beacon/aggregation/messages/opt-in.ts +35 -0
- package/src/btcr2/beacon/aggregation/messages/sign.ts +7 -0
- package/src/btcr2/beacon/aggregation/models/cohort/index.ts +112 -0
- package/src/btcr2/beacon/aggregation/models/cohort/status.ts +7 -0
- package/src/btcr2/beacon/aggregation/participant.ts +0 -0
- package/src/btcr2/beacon/aggregation/protocol/nostr.ts +81 -0
- package/src/btcr2/beacon/aggregation/protocol/service.ts +6 -0
- package/src/btcr2/beacon/cid-aggregate.ts +154 -0
- package/src/btcr2/beacon/factory.ts +36 -0
- package/src/btcr2/beacon/singleton.ts +257 -0
- package/src/btcr2/beacon/smt-aggregate.ts +136 -0
- package/src/btcr2/crud/create.ts +160 -0
- package/src/btcr2/crud/deactivate.ts +13 -0
- package/src/btcr2/crud/read.ts +946 -0
- package/src/btcr2/crud/update.ts +277 -0
- package/src/btcr2/key-manager/index.ts +364 -0
- package/src/btcr2/key-manager/interface.ts +129 -0
- package/src/canonicalize.d.ts +6 -0
- package/src/did-btcr2.ts +288 -0
- package/src/index.ts +34 -0
- package/src/interfaces/beacon.ts +68 -0
- package/src/interfaces/crud.ts +36 -0
- package/src/interfaces/ibeacon.ts +76 -0
- package/src/types/bitcoin.ts +1028 -0
- package/src/types/crud.ts +41 -0
- package/src/utils/appendix.ts +257 -0
- package/src/utils/beacons.ts +276 -0
- package/src/utils/did-document-builder.ts +73 -0
- package/src/utils/did-document.ts +474 -0
- package/src/utils/general.ts +204 -0
- package/src/utils/identifier.ts +276 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { KeyBytes, HdWallet } from '@did-btcr2/common';
|
|
2
|
+
import { HDKey } from '@scure/bip32';
|
|
3
|
+
/**
|
|
4
|
+
* Static class of general utility functions for the did-btcr2 spec implementation
|
|
5
|
+
* @class GeneralUtils
|
|
6
|
+
* @type {GeneralUtils}
|
|
7
|
+
*/
|
|
8
|
+
export declare class GeneralUtils {
|
|
9
|
+
/**
|
|
10
|
+
* Helper function to encode a secp256k1 key in SchnorrSecp256k1 Multikey Format
|
|
11
|
+
* @param {KeyBytes} xOnlyKeyBytes
|
|
12
|
+
* @returns {PublicKeyMultibase}
|
|
13
|
+
*/
|
|
14
|
+
static encode(xOnlyKeyBytes: KeyBytes): string;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a bigint to a buffer
|
|
17
|
+
* @param {bigint} value The bigint to convert
|
|
18
|
+
* @returns {Buffer} The buffer representation of the bigint
|
|
19
|
+
*/
|
|
20
|
+
static bigintToBuffer(value: bigint): Buffer;
|
|
21
|
+
/**
|
|
22
|
+
* Generates a new mnemonic phrase and HD wallet
|
|
23
|
+
* @returns {HdWallet} Promise resolving to a new hdwallet object w/ mnemonic and hdkey
|
|
24
|
+
* @throws {Error} if the public key bytes cannot be derived
|
|
25
|
+
*/
|
|
26
|
+
static generateHdWallet(): Promise<HdWallet>;
|
|
27
|
+
static generateCompressedSecp256k1KeyPair(): {
|
|
28
|
+
privateKey: import("@noble/secp256k1").Bytes;
|
|
29
|
+
publicKey: import("@noble/secp256k1").Bytes;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Recovers an HDKey from a mnemonic phrase
|
|
33
|
+
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
34
|
+
* @param {Uint8Array} seed Optional seed to recover the HDKey from
|
|
35
|
+
* @returns {HDKey} Promise resolving to the recovered HDKey
|
|
36
|
+
* @throws Error if the HDKey cannot be recovered
|
|
37
|
+
*/
|
|
38
|
+
static recoverHdWallet(mnemonic: string, seed?: Uint8Array): Promise<HDKey>;
|
|
39
|
+
/**
|
|
40
|
+
* Recovers a secp256k1 privateKey from its original entropy
|
|
41
|
+
* @param {Uint8Array} xorEntropy The original entropy to recover the privateKey from
|
|
42
|
+
* @param {Uint8Array} salt The salt used to tweak the privateKey
|
|
43
|
+
* @returns {Uint8Array} The recovered privateKey
|
|
44
|
+
* @throws {Error} if the privateKey cannot be recovered
|
|
45
|
+
*/
|
|
46
|
+
static recoverTweakedRawPrivateKey(xorEntropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
47
|
+
/**
|
|
48
|
+
* Recovers a secp256k1 privateKey from its original entropy
|
|
49
|
+
* @param {Uint8Array} entropy The entropy to recover the privateKey from
|
|
50
|
+
* @returns {Uint8Array} The recovered privateKey
|
|
51
|
+
* @throws {Error} if the privateKey cannot be recovered
|
|
52
|
+
*/
|
|
53
|
+
static recoverRawPrivateKey(entropy: Uint8Array): Uint8Array;
|
|
54
|
+
/**
|
|
55
|
+
* Tweak the entropy with a salt using XOR
|
|
56
|
+
* @param {Uint8Array} entropy The entropy to tweak
|
|
57
|
+
* @param {Uint8Array} salt The salt to tweak the entropy with
|
|
58
|
+
* @returns {Uint8Array} The tweaked entropy
|
|
59
|
+
*/
|
|
60
|
+
static XOR(entropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
61
|
+
/**
|
|
62
|
+
* Untweak the entropy with a salt using XNOR
|
|
63
|
+
*
|
|
64
|
+
* @param {Uint8Array} tweakedEntropy The tweaked entropy to untweak
|
|
65
|
+
* @param {Uint8Array} salt The salt to untweak the entropy with
|
|
66
|
+
* @returns {Uint8Array} The original entropy
|
|
67
|
+
*/
|
|
68
|
+
static XNOR(tweakedEntropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
69
|
+
/**
|
|
70
|
+
* Recovers an HDKey from a mnemonic phrase
|
|
71
|
+
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
72
|
+
* @param {string} path The path to derive the child key from
|
|
73
|
+
* @returns {Uint8Array} Promise resolving to the recovered private key bytes
|
|
74
|
+
* @throws {Error} if the HDKey cannot be recovered
|
|
75
|
+
*/
|
|
76
|
+
static recoverHdChildFromMnemonic(mnemonic: string, path: string): Promise<Uint8Array>;
|
|
77
|
+
/**
|
|
78
|
+
* Derives a child key from an HDKey
|
|
79
|
+
* @param {HDKey} hdkey The HDKey to derive the child key from
|
|
80
|
+
* @param {string} path The path to derive the child key from
|
|
81
|
+
* @returns {HDKey} A Promise resolving to the child key
|
|
82
|
+
* @throws {Error} Error if the child key cannot be derived
|
|
83
|
+
*/
|
|
84
|
+
static deriveChildKey(hdkey: HDKey, path: string): HDKey;
|
|
85
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../../src/utils/general.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAsC,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAG3F,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAKrC;;;;GAIG;AACH,qBAAa,YAAY;IACvB;;;;OAIG;WACW,MAAM,CAAC,aAAa,EAAE,QAAQ,GAAG,MAAM;IAYrD;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAK5C;;;;OAIG;WACU,gBAAgB,IAAI,OAAO,CAAC,QAAQ,CAAC;IAclD,MAAM,CAAC,kCAAkC;;;;IAQzC;;;;;;OAMG;WACU,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC;IAYjF;;;;;;OAMG;IACH,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU;IAkBxF;;;;;OAKG;IACH,MAAM,CAAC,oBAAoB,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU;IAiB5D;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU;IAQ7D;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU;IAQrE;;;;;;OAMG;WACU,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAe5F;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK;CAUzD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Bytes } from '@did-btcr2/common';
|
|
2
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
3
|
+
import { DidComponents } from './appendix.js';
|
|
4
|
+
/**
|
|
5
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#syntax | 3 Syntax}.
|
|
6
|
+
* A did:btcr2 DID consists of a did:btcr2 prefix, followed by an id-bech32 value, which is a Bech32m encoding of:
|
|
7
|
+
* - the specification version;
|
|
8
|
+
* - the Bitcoin network identifier; and
|
|
9
|
+
* - either:
|
|
10
|
+
* - a key-value representing a secp256k1 public key; or
|
|
11
|
+
* - a hash-value representing the hash of an initiating external DID document.
|
|
12
|
+
* @class Btc1Identifier
|
|
13
|
+
* @type {Btc1Identifier}
|
|
14
|
+
*/
|
|
15
|
+
export declare class Btc1Identifier {
|
|
16
|
+
/**
|
|
17
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#didbtc1-identifier-encoding | 3.2 did:btcr2 Identifier Encoding}.
|
|
18
|
+
*
|
|
19
|
+
* A did:btcr2 DID consists of a did:btcr2 prefix, followed by an id-bech32 value, which is a Bech32m encoding of:
|
|
20
|
+
* - the specification version;
|
|
21
|
+
* - the Bitcoin network identifier; and
|
|
22
|
+
* - either:
|
|
23
|
+
* - a key-value representing a secp256k1 public key; or
|
|
24
|
+
* - a hash-value representing the hash of an initiating external DID document.
|
|
25
|
+
*
|
|
26
|
+
* @param {CreateIdentifierParams} params See {@link CreateIdentifierParams} for details.
|
|
27
|
+
* @param {Btcr2IdentifierTypes} params.idType Identifier type (key or external).
|
|
28
|
+
* @param {string} params.network Bitcoin network name.
|
|
29
|
+
* @param {number} params.version Identifier version.
|
|
30
|
+
* @param {KeyBytes | DocumentBytes} params.genesisBytes Public key or an intermediate document bytes.
|
|
31
|
+
* @returns {string} The new did:btcr2 identifier.
|
|
32
|
+
*/
|
|
33
|
+
static encode({ idType, version, network, genesisBytes }: {
|
|
34
|
+
idType: string;
|
|
35
|
+
version: number;
|
|
36
|
+
network: string | number;
|
|
37
|
+
genesisBytes: Bytes;
|
|
38
|
+
}): string;
|
|
39
|
+
/**
|
|
40
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#didbtc1-identifier-decoding | 3.3 did:btcr2 Identifier Decoding}.
|
|
41
|
+
* @param {string} identifier The BTCR2 DID to be parsed
|
|
42
|
+
* @returns {DidComponents} The parsed identifier components. See {@link DidComponents} for details.
|
|
43
|
+
* @throws {DidError} if an error occurs while parsing the identifier
|
|
44
|
+
* @throws {DidErrorCode.InvalidDid} if identifier is invalid
|
|
45
|
+
* @throws {DidErrorCode.MethodNotSupported} if the method is not supported
|
|
46
|
+
*/
|
|
47
|
+
static decode(identifier: string): DidComponents;
|
|
48
|
+
/**
|
|
49
|
+
* Generates a new did:btcr2 identifier based on a newly generated key pair.
|
|
50
|
+
* @returns {string} The new did:btcr2 identifier.
|
|
51
|
+
*/
|
|
52
|
+
static generate(): {
|
|
53
|
+
keys: SchnorrKeyPair;
|
|
54
|
+
identifier: {
|
|
55
|
+
controller: string;
|
|
56
|
+
id: string;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.d.ts","sourceRoot":"","sources":["../../../src/utils/identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyD,KAAK,EAAqC,MAAM,mBAAmB,CAAC;AACpI,OAAO,EAAa,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,qBAAa,cAAc;IACzB;;;;;;;;;;;;;;;;OAgBG;WACW,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,EAAE;QAC/D,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;QACzB,YAAY,EAAE,KAAK,CAAC;KACrB,GAAG,MAAM;IAuFV;;;;;;;OAOG;WACW,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,aAAa;IA8HvD;;;OAGG;WACW,QAAQ,IAAI;QAAE,IAAI,EAAE,cAAc,CAAC;QAAC,UAAU,EAAE;YAAE,UAAU,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE;CAWnG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@did-btcr2/method",
|
|
3
|
+
"version": "0.13.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Javascript/TypeScript reference implementation of did:btcr2 method, a censorship resistant DID Method using the Bitcoin blockchain as a Verifiable Data Registry to announce changes to the DID document. Core package of the did-btcr2-js monorepo.",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/esm/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./browser": {
|
|
16
|
+
"types": "./dist/types/index.d.ts",
|
|
17
|
+
"import": "./dist/browser.mjs",
|
|
18
|
+
"require": "./dist/browser.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"license": "MPL-2.0",
|
|
26
|
+
"contributors": [
|
|
27
|
+
{
|
|
28
|
+
"name": "dcdpr",
|
|
29
|
+
"url": "https://github.com/dcdpr"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"name": "jintekc",
|
|
33
|
+
"url": "https://github.com/jintekc",
|
|
34
|
+
"email": "github@jintek.consulting"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"homepage": "https://github.com/dcdpr/did-btcr2-js/tree/main/packages/method",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+ssh://git@github.com:dcdpr/did-btcr2-js.git",
|
|
41
|
+
"directory": "packages/method"
|
|
42
|
+
},
|
|
43
|
+
"bugs": "https://github.com/dcdpr/did-btcr2-js/issues",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.0.0"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"did",
|
|
52
|
+
"dids",
|
|
53
|
+
"decentralized identity",
|
|
54
|
+
"decentralized identifiers",
|
|
55
|
+
"did method",
|
|
56
|
+
"did:btcr2",
|
|
57
|
+
"bitcoin"
|
|
58
|
+
],
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@helia/strings": "^4.0.2",
|
|
61
|
+
"@noble/curves": "^1.8.1",
|
|
62
|
+
"@noble/hashes": "^1.5.0",
|
|
63
|
+
"@noble/secp256k1": "^2.1.0",
|
|
64
|
+
"@scure/base": "^1.1.9",
|
|
65
|
+
"@scure/bip32": "^1.5.0",
|
|
66
|
+
"@scure/bip39": "^1.4.0",
|
|
67
|
+
"@scure/btc-signer": "^1.8.1",
|
|
68
|
+
"@web5/common": "^1.1.0",
|
|
69
|
+
"@web5/crypto": "^1.0.6",
|
|
70
|
+
"@web5/dids": "^1.2.0",
|
|
71
|
+
"bitcoin-core": "^5.0.0",
|
|
72
|
+
"bitcoinjs-lib": "7.0.0-rc.0",
|
|
73
|
+
"canonicalize": "^2.1.0",
|
|
74
|
+
"dotenv": "^16.5.0",
|
|
75
|
+
"helia": "^5.2.1",
|
|
76
|
+
"multiformats": "^13.3.1",
|
|
77
|
+
"nostr-tools": "^2.15.0",
|
|
78
|
+
"tiny-secp256k1": "^2.2.3",
|
|
79
|
+
"@did-btcr2/common": "2.0.0",
|
|
80
|
+
"@did-btcr2/keypair": "0.5.1",
|
|
81
|
+
"@did-btcr2/cryptosuite": "3.0.1"
|
|
82
|
+
},
|
|
83
|
+
"devDependencies": {
|
|
84
|
+
"@eslint/js": "^9.22.0",
|
|
85
|
+
"@types/chai": "^5.0.1",
|
|
86
|
+
"@types/chai-as-promised": "^8.0.1",
|
|
87
|
+
"@types/eslint": "^9.6.1",
|
|
88
|
+
"@types/mocha": "^10.0.9",
|
|
89
|
+
"@types/node": "^22.5.4",
|
|
90
|
+
"@typescript-eslint/eslint-plugin": "^8.5.0",
|
|
91
|
+
"@typescript-eslint/parser": "^8.5.0",
|
|
92
|
+
"c8": "^10.1.2",
|
|
93
|
+
"chai": "^5.1.2",
|
|
94
|
+
"chai-as-promised": "^8.0.0",
|
|
95
|
+
"esbuild": "^0.24.2",
|
|
96
|
+
"eslint": "^9.14.0",
|
|
97
|
+
"eslint-plugin-mocha": "^10.5.0",
|
|
98
|
+
"globals": "^15.11.0",
|
|
99
|
+
"mocha": "^10.8.2",
|
|
100
|
+
"mocha-junit-reporter": "^2.2.1",
|
|
101
|
+
"node-stdlib-browser": "^1.3.1",
|
|
102
|
+
"rimraf": "^6.0.1",
|
|
103
|
+
"typedoc-plugin-markdown": "^4.7.0",
|
|
104
|
+
"typescript": "^5.6.2",
|
|
105
|
+
"typescript-eslint": "^8.19.1"
|
|
106
|
+
},
|
|
107
|
+
"scripts": {
|
|
108
|
+
"clean": "rimraf dist coverage tests/compiled",
|
|
109
|
+
"clean:build": "rimraf dist",
|
|
110
|
+
"clean:tests": "rimraf coverage tests/compiled",
|
|
111
|
+
"wipe": "rimraf node_modules pnpm-lock.json",
|
|
112
|
+
"wipe:clean": "pnpm wipe && pnpm clean",
|
|
113
|
+
"wipe:install": "pnpm wipe && pnpm install",
|
|
114
|
+
"clean:install": "pnpm clean && pnpm install",
|
|
115
|
+
"wipe:clean:install": "pnpm wipe:clean && pnpm install",
|
|
116
|
+
"reinstall": "pnpm install --force",
|
|
117
|
+
"build": "pnpm clean:build && pnpm build:esm && pnpm build:cjs && pnpm build:browser",
|
|
118
|
+
"build:esm": "rimraf dist/esm dist/types && pnpm tsc -p tsconfig.json",
|
|
119
|
+
"build:cjs": "rimraf dist/cjs && tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
|
|
120
|
+
"build:browser": "rimraf dist/browser.mjs dist/browser.js && node build/bundles.js",
|
|
121
|
+
"build:tests": "pnpm clean:tests && pnpm tsc -p tests/tsconfig.json",
|
|
122
|
+
"build:docs": "typedoc --options typedoc.json",
|
|
123
|
+
"build:all": "pnpm build && pnpm build:tests && pnpm build:docs",
|
|
124
|
+
"release": "pnpm build && pnpm pack && mv *.tgz ../../release/method",
|
|
125
|
+
"lint": "eslint . --max-warnings 0",
|
|
126
|
+
"lint:fix": "eslint . --fix",
|
|
127
|
+
"test": "pnpm c8 mocha",
|
|
128
|
+
"test:unit": "[ -z \"$FILE_NAME\" ] && echo 'ERROR: FILE_NAME is not set' || pnpm tsx lib/test-$FILE_NAME.ts",
|
|
129
|
+
"build:test": "pnpm build && pnpm build:tests && pnpm c8 mocha",
|
|
130
|
+
"build:lint:test": "pnpm build && pnpm build:tests && pnpm lint:fix",
|
|
131
|
+
"prepublish": "pnpm build",
|
|
132
|
+
"do": "pnpm tsx",
|
|
133
|
+
"do:lib": "${PWD}/lib/run-lib.sh",
|
|
134
|
+
"do:lib:btc": "${PWD}/lib/do-lib.sh bitcoin",
|
|
135
|
+
"do:lib:btcr2": "${PWD}/lib/do-lib.sh btcr2"
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const INITIAL_BLOCK_REWARD = 50;
|
|
2
|
+
export const HALVING_INTERVAL = 150;
|
|
3
|
+
export const COINBASE_MATURITY_DELAY = 100;
|
|
4
|
+
export const DEFAULT_RPC_CLIENT_CONFIG = {
|
|
5
|
+
network : 'regtest',
|
|
6
|
+
host : 'http://localhost:18443',
|
|
7
|
+
port : 18443,
|
|
8
|
+
username : 'polaruser',
|
|
9
|
+
password : 'polarpass',
|
|
10
|
+
allowDefaultWallet : true,
|
|
11
|
+
version : '28.1.0',
|
|
12
|
+
};
|
|
13
|
+
export const DEFAULT_REST_CLIENT_CONFIG = {
|
|
14
|
+
network : 'regtest',
|
|
15
|
+
host : 'http://localhost:3000'
|
|
16
|
+
};
|
|
17
|
+
export const DEFAULT_BLOCK_CONFIRMATIONS = 7;
|
|
18
|
+
export const TXIN_WITNESS_COINBASE = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
19
|
+
export const GENESIS_TX_ID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export class BitcoinRpcError extends Error {
|
|
2
|
+
public readonly code: number | string;
|
|
3
|
+
public readonly data?: any;
|
|
4
|
+
constructor(code: number | string, message: string, data?: any) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.data = data;
|
|
8
|
+
this.name = 'BitcoinRpcError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Btcr2Error } from '@did-btcr2/common';
|
|
2
|
+
import { networks } from 'bitcoinjs-lib';
|
|
3
|
+
import { DEFAULT_REST_CLIENT_CONFIG, DEFAULT_RPC_CLIENT_CONFIG } from './constants.js';
|
|
4
|
+
import { getNetwork } from './network.js';
|
|
5
|
+
import BitcoinRest from './rest-client.js';
|
|
6
|
+
import BitcoinRpc from './rpc-client.js';
|
|
7
|
+
|
|
8
|
+
type BitcoinClientConfig = {
|
|
9
|
+
rpc: {
|
|
10
|
+
username: string;
|
|
11
|
+
password: string;
|
|
12
|
+
host: string;
|
|
13
|
+
version: string;
|
|
14
|
+
allowDefaultWallet: boolean;
|
|
15
|
+
},
|
|
16
|
+
rest?: {
|
|
17
|
+
host: string;
|
|
18
|
+
username?: string;
|
|
19
|
+
password?: string;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type BitcoinNetworkConfig = {
|
|
24
|
+
name: keyof AvailableNetworks;
|
|
25
|
+
rpc: BitcoinRpc;
|
|
26
|
+
rest: BitcoinRest;
|
|
27
|
+
config: BitcoinClientConfig;
|
|
28
|
+
data: networks.Network;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type BitcoinNetworkConfigMap = {
|
|
32
|
+
[key in keyof AvailableNetworks]?: BitcoinNetworkConfig;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type AvailableNetworks = {
|
|
36
|
+
mainnet: true;
|
|
37
|
+
testnet: true;
|
|
38
|
+
signet: true;
|
|
39
|
+
mutinynet: true;
|
|
40
|
+
regtest: true;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* General class to house the Bitcoin client connections, client config and various utility methods.
|
|
45
|
+
* @name Bitcoin
|
|
46
|
+
* @type {Bitcoin}
|
|
47
|
+
*/
|
|
48
|
+
export class Bitcoin {
|
|
49
|
+
public network: BitcoinNetworkConfig;
|
|
50
|
+
public mainnet?: BitcoinNetworkConfig;
|
|
51
|
+
public testnet?: BitcoinNetworkConfig;
|
|
52
|
+
public signet?: BitcoinNetworkConfig;
|
|
53
|
+
public mutinynet?: BitcoinNetworkConfig;
|
|
54
|
+
public regtest?: BitcoinNetworkConfig;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates an instance of the Bitcoin class.
|
|
58
|
+
* @param {BitcoinNetworkConfigMap} configs Optional configuration object for the Bitcoin client. If not provided, it will
|
|
59
|
+
* be loaded from the BITCOIN_CLIENT_CONFIG environment variables.
|
|
60
|
+
* @throws {Btcr2Error} If no configs is passed and BITCOIN_NETWORK_CONFIG is missing or invalid.
|
|
61
|
+
*/
|
|
62
|
+
constructor(configs?: BitcoinNetworkConfigMap) {
|
|
63
|
+
const BITCOIN_NETWORK_CONFIG = process.env.BITCOIN_NETWORK_CONFIG ?? JSON.stringify(configs ?? {
|
|
64
|
+
regtest : {
|
|
65
|
+
rpc : DEFAULT_RPC_CLIENT_CONFIG,
|
|
66
|
+
rest : DEFAULT_REST_CLIENT_CONFIG
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if(!BITCOIN_NETWORK_CONFIG) {
|
|
71
|
+
throw new Btcr2Error(
|
|
72
|
+
'No BITCOIN_NETWORK_CONFIG available: must pass `configs` to constructor or set `BITCOIN_NETWORK_CONFIG` in env',
|
|
73
|
+
'MISSING_BITCOIN_NETWORK_CONFIG',
|
|
74
|
+
{ BITCOIN_NETWORK_CONFIG }
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Check if BITCOIN_NETWORK_CONFIG is parsable JSON string
|
|
79
|
+
if (!JSON.parsable(BITCOIN_NETWORK_CONFIG)) {
|
|
80
|
+
throw new Btcr2Error(
|
|
81
|
+
'Parsing failed: malformed BITCOIN_NETWORK_CONFIG',
|
|
82
|
+
'MISSING_MALFORMED_BITCOIN_NETWORK_CONFIG',
|
|
83
|
+
{ BITCOIN_NETWORK_CONFIG }
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Parse the BITCOIN_NETWORK_CONFIG
|
|
88
|
+
const networkConfigs: Record<string, BitcoinClientConfig> = JSON.parse(BITCOIN_NETWORK_CONFIG);
|
|
89
|
+
|
|
90
|
+
// Set a list of available networks
|
|
91
|
+
const networks: (keyof AvailableNetworks)[] = ['mainnet', 'testnet', 'signet', 'mutinynet', 'regtest'];
|
|
92
|
+
|
|
93
|
+
// Iterate over the networks and create the client connections
|
|
94
|
+
for (const network of networks) {
|
|
95
|
+
const networkConfig: BitcoinClientConfig = (configs?.[network] ?? networkConfigs[network]) as BitcoinClientConfig;
|
|
96
|
+
if (networkConfig) {
|
|
97
|
+
this[network] = {
|
|
98
|
+
name : network,
|
|
99
|
+
config : networkConfig,
|
|
100
|
+
rpc : new BitcoinRpc(networkConfig.rpc ?? DEFAULT_RPC_CLIENT_CONFIG),
|
|
101
|
+
rest : new BitcoinRest(networkConfig.rest ?? DEFAULT_REST_CLIENT_CONFIG) ,
|
|
102
|
+
data : getNetwork(network),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Load and check the ACTIVE_NETWORK variable
|
|
108
|
+
const ACTIVE_NETWORK = (process.env.ACTIVE_NETWORK?.toLowerCase() ?? 'regtest') as keyof AvailableNetworks;
|
|
109
|
+
if (!ACTIVE_NETWORK) {
|
|
110
|
+
throw new Btcr2Error('Missing ACTIVE_NETWORK environment variable', 'MISSING_ACTIVE_NETWORK', { ACTIVE_NETWORK });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
if (!this[ACTIVE_NETWORK]) {
|
|
115
|
+
throw new Btcr2Error(
|
|
116
|
+
`No configuration found for ACTIVE_NETWORK='${ACTIVE_NETWORK}'`,
|
|
117
|
+
'MISSING_CONFIG_FOR_NETWORK'
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.network = this[ACTIVE_NETWORK];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Converts Bitcoin (BTC) to satoshis (SAT).
|
|
126
|
+
* @param {number} btc - The amount in BTC.
|
|
127
|
+
* @returns {number} The amount in SAT.
|
|
128
|
+
*/
|
|
129
|
+
public static btcToSats (btc: number): number {
|
|
130
|
+
return Math.round(btc * 1e8);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Converts satoshis (SAT) to Bitcoin (BTC).
|
|
135
|
+
* @param {number} sats - The amount in SAT.
|
|
136
|
+
* @returns {number} The amount in BTC.
|
|
137
|
+
*/
|
|
138
|
+
public static satsToBtc (sats: number): number {
|
|
139
|
+
return sats / 1e8;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const connection = new Bitcoin();
|
|
144
|
+
const network = connection.network;
|
|
145
|
+
const bitcoin = {
|
|
146
|
+
network,
|
|
147
|
+
rest : {
|
|
148
|
+
transaction : network.rest.transaction,
|
|
149
|
+
block : network.rest.block,
|
|
150
|
+
address : network.rest.address,
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export default bitcoin;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddMultiSigAddressParams,
|
|
3
|
+
BatchOption,
|
|
4
|
+
BlockResponse,
|
|
5
|
+
BumpFeeOption,
|
|
6
|
+
BumpFeeResult,
|
|
7
|
+
ChainInfo,
|
|
8
|
+
CreateMultiSigResult,
|
|
9
|
+
CreateRawTxInputs,
|
|
10
|
+
CreateRawTxOutputs,
|
|
11
|
+
CreateWalletParams,
|
|
12
|
+
CreateWalletResult,
|
|
13
|
+
DecodedRawTransaction,
|
|
14
|
+
FeeEstimateMode,
|
|
15
|
+
FundRawTxOptions,
|
|
16
|
+
GetBlockParams,
|
|
17
|
+
MempoolInfo,
|
|
18
|
+
MiningInfo,
|
|
19
|
+
PeerInfo,
|
|
20
|
+
RawTransactionResponse,
|
|
21
|
+
ScriptDecoded,
|
|
22
|
+
ValidateAddressResult
|
|
23
|
+
} from '../types/bitcoin.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Interface for the Bitcoin Core RPC client.
|
|
27
|
+
*/
|
|
28
|
+
export interface IBitcoinRpc {
|
|
29
|
+
/** Executes multiple commands in a batch request. */
|
|
30
|
+
command?<R extends ReturnType<any>>(methods: BatchOption[]): Promise<ReadonlyArray<R>>;
|
|
31
|
+
|
|
32
|
+
/** Marks a transaction and its in-wallet descendants as abandoned, allowing their inputs to be respent. */
|
|
33
|
+
abandonTransaction(txid: string): Promise<void>;
|
|
34
|
+
|
|
35
|
+
/** Stops the current wallet rescan triggered by an RPC call, such as by an importprivkey call. */
|
|
36
|
+
abortRescan(): Promise<void>;
|
|
37
|
+
|
|
38
|
+
/** Adds a multi-signature address with n required signatures and a set of keys. */
|
|
39
|
+
addMultiSigAddress({ nrequired, keys, account }: AddMultiSigAddressParams): Promise<string>;
|
|
40
|
+
|
|
41
|
+
/** Adds a witness address for SegWit compatibility. */
|
|
42
|
+
addWitnessAddress(address: string): Promise<void>;
|
|
43
|
+
|
|
44
|
+
/** Backs up the wallet file to a specified destination. */
|
|
45
|
+
backupWallet(destination: string): Promise<void>;
|
|
46
|
+
|
|
47
|
+
/** Increases the fee of an unconfirmed transaction to improve its confirmation time. */
|
|
48
|
+
bumpFee(txid: string, options?: BumpFeeOption): Promise<BumpFeeResult>;
|
|
49
|
+
|
|
50
|
+
/** Removes all banned nodes from the ban list. */
|
|
51
|
+
clearBanned(): Promise<void>;
|
|
52
|
+
|
|
53
|
+
/** Combines multiple raw transactions into a single transaction. */
|
|
54
|
+
combineRawTransaction(txs: string[]): Promise<string>;
|
|
55
|
+
|
|
56
|
+
/** Creates a multi-signature address with n required signatures and a set of keys. */
|
|
57
|
+
createMultiSig(nrequired: number, keys: string[]): Promise<CreateMultiSigResult>;
|
|
58
|
+
|
|
59
|
+
/** Creates a raw transaction spending specified inputs to specified outputs. */
|
|
60
|
+
createRawTransaction(inputs: CreateRawTxInputs[], outputs: CreateRawTxOutputs[], locktime?: number, replacable?: boolean): Promise<string>;
|
|
61
|
+
|
|
62
|
+
/** Creates a new wallet with various optional parameters. */
|
|
63
|
+
createWallet({
|
|
64
|
+
wallet_name,
|
|
65
|
+
disable_private_keys,
|
|
66
|
+
blank,
|
|
67
|
+
passphrase,
|
|
68
|
+
avoid_reuse,
|
|
69
|
+
descriptors,
|
|
70
|
+
load_on_startup,
|
|
71
|
+
}: CreateWalletParams): Promise<CreateWalletResult>;
|
|
72
|
+
|
|
73
|
+
/** Decodes a raw transaction hex string. */
|
|
74
|
+
decodeRawTransaction(hexstring: string): Promise<DecodedRawTransaction>;
|
|
75
|
+
|
|
76
|
+
/** Decodes a hex-encoded script. */
|
|
77
|
+
decodeScript(hexstring: string): Promise<ScriptDecoded>;
|
|
78
|
+
|
|
79
|
+
/** Disconnects a node by address or node ID. */
|
|
80
|
+
disconnectNode(address?: string, nodeid?: number): Promise<void>;
|
|
81
|
+
|
|
82
|
+
/** Reveals the private key corresponding to an address. */
|
|
83
|
+
dumpPrivKey(address: string): Promise<string>;
|
|
84
|
+
|
|
85
|
+
/** Dumps all wallet keys and metadata to a file. */
|
|
86
|
+
dumpWallet(filename: string): Promise<{ filename: string }>;
|
|
87
|
+
|
|
88
|
+
/** Encrypts the wallet with a passphrase. */
|
|
89
|
+
encryptWallet(passphrase: string): Promise<void>;
|
|
90
|
+
|
|
91
|
+
/** Estimates the fee rate for a given confirmation target. */
|
|
92
|
+
estimateSmartFee(
|
|
93
|
+
conf_target: number,
|
|
94
|
+
estimate_mode?: FeeEstimateMode,
|
|
95
|
+
): Promise<{ feerate?: number; errors?: string[]; blocks?: number }>;
|
|
96
|
+
|
|
97
|
+
/** Funds a raw transaction by adding necessary inputs and change. */
|
|
98
|
+
fundRawTransaction(
|
|
99
|
+
hexstring: string,
|
|
100
|
+
options: FundRawTxOptions,
|
|
101
|
+
): Promise<{ hex: string; fee: number; changepos: number }>;
|
|
102
|
+
|
|
103
|
+
/** Returns the number of blocks in the longest blockchain. */
|
|
104
|
+
getBlockCount(): Promise<number>;
|
|
105
|
+
|
|
106
|
+
/** Gets the hash of a block at a given height. */
|
|
107
|
+
getBlockHash(height: number): Promise<string>;
|
|
108
|
+
|
|
109
|
+
/** Gets detailed information about a specific block. */
|
|
110
|
+
getBlock({ blockhash, height, verbosity }: GetBlockParams): Promise<BlockResponse | undefined>
|
|
111
|
+
|
|
112
|
+
/** Retrieves general blockchain state info. */
|
|
113
|
+
getBlockchainInfo(): Promise<ChainInfo>;
|
|
114
|
+
|
|
115
|
+
/** Gets the number of active connections to other nodes. */
|
|
116
|
+
getConnectionCount(): Promise<number>;
|
|
117
|
+
|
|
118
|
+
/** Gets the estimated network difficulty. */
|
|
119
|
+
getDifficulty(): Promise<number>;
|
|
120
|
+
|
|
121
|
+
/** Retrieves memory pool statistics. */
|
|
122
|
+
getMempoolInfo(): Promise<MempoolInfo>;
|
|
123
|
+
|
|
124
|
+
/** Retrieves mining statistics. */
|
|
125
|
+
getMiningInfo(): Promise<MiningInfo>;
|
|
126
|
+
|
|
127
|
+
/** Gets a new Bitcoin address for receiving payments. */
|
|
128
|
+
getNewAddress(account?: string): Promise<string>;
|
|
129
|
+
|
|
130
|
+
/** Gets detailed peer connection information. */
|
|
131
|
+
getPeerInfo(): Promise<PeerInfo[]>;
|
|
132
|
+
|
|
133
|
+
/** Sends raw transaction hex to the Bitcoin network. */
|
|
134
|
+
sendRawTransaction(
|
|
135
|
+
hexstring: string,
|
|
136
|
+
maxfeerate?: number | string,
|
|
137
|
+
maxBurnAmount?: number | string
|
|
138
|
+
): Promise<string>;
|
|
139
|
+
|
|
140
|
+
/** Sends bitcoins to a specified address. */
|
|
141
|
+
sendToAddress(
|
|
142
|
+
address: string,
|
|
143
|
+
amount: number,
|
|
144
|
+
comment?: string,
|
|
145
|
+
comment_to?: string,
|
|
146
|
+
subtreactfeefromamount?: boolean,
|
|
147
|
+
replaceable?: boolean,
|
|
148
|
+
conf_target?: number,
|
|
149
|
+
estimate_mode?: FeeEstimateMode,
|
|
150
|
+
): Promise<RawTransactionResponse>;
|
|
151
|
+
|
|
152
|
+
/** Validates a Bitcoin address. */
|
|
153
|
+
validateAddress(address: string): Promise<ValidateAddressResult>;
|
|
154
|
+
|
|
155
|
+
/** Verifies a signed message. */
|
|
156
|
+
verifyMessage(address: string, signature: string, message: string): Promise<boolean>;
|
|
157
|
+
|
|
158
|
+
/** Locks the wallet, requiring a passphrase to unlock. */
|
|
159
|
+
walletLock(passphrase: string, timeout: number): Promise<void>;
|
|
160
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { networks } from 'bitcoinjs-lib';
|
|
2
|
+
|
|
3
|
+
export function getNetwork(network: string): networks.Network {
|
|
4
|
+
switch (network) {
|
|
5
|
+
case 'bitcoin':
|
|
6
|
+
return networks.bitcoin;
|
|
7
|
+
case 'testnet3':
|
|
8
|
+
case 'testnet4':
|
|
9
|
+
case 'signet':
|
|
10
|
+
case 'mutinynet':
|
|
11
|
+
return networks.testnet;
|
|
12
|
+
case 'regtest':
|
|
13
|
+
return networks.regtest;
|
|
14
|
+
default:
|
|
15
|
+
throw new Error(`Unknown network "${network}"`);
|
|
16
|
+
}
|
|
17
|
+
}
|