aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/types.ts
blob: ed3aea176652760103598e807aaf0961ff70fc9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { ECSignature } from '@0x/types';
import { JSONRPCRequestPayload } from 'ethereum-types';
import HDNode = require('hdkey');

export interface LedgerCommunicationClient {
    close: () => Promise<void>;
}

/**
 * Elliptic Curve signature
 * The LedgerEthereumClient sends Ethereum-specific requests to the Ledger Nano S
 * It uses an internal LedgerCommunicationClient to relay these requests. Currently
 * NodeJs and Browser communication are supported.
 */
export interface LedgerEthereumClient {
    // shouldGetChainCode is defined as `true` instead of `boolean` because other types rely on the assumption
    // that we get back the chain code and we don't have dependent types to express it properly
    getAddress: (
        derivationPath: string,
        askForDeviceConfirmation: boolean,
        shouldGetChainCode: true,
    ) => Promise<LedgerGetAddressResult>;
    signTransaction: (derivationPath: string, rawTxHex: string) => Promise<ECSignatureString>;
    signPersonalMessage: (derivationPath: string, messageHex: string) => Promise<ECSignature>;
    transport: LedgerCommunicationClient;
}

export interface ECSignatureString {
    v: string;
    r: string;
    s: string;
}

export type LedgerEthereumClientFactoryAsync = () => Promise<LedgerEthereumClient>;

/**
 * networkId: The ethereum networkId to set as the chainId from EIP155
 * ledgerConnectionType: Environment in which you wish to connect to Ledger (nodejs or browser)
 * derivationPath: Initial derivation path to use e.g 44'/60'/0'
 * accountFetchingConfigs: configs related to fetching accounts from a Ledger
 */
export interface LedgerSubproviderConfigs {
    networkId: number;
    ledgerEthereumClientFactoryAsync: LedgerEthereumClientFactoryAsync;
    baseDerivationPath?: string;
    accountFetchingConfigs?: AccountFetchingConfigs;
}

/**
 * addressSearchLimit: The maximum number of addresses to search through, defaults to 1000
 * numAddressesToReturn: Number of addresses to return from 'eth_accounts' call
 * shouldAskForOnDeviceConfirmation: Whether you wish to prompt the user on their Ledger
 *                                   before fetching their addresses
 */
export interface AccountFetchingConfigs {
    addressSearchLimit?: number;
    numAddressesToReturn?: number;
    shouldAskForOnDeviceConfirmation?: boolean;
}

/**
 * mnemonic: The string mnemonic seed
 * addressSearchLimit: The maximum number of addresses to search through, defaults to 1000
 * baseDerivationPath: The base derivation path (e.g 44'/60'/0'/0)
 */
export interface MnemonicWalletSubproviderConfigs {
    mnemonic: string;
    addressSearchLimit?: number;
    baseDerivationPath?: string;
}

export interface SignatureData {
    hash: string;
    r: string;
    s: string;
    v: number;
}

export interface LedgerGetAddressResult {
    address: string;
    publicKey: string;
    chainCode: string;
}

export interface PartialTxParams {
    nonce: string;
    gasPrice?: string;
    gas: string;
    to: string;
    from: string;
    value?: string;
    data?: string;
    chainId: number; // EIP 155 chainId - mainnet: 1, ropsten: 3
}

export type DoneCallback = (err?: Error) => void;

export interface LedgerCommunication {
    close_async: () => Promise<void>;
}

export interface ResponseWithTxParams {
    raw: string;
    tx: PartialTxParams;
}

export enum WalletSubproviderErrors {
    AddressNotFound = 'ADDRESS_NOT_FOUND',
    DataMissingForSignPersonalMessage = 'DATA_MISSING_FOR_SIGN_PERSONAL_MESSAGE',
    DataMissingForSignTypedData = 'DATA_MISSING_FOR_SIGN_TYPED_DATA',
    SenderInvalidOrNotSupplied = 'SENDER_INVALID_OR_NOT_SUPPLIED',
    FromAddressMissingOrInvalid = 'FROM_ADDRESS_MISSING_OR_INVALID',
    MethodNotSupported = 'METHOD_NOT_SUPPORTED',
}
export enum LedgerSubproviderErrors {
    TooOldLedgerFirmware = 'TOO_OLD_LEDGER_FIRMWARE',
    MultipleOpenConnectionsDisallowed = 'MULTIPLE_OPEN_CONNECTIONS_DISALLOWED',
}

export enum NonceSubproviderErrors {
    EmptyParametersFound = 'EMPTY_PARAMETERS_FOUND',
    CannotDetermineAddressFromPayload = 'CANNOT_DETERMINE_ADDRESS_FROM_PAYLOAD',
}
export interface DerivedHDKeyInfo {
    address: string;
    baseDerivationPath: string;
    derivationPath: string;
    hdKey: HDNode;
}

export type ErrorCallback = (err: Error | null, data?: any) => void;
export type Callback = () => void;
export type OnNextCompleted = (err: Error | null, result: any, cb: Callback) => void;
export type NextCallback = (callback?: OnNextCompleted) => void;

export interface JSONRPCRequestPayloadWithMethod extends JSONRPCRequestPayload {
    method: string;
}