@sidhujag/sysweb3-keyring 1.0.491

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.
Files changed (104) hide show
  1. package/README.md +201 -0
  2. package/cjs/errorUtils.js +75 -0
  3. package/cjs/errorUtils.js.map +1 -0
  4. package/cjs/hardware-wallet-manager.js +462 -0
  5. package/cjs/hardware-wallet-manager.js.map +1 -0
  6. package/cjs/index.js +31 -0
  7. package/cjs/index.js.map +1 -0
  8. package/cjs/initial-state.js +105 -0
  9. package/cjs/initial-state.js.map +1 -0
  10. package/cjs/keyring-manager.js +1687 -0
  11. package/cjs/keyring-manager.js.map +1 -0
  12. package/cjs/ledger/bitcoin_client/index.js +47 -0
  13. package/cjs/ledger/bitcoin_client/index.js.map +1 -0
  14. package/cjs/ledger/bitcoin_client/lib/appClient.js +408 -0
  15. package/cjs/ledger/bitcoin_client/lib/appClient.js.map +1 -0
  16. package/cjs/ledger/bitcoin_client/lib/bip32.js +61 -0
  17. package/cjs/ledger/bitcoin_client/lib/bip32.js.map +1 -0
  18. package/cjs/ledger/bitcoin_client/lib/buffertools.js +126 -0
  19. package/cjs/ledger/bitcoin_client/lib/buffertools.js.map +1 -0
  20. package/cjs/ledger/bitcoin_client/lib/clientCommands.js +270 -0
  21. package/cjs/ledger/bitcoin_client/lib/clientCommands.js.map +1 -0
  22. package/cjs/ledger/bitcoin_client/lib/constants.js +16 -0
  23. package/cjs/ledger/bitcoin_client/lib/constants.js.map +1 -0
  24. package/cjs/ledger/bitcoin_client/lib/merkelizedPsbt.js +54 -0
  25. package/cjs/ledger/bitcoin_client/lib/merkelizedPsbt.js.map +1 -0
  26. package/cjs/ledger/bitcoin_client/lib/merkle.js +109 -0
  27. package/cjs/ledger/bitcoin_client/lib/merkle.js.map +1 -0
  28. package/cjs/ledger/bitcoin_client/lib/merkleMap.js +46 -0
  29. package/cjs/ledger/bitcoin_client/lib/merkleMap.js.map +1 -0
  30. package/cjs/ledger/bitcoin_client/lib/policy.js +66 -0
  31. package/cjs/ledger/bitcoin_client/lib/policy.js.map +1 -0
  32. package/cjs/ledger/bitcoin_client/lib/psbtv2.js +640 -0
  33. package/cjs/ledger/bitcoin_client/lib/psbtv2.js.map +1 -0
  34. package/cjs/ledger/bitcoin_client/lib/varint.js +113 -0
  35. package/cjs/ledger/bitcoin_client/lib/varint.js.map +1 -0
  36. package/cjs/ledger/consts.js +7 -0
  37. package/cjs/ledger/consts.js.map +1 -0
  38. package/cjs/ledger/index.js +319 -0
  39. package/cjs/ledger/index.js.map +1 -0
  40. package/cjs/ledger/types.js +3 -0
  41. package/cjs/ledger/types.js.map +1 -0
  42. package/cjs/network-utils.js +76 -0
  43. package/cjs/network-utils.js.map +1 -0
  44. package/cjs/providers.js +270 -0
  45. package/cjs/providers.js.map +1 -0
  46. package/cjs/signers.js +64 -0
  47. package/cjs/signers.js.map +1 -0
  48. package/cjs/storage.js +30 -0
  49. package/cjs/storage.js.map +1 -0
  50. package/cjs/transactions/__tests__/integration.test.js +237 -0
  51. package/cjs/transactions/__tests__/integration.test.js.map +1 -0
  52. package/cjs/transactions/__tests__/syscoin.test.js +361 -0
  53. package/cjs/transactions/__tests__/syscoin.test.js.map +1 -0
  54. package/cjs/transactions/ethereum.js +1577 -0
  55. package/cjs/transactions/ethereum.js.map +1 -0
  56. package/cjs/transactions/index.js +19 -0
  57. package/cjs/transactions/index.js.map +1 -0
  58. package/cjs/transactions/syscoin.js +328 -0
  59. package/cjs/transactions/syscoin.js.map +1 -0
  60. package/cjs/trezor/index.js +718 -0
  61. package/cjs/trezor/index.js.map +1 -0
  62. package/cjs/types.js +12 -0
  63. package/cjs/types.js.map +1 -0
  64. package/cjs/utils/derivation-paths.js +99 -0
  65. package/cjs/utils/derivation-paths.js.map +1 -0
  66. package/cjs/utils/psbt.js +60 -0
  67. package/cjs/utils/psbt.js.map +1 -0
  68. package/cjs/utils.js +130 -0
  69. package/cjs/utils.js.map +1 -0
  70. package/package.json +46 -0
  71. package/types/errorUtils.d.ts +1 -0
  72. package/types/hardware-wallet-manager.d.ts +110 -0
  73. package/types/index.d.ts +12 -0
  74. package/types/initial-state.d.ts +79 -0
  75. package/types/keyring-manager.d.ts +184 -0
  76. package/types/ledger/bitcoin_client/index.d.ts +5 -0
  77. package/types/ledger/bitcoin_client/lib/appClient.d.ts +106 -0
  78. package/types/ledger/bitcoin_client/lib/bip32.d.ts +11 -0
  79. package/types/ledger/bitcoin_client/lib/buffertools.d.ts +28 -0
  80. package/types/ledger/bitcoin_client/lib/clientCommands.d.ts +77 -0
  81. package/types/ledger/bitcoin_client/lib/constants.d.ts +12 -0
  82. package/types/ledger/bitcoin_client/lib/merkelizedPsbt.d.ts +24 -0
  83. package/types/ledger/bitcoin_client/lib/merkle.d.ts +32 -0
  84. package/types/ledger/bitcoin_client/lib/merkleMap.d.ts +23 -0
  85. package/types/ledger/bitcoin_client/lib/policy.d.ts +36 -0
  86. package/types/ledger/bitcoin_client/lib/psbtv2.d.ts +167 -0
  87. package/types/ledger/bitcoin_client/lib/varint.d.ts +23 -0
  88. package/types/ledger/consts.d.ts +3 -0
  89. package/types/ledger/index.d.ts +51 -0
  90. package/types/ledger/types.d.ts +48 -0
  91. package/types/network-utils.d.ts +14 -0
  92. package/types/providers.d.ts +47 -0
  93. package/types/signers.d.ts +95 -0
  94. package/types/storage.d.ts +2 -0
  95. package/types/transactions/__tests__/integration.test.d.ts +1 -0
  96. package/types/transactions/__tests__/syscoin.test.d.ts +1 -0
  97. package/types/transactions/ethereum.d.ts +80 -0
  98. package/types/transactions/index.d.ts +2 -0
  99. package/types/transactions/syscoin.d.ts +61 -0
  100. package/types/trezor/index.d.ts +170 -0
  101. package/types/types.d.ts +294 -0
  102. package/types/utils/derivation-paths.d.ts +35 -0
  103. package/types/utils/psbt.d.ts +17 -0
  104. package/types/utils.d.ts +4 -0
@@ -0,0 +1,184 @@
1
+ import { INetwork, INetworkType } from '@sidhujag/sysweb3-network';
2
+ import { ethers } from 'ethers';
3
+ import { LedgerKeyring } from './ledger';
4
+ import { SyscoinHDSigner } from './signers';
5
+ import { TrezorKeyring } from './trezor';
6
+ import { IKeyringAccountState, ISyscoinTransactions, KeyringAccountType, IEthereumTransactions, IKeyringManager } from './types';
7
+ export interface ISysAccount {
8
+ address: string;
9
+ label?: string;
10
+ xprv?: string;
11
+ xpub: string;
12
+ }
13
+ /**
14
+ * Secure Buffer implementation for sensitive data
15
+ * Provides explicit memory clearing capability
16
+ */
17
+ declare class SecureBuffer {
18
+ private buffer;
19
+ private _isCleared;
20
+ constructor(data: string | Buffer);
21
+ get(): Buffer;
22
+ toString(): string;
23
+ clear(): void;
24
+ isCleared(): boolean;
25
+ }
26
+ export declare class KeyringManager implements IKeyringManager {
27
+ trezorSigner: TrezorKeyring;
28
+ ledgerSigner: LedgerKeyring;
29
+ initialTrezorAccountState: IKeyringAccountState;
30
+ initialLedgerAccountState: IKeyringAccountState;
31
+ utf8Error: boolean;
32
+ ethereumTransaction: IEthereumTransactions;
33
+ syscoinTransaction: ISyscoinTransactions;
34
+ private storage;
35
+ private getVaultState;
36
+ setVaultStateGetter: (getter: () => any) => void;
37
+ private getVault;
38
+ private getActiveChain;
39
+ private sessionPassword;
40
+ private sessionMnemonic;
41
+ constructor();
42
+ static createInitialized(seed: string, password: string, vaultStateGetter: () => any): Promise<KeyringManager>;
43
+ initialize(seed: string, password: string, network?: INetwork): Promise<IKeyringAccountState>;
44
+ setStorage: (client: any) => any;
45
+ validateAccountType: (account: IKeyringAccountState) => KeyringAccountType.HDAccount | KeyringAccountType.Imported;
46
+ isUnlocked: () => boolean;
47
+ lockWallet: () => void;
48
+ transferSessionTo: (targetKeyring: IKeyringManager) => void;
49
+ receiveSessionOwnership: (sessionPassword: SecureBuffer, sessionMnemonic: SecureBuffer) => void;
50
+ addNewAccount: (label?: string) => Promise<IKeyringAccountState>;
51
+ unlock(password: string): Promise<{
52
+ canLogin: boolean;
53
+ needsAccountCreation?: boolean;
54
+ }>;
55
+ getNewChangeAddress: () => Promise<string>;
56
+ getChangeAddress: (id: number) => Promise<string>;
57
+ getPubkey: (id: number, isChangeAddress: boolean) => Promise<string>;
58
+ getBip32Path: (id: number, isChangeAddress: boolean) => Promise<string>;
59
+ updateReceivingAddress: () => Promise<string>;
60
+ getAccountById: (id: number, accountType: KeyringAccountType) => Omit<IKeyringAccountState, "xprv">;
61
+ getPrivateKeyByAccountId: (id: number, accountType: KeyringAccountType, pwd: string) => Promise<string>;
62
+ getActiveAccount: () => {
63
+ activeAccount: Omit<IKeyringAccountState, "xprv">;
64
+ activeAccountType: KeyringAccountType;
65
+ };
66
+ getEncryptedXprv: (hd: SyscoinHDSigner) => string;
67
+ getSeed: (pwd: string) => Promise<any>;
68
+ setSignerNetwork: (network: INetwork) => Promise<{
69
+ activeChain?: INetworkType;
70
+ success: boolean;
71
+ }>;
72
+ forgetMainWallet: (pwd: string) => Promise<void>;
73
+ importWeb3Account: (mnemonicOrPrivKey: string) => ethers.Wallet;
74
+ getAccountXpub: () => string;
75
+ isSeedValid: (seedPhrase: string) => boolean;
76
+ createNewSeed: () => string;
77
+ getUTXOState: () => any;
78
+ importTrezorAccount(label?: string): Promise<IKeyringAccountState>;
79
+ importLedgerAccount(label?: string): Promise<IKeyringAccountState>;
80
+ getActiveUTXOAccountState: () => any;
81
+ getNetwork: () => any;
82
+ createEthAccount: (privateKey: string) => ethers.Wallet;
83
+ private fetchCurrentAccountData;
84
+ getAddress: (xpub: string, isChangeAddress: boolean) => Promise<string>;
85
+ getCurrentAddressPubkey: (xpub: string, isChangeAddress: boolean) => Promise<string>;
86
+ getCurrentAddressBip32Path: (xpub: string, isChangeAddress: boolean) => Promise<string>;
87
+ logout: () => void;
88
+ importAccount(privKey: string, label?: string): Promise<IKeyringAccountState>;
89
+ validateZprv(zprv: string, targetNetwork?: INetwork): {
90
+ isValid: boolean;
91
+ node: any;
92
+ network: {
93
+ bip32: {
94
+ public: number;
95
+ private: number;
96
+ };
97
+ messagePrefix: string;
98
+ bech32: string;
99
+ pubKeyHash: any;
100
+ scriptHash: any;
101
+ slip44: any;
102
+ wif: any;
103
+ };
104
+ message: string;
105
+ } | {
106
+ isValid: boolean;
107
+ message: any;
108
+ node?: undefined;
109
+ network?: undefined;
110
+ };
111
+ /**
112
+ * PRIVATE METHODS
113
+ */
114
+ private getDecryptedPrivateKey;
115
+ private getSigner;
116
+ private getReadOnlySigner;
117
+ private validateAndHandleErrorByMessage;
118
+ private getAccountsState;
119
+ /**
120
+ *
121
+ * @param password
122
+ * @param salt
123
+ * @returns hash: string
124
+ */
125
+ private encryptSHA512;
126
+ private getSysActivePrivateKey;
127
+ private getInitialAccountData;
128
+ private _createTrezorAccount;
129
+ private _createLedgerAccount;
130
+ private getFormattedBackendAccount;
131
+ private setLatestIndexesFromXPubTokens;
132
+ private createUTXOAccountAtIndex;
133
+ private addNewAccountToSyscoinChain;
134
+ private addNewAccountToEth;
135
+ private getNextAccountId;
136
+ private getBasicWeb3AccountInfo;
137
+ private setDerivedWeb3Accounts;
138
+ private setSignerEVM;
139
+ private clearTemporaryLocalKeys;
140
+ private recreateSessionFromVault;
141
+ private _getPrivateKeyAccountInfos;
142
+ /**
143
+ * Common method to decrypt mnemonic from session
144
+ * Eliminates code duplication across multiple methods
145
+ */
146
+ private getDecryptedMnemonic;
147
+ /**
148
+ * Creates network RPC config from current active network without making RPC calls
149
+ * Common utility for all on-demand signer creation
150
+ */
151
+ private createNetworkRpcConfig;
152
+ /**
153
+ * Common signer creation logic - takes decrypted mnemonic/zprv and creates fresh signer
154
+ * OPTIMIZED: No RPC call needed - uses network config directly
155
+ */
156
+ private createFreshUTXOSigner;
157
+ /**
158
+ * Creates a fresh UTXO signer for HD accounts derived from the main seed
159
+ * OPTIMIZED: No RPC call needed - uses network config directly
160
+ */
161
+ private createOnDemandUTXOSigner;
162
+ /**
163
+ * Creates a fresh UTXO signer for imported accounts from stored zprv
164
+ * OPTIMIZED: No RPC call needed - uses network config directly
165
+ */
166
+ private createOnDemandUTXOSignerFromImported;
167
+ /**
168
+ * Common method to create on-demand signer for active account
169
+ * Handles account type determination and delegates to appropriate method
170
+ */
171
+ private createOnDemandSignerForActiveAccount;
172
+ private isZprv;
173
+ initializeSession: (seedPhrase: string, password: string) => Promise<void>;
174
+ createFirstAccount: (label?: string) => Promise<IKeyringAccountState>;
175
+ initializeWalletSecurely: (seedPhrase: string, password: string) => Promise<IKeyringAccountState>;
176
+ private getSessionPasswordString;
177
+ private getSessionMnemonicString;
178
+ private generateNetworkAwareLabel;
179
+ /**
180
+ * Clean up all resources
181
+ */
182
+ destroy(): Promise<void>;
183
+ }
184
+ export {};
@@ -0,0 +1,5 @@
1
+ import AppClient, { PartialSignature } from './lib/appClient';
2
+ import { DefaultDescriptorTemplate, DefaultWalletPolicy, WalletPolicy } from './lib/policy';
3
+ import { PsbtV2 } from './lib/psbtv2';
4
+ export { AppClient, PsbtV2, DefaultDescriptorTemplate, DefaultWalletPolicy, PartialSignature, WalletPolicy, };
5
+ export default AppClient;
@@ -0,0 +1,106 @@
1
+ import Transport from '@ledgerhq/hw-transport';
2
+ import { WalletPolicy } from './policy';
3
+ import { PsbtV2 } from './psbtv2';
4
+ /**
5
+ * This class represents a partial signature produced by the app during signing.
6
+ * It always contains the `signature` and the corresponding `pubkey` whose private key
7
+ * was used for signing; in the case of taproot script paths, it also contains the
8
+ * tapleaf hash.
9
+ */
10
+ export declare class PartialSignature {
11
+ readonly pubkey: Buffer;
12
+ readonly signature: Buffer;
13
+ readonly tapleafHash?: Buffer;
14
+ constructor(pubkey: Buffer, signature: Buffer, tapleafHash?: Buffer);
15
+ }
16
+ /**
17
+ * This class encapsulates the APDU protocol documented at
18
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/master/doc/bitcoin.md
19
+ */
20
+ export declare class AppClient {
21
+ readonly transport: Transport;
22
+ constructor(transport: Transport);
23
+ private makeRequest;
24
+ /**
25
+ * Returns an object containing the currently running app's name, version and the device status flags.
26
+ *
27
+ * @returns an object with app name, version and device status flags.
28
+ */
29
+ getAppAndVersion(): Promise<{
30
+ flags: number | Buffer;
31
+ name: string;
32
+ version: string;
33
+ }>;
34
+ /**
35
+ * Requests the BIP-32 extended pubkey to the hardware wallet.
36
+ * If `display` is `false`, only standard paths will be accepted; an error is returned if an unusual path is
37
+ * requested.
38
+ * If `display` is `true`, the requested path is shown on screen for user verification; unusual paths can be
39
+ * requested, and a warning is shown to the user in that case.
40
+ *
41
+ * @param path the requested BIP-32 path as a string
42
+ * @param display `false` to silently retrieve a pubkey for a standard path, `true` to display the path on screen
43
+ * @returns the base58-encoded serialized extended pubkey (xpub)
44
+ */
45
+ getExtendedPubkey(path: string, display?: boolean): Promise<string>;
46
+ /**
47
+ * Registers a `WalletPolicy`, after interactive verification from the user.
48
+ * On success, after user's approval, this function returns the id (which is the same that can be computed with
49
+ * `walletPolicy.getid()`), followed by the 32-byte hmac. The client should store the hmac to use it for future
50
+ * requests to `getWalletAddress` or `signPsbt` using this `WalletPolicy`.
51
+ *
52
+ * @param walletPolicy the `WalletPolicy` to register
53
+ * @returns a pair of two 32-byte arrays: the id of the Wallet Policy, followed by the policy hmac
54
+ */
55
+ registerWallet(walletPolicy: WalletPolicy): Promise<readonly [Buffer, Buffer]>;
56
+ /**
57
+ * Returns the address of `walletPolicy` for the given `change` and `addressIndex`.
58
+ *
59
+ * @param walletPolicy the `WalletPolicy` to use
60
+ * @param walletHMAC the 32-byte hmac returned during wallet registration for a registered policy; otherwise
61
+ * `null` for a standard policy
62
+ * @param change `0` for a normal receive address, `1` for a change address
63
+ * @param addressIndex the address index to retrieve
64
+ * @param display `True` to show the address on screen, `False` to retrieve it silently
65
+ * @returns the address, as an ascii string.
66
+ */
67
+ getWalletAddress(walletPolicy: WalletPolicy, walletHMAC: Buffer | null, change: number, addressIndex: number, display: boolean): Promise<string>;
68
+ /**
69
+ * Signs a psbt using a (standard or registered) `WalletPolicy`. This is an interactive command, as user validation
70
+ * is necessary using the device's secure screen.
71
+ * On success, a map of input indexes and signatures is returned.
72
+ * @param psbt a base64-encoded string, or a psbt in a binary Buffer. Using the `PsbtV2` type is deprecated.
73
+ * @param walletPolicy the `WalletPolicy` to use for signing
74
+ * @param walletHMAC the 32-byte hmac obtained during wallet policy registration, or `null` for a standard policy
75
+ * @param progressCallback optionally, a callback that will be called every time a signature is produced during
76
+ * the signing process. The callback does not receive any argument, but can be used to track progress.
77
+ * @returns an array of of tuples with 2 elements containing:
78
+ * - the index of the input being signed;
79
+ * - an instance of PartialSignature
80
+ */
81
+ signPsbt(psbt: PsbtV2 | string | Buffer, walletPolicy: WalletPolicy, walletHMAC: Buffer | null, progressCallback?: () => void): Promise<[number, PartialSignature][]>;
82
+ /**
83
+ * Returns the fingerprint of the master public key, as per BIP-32 standard.
84
+ * @returns the master key fingerprint as a string of 8 hexadecimal digits.
85
+ */
86
+ getMasterFingerprint(): Promise<string>;
87
+ /**
88
+ * Signs a message using the legacy Bitcoin Message Signing standard. The signed message is
89
+ * the double-sha256 hash of the concatenation of:
90
+ * - "\x18Bitcoin Signed Message:\n";
91
+ * - the length of `message`, encoded as a Bitcoin-style variable length integer;
92
+ * - `message`.
93
+ *
94
+ * @param message the serialized message to sign
95
+ * @param path the BIP-32 path of the key used to sign the message
96
+ * @returns base64-encoded signature of the message.
97
+ */
98
+ signMessage(message: Buffer, path: string): Promise<string>;
99
+ private validateAddress;
100
+ /**
101
+ * Maps Ledger app names to network configurations
102
+ * This allows support for multiple UTXO networks beyond just Syscoin
103
+ */
104
+ private getNetworkFromAppName;
105
+ }
106
+ export default AppClient;
@@ -0,0 +1,11 @@
1
+ export declare function pathElementsToBuffer(paths: readonly number[]): Buffer;
2
+ export declare function bip32asBuffer(path: string): Buffer;
3
+ export declare function pathArrayToString(pathElements: readonly number[]): string;
4
+ export declare function pathStringToArray(path: string): readonly number[];
5
+ export declare function pubkeyFromXpub(xpub: string): Buffer;
6
+ export declare function getXpubComponents(xpub: string): {
7
+ readonly chaincode: Buffer;
8
+ readonly pubkey: Buffer;
9
+ readonly version: number;
10
+ };
11
+ export declare function hardenedPathOf(pathElements: readonly number[]): readonly number[];
@@ -0,0 +1,28 @@
1
+ export declare function unsafeTo64bitLE(n: number): Buffer;
2
+ export declare function unsafeFrom64bitLE(byteArray: Buffer): number;
3
+ export declare class BufferWriter {
4
+ private bufs;
5
+ write(alloc: number, fn: (b: Buffer) => void): void;
6
+ writeUInt8(i: number): void;
7
+ writeInt32(i: number): void;
8
+ writeUInt32(i: number): void;
9
+ writeUInt64(i: number): void;
10
+ writeVarInt(i: number): void;
11
+ writeSlice(slice: Buffer): void;
12
+ writeVarSlice(slice: Buffer): void;
13
+ buffer(): Buffer;
14
+ }
15
+ export declare class BufferReader {
16
+ readonly buffer: Buffer;
17
+ offset: number;
18
+ constructor(buffer: Buffer, offset?: number);
19
+ available(): number;
20
+ readUInt8(): number;
21
+ readInt32(): number;
22
+ readUInt32(): number;
23
+ readUInt64(): number;
24
+ readVarInt(): bigint;
25
+ readSlice(n: number): Buffer;
26
+ readVarSlice(): Buffer;
27
+ readVector(): readonly Buffer[];
28
+ }
@@ -0,0 +1,77 @@
1
+ import { Merkle } from './merkle';
2
+ import { MerkleMap } from './merkleMap';
3
+ import { WalletPolicy } from './policy';
4
+ declare enum ClientCommandCode {
5
+ YIELD = 16,
6
+ GET_PREIMAGE = 64,
7
+ GET_MERKLE_LEAF_PROOF = 65,
8
+ GET_MERKLE_LEAF_INDEX = 66,
9
+ GET_MORE_ELEMENTS = 160
10
+ }
11
+ declare abstract class ClientCommand {
12
+ abstract code: ClientCommandCode;
13
+ abstract execute(request: Buffer): Buffer;
14
+ }
15
+ export declare class YieldCommand extends ClientCommand {
16
+ private readonly progressCallback?;
17
+ private results;
18
+ readonly code = ClientCommandCode.YIELD;
19
+ constructor(results: Buffer[], progressCallback?: (() => void) | undefined);
20
+ execute(request: Buffer): Buffer;
21
+ }
22
+ export declare class GetPreimageCommand extends ClientCommand {
23
+ private readonly known_preimages;
24
+ private queue;
25
+ readonly code = ClientCommandCode.GET_PREIMAGE;
26
+ constructor(known_preimages: ReadonlyMap<string, Buffer>, queue: Buffer[]);
27
+ execute(request: Buffer): Buffer;
28
+ }
29
+ export declare class GetMerkleLeafProofCommand extends ClientCommand {
30
+ private readonly known_trees;
31
+ private queue;
32
+ readonly code = ClientCommandCode.GET_MERKLE_LEAF_PROOF;
33
+ constructor(known_trees: ReadonlyMap<string, Merkle>, queue: Buffer[]);
34
+ execute(request: Buffer): Buffer;
35
+ }
36
+ export declare class GetMerkleLeafIndexCommand extends ClientCommand {
37
+ private readonly known_trees;
38
+ readonly code = ClientCommandCode.GET_MERKLE_LEAF_INDEX;
39
+ constructor(known_trees: ReadonlyMap<string, Merkle>);
40
+ execute(request: Buffer): Buffer;
41
+ }
42
+ export declare class GetMoreElementsCommand extends ClientCommand {
43
+ queue: Buffer[];
44
+ readonly code = ClientCommandCode.GET_MORE_ELEMENTS;
45
+ constructor(queue: Buffer[]);
46
+ execute(request: Buffer): Buffer;
47
+ }
48
+ /**
49
+ * This class will dispatch a client command coming from the hardware device to
50
+ * the appropriate client command implementation. Those client commands
51
+ * typically requests data from a merkle tree or merkelized maps.
52
+ *
53
+ * A ClientCommandInterpreter is prepared by adding the merkle trees and
54
+ * merkelized maps it should be able to serve to the hardware device. This class
55
+ * doesn't know anything about the semantics of the data it holds, it just
56
+ * serves merkle data. It doesn't even know in what context it is being
57
+ * executed, ie SignPsbt, getWalletAddress, etc.
58
+ *
59
+ * If the command yelds results to the client, as signPsbt does, the yielded
60
+ * data will be accessible after the command completed by calling getYielded(),
61
+ * which will return the yields in the same order as they came in.
62
+ */
63
+ export declare class ClientCommandInterpreter {
64
+ private readonly roots;
65
+ private readonly preimages;
66
+ private yielded;
67
+ private queue;
68
+ private readonly commands;
69
+ constructor(progressCallback?: () => void);
70
+ getYielded(): readonly Buffer[];
71
+ addKnownPreimage(preimage: Buffer): void;
72
+ addKnownList(elements: readonly Buffer[]): void;
73
+ addKnownMapping(mm: MerkleMap): void;
74
+ addKnownWalletPolicy(wp: WalletPolicy): void;
75
+ execute(request: Buffer): Buffer;
76
+ }
77
+ export {};
@@ -0,0 +1,12 @@
1
+ export declare const MAX_SCRIPT_BLOCK = 50;
2
+ export declare const DEFAULT_VERSION = 1;
3
+ export declare const DEFAULT_LOCKTIME = 0;
4
+ export declare const DEFAULT_SEQUENCE = 4294967295;
5
+ export declare const SIGHASH_ALL = 1;
6
+ export declare const OP_DUP = 118;
7
+ export declare const OP_HASH160 = 169;
8
+ export declare const HASH_SIZE = 20;
9
+ export declare const OP_EQUAL = 135;
10
+ export declare const OP_EQUALVERIFY = 136;
11
+ export declare const OP_CHECKSIG = 172;
12
+ export declare const OP_RETURN = 106;
@@ -0,0 +1,24 @@
1
+ import { MerkleMap } from './merkleMap';
2
+ import { PsbtV2 } from './psbtv2';
3
+ /**
4
+ * This class merkelizes a PSBTv2, by merkelizing the different
5
+ * maps of the psbt. This is used during the transaction signing process,
6
+ * where the hardware app can request specific parts of the psbt from the
7
+ * client code and be sure that the response data actually belong to the psbt.
8
+ * The reason for this is the limited amount of memory available to the app,
9
+ * so it can't always store the full psbt in memory.
10
+ *
11
+ * The signing process is documented at
12
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/master/doc/bitcoin.md#sign_psbt
13
+ */
14
+ export declare class MerkelizedPsbt extends PsbtV2 {
15
+ readonly globalMerkleMap: MerkleMap;
16
+ inputMerkleMaps: MerkleMap[];
17
+ outputMerkleMaps: MerkleMap[];
18
+ inputMapCommitments: Buffer[];
19
+ outputMapCommitments: Buffer[];
20
+ constructor(psbt: PsbtV2);
21
+ getGlobalSize(): number;
22
+ getGlobalKeysValuesRoot(): Buffer;
23
+ private static createMerkleMap;
24
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * This class implements the merkle tree used by Ledger Bitcoin app v2+,
3
+ * which is documented at
4
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/master/doc/merkle.md
5
+ */
6
+ export declare class Merkle {
7
+ private leaves;
8
+ private rootNode;
9
+ private leafNodes;
10
+ private h;
11
+ constructor(leaves: Buffer[], hasher?: (buf: Buffer) => Buffer);
12
+ getRoot(): Buffer;
13
+ size(): number;
14
+ getLeaves(): Buffer[];
15
+ getLeafHash(index: number): Buffer;
16
+ getProof(index: number): Buffer[];
17
+ calculateRoot(leaves: Buffer[]): {
18
+ leaves: Node[];
19
+ root: Node;
20
+ };
21
+ hashNode(left: Buffer, right: Buffer): Buffer;
22
+ }
23
+ export declare function hashLeaf(buf: Buffer, hashFunction?: (buf: Buffer) => Buffer): Buffer;
24
+ declare class Node {
25
+ leftChild?: Node;
26
+ rightChild?: Node;
27
+ parent?: Node;
28
+ hash: Buffer;
29
+ constructor(left: Node | undefined, right: Node | undefined, hash: Buffer);
30
+ isLeaf(): boolean;
31
+ }
32
+ export {};
@@ -0,0 +1,23 @@
1
+ import { Merkle } from './merkle';
2
+ /**
3
+ * This implements "Merkelized Maps", documented at
4
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/master/doc/merkle.md#merkleized-maps
5
+ *
6
+ * A merkelized map consist of two merkle trees, one for the keys of
7
+ * a map and one for the values of the same map, thus the two merkle
8
+ * trees have the same shape. The commitment is the number elements
9
+ * in the map followed by the keys' merkle root followed by the
10
+ * values' merkle root.
11
+ */
12
+ export declare class MerkleMap {
13
+ readonly keys: readonly Buffer[];
14
+ readonly keysTree: Merkle;
15
+ readonly values: readonly Buffer[];
16
+ readonly valuesTree: Merkle;
17
+ /**
18
+ * @param keys Sorted list of (unhashed) keys
19
+ * @param values values, in corresponding order as the keys, and of equal length
20
+ */
21
+ constructor(keys: readonly Buffer[], values: readonly Buffer[]);
22
+ commitment(): Buffer;
23
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The Bitcon hardware app uses a descriptors-like thing to describe
3
+ * how to construct output scripts from keys. A "Wallet Policy" consists
4
+ * of a "Descriptor Template" and a list of "keys". A key is basically
5
+ * a serialized BIP32 extended public key with some added derivation path
6
+ * information. This is documented at
7
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/master/doc/wallet.md
8
+ */
9
+ export declare class WalletPolicy {
10
+ readonly name: string;
11
+ readonly descriptorTemplate: string;
12
+ readonly keys: readonly string[];
13
+ /**
14
+ * Creates and instance of a wallet policy.
15
+ * @param name an ascii string, up to 16 bytes long; it must be an empty string for default wallet policies
16
+ * @param descriptorTemplate the wallet policy template
17
+ * @param keys and array of the keys, with the key derivation information
18
+ */
19
+ constructor(name: string, descriptorTemplate: string, keys: readonly string[]);
20
+ /**
21
+ * Returns the unique 32-bytes id of this wallet policy.
22
+ */
23
+ getId(): Buffer;
24
+ /**
25
+ * Serializes the wallet policy for transmission via the hardware wallet protocol.
26
+ * @returns the serialized wallet policy
27
+ */
28
+ serialize(): Buffer;
29
+ }
30
+ export type DefaultDescriptorTemplate = 'pkh(@0/**)' | 'sh(wpkh(@0/**))' | 'wpkh(@0/**)' | 'tr(@0/**)';
31
+ /**
32
+ * Simplified class to handle default wallet policies that can be used without policy registration.
33
+ */
34
+ export declare class DefaultWalletPolicy extends WalletPolicy {
35
+ constructor(descriptorTemplate: DefaultDescriptorTemplate, key: string);
36
+ }