-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtypes.ts
273 lines (235 loc) · 7.67 KB
/
types.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import type * as Crypto from '@cardano-sdk/crypto';
import type { Cardano, HandleResolution, Serialization } from '@cardano-sdk/core';
import type { Cip30DataSignature } from '@cardano-sdk/dapp-connector';
import type { Cip30SignDataRequest, Cip8SignDataContext } from './cip8';
import type { HexBlob, OpaqueString, Shutdown } from '@cardano-sdk/util';
import type { Logger } from 'ts-log';
import type { Runtime } from 'webextension-polyfill';
export type MessageSender = Runtime.MessageSender;
export interface SignBlobResult {
publicKey: Crypto.Ed25519PublicKeyHex;
signature: Crypto.Ed25519SignatureHex;
}
export enum CardanoKeyConst {
PURPOSE = 1852,
COIN_TYPE = 1815
}
export enum Cip1852PathLevelIndexes {
PURPOSE = 0,
COIN_TYPE = 1,
ACCOUNT = 2,
ROLE = 3,
// address
INDEX = 4
}
export enum KeyAgentType {
InMemory = 'InMemory',
Ledger = 'Ledger',
Trezor = 'Trezor'
}
export enum KeyRole {
External = 0,
Internal = 1,
Stake = 2,
DRep = 3
}
export enum KeyPurpose {
STANDARD = 1852,
MULTI_SIG = 1854
}
export interface AccountKeyDerivationPath {
role: KeyRole;
index: number;
}
/** Internal = change address & External = receipt address */
export enum AddressType {
/** Change address */
Internal = 1,
/** Receipt address */
External = 0
}
export enum DeviceType {
Ledger = 'Ledger'
}
export enum CommunicationType {
Web = 'web',
Node = 'node'
}
export interface KeyAgentDependencies {
logger: Logger;
bip32Ed25519: Crypto.Bip32Ed25519;
}
export interface AccountAddressDerivationPath {
type: AddressType;
index: number;
}
export interface GroupedAddress {
type: AddressType;
index: number;
networkId: Cardano.NetworkId;
accountIndex: number;
address: Cardano.PaymentAddress;
rewardAccount: Cardano.RewardAccount;
stakeKeyDerivationPath?: AccountKeyDerivationPath;
}
export interface TrezorConfig {
communicationType: CommunicationType;
silentMode?: boolean;
lazyLoad?: boolean;
manifest: {
email: string;
appUrl: string;
};
/** When set to true, Trezor automatically handle passphrase entry by forcing it to occur on the device */
shouldHandlePassphrase?: boolean;
}
export interface SerializableKeyAgentDataBase {
chainId: Cardano.ChainId;
accountIndex: number;
extendedAccountPublicKey: Crypto.Bip32PublicKeyHex;
purpose?: KeyPurpose;
}
export interface SerializableInMemoryKeyAgentData extends SerializableKeyAgentDataBase {
__typename: KeyAgentType.InMemory;
encryptedRootPrivateKeyBytes: number[];
}
export interface SerializableLedgerKeyAgentData extends SerializableKeyAgentDataBase {
__typename: KeyAgentType.Ledger;
communicationType: CommunicationType;
}
export interface SerializableTrezorKeyAgentData extends SerializableKeyAgentDataBase {
__typename: KeyAgentType.Trezor;
trezorConfig: TrezorConfig;
}
export type SerializableKeyAgentData =
| SerializableInMemoryKeyAgentData
| SerializableLedgerKeyAgentData
| SerializableTrezorKeyAgentData;
export interface KeyPair {
skey: Crypto.Bip32PrivateKeyHex;
vkey: Crypto.Bip32PublicKeyHex;
}
export interface Ed25519KeyPair {
skey: Crypto.Ed25519PrivateNormalKeyHex | Crypto.Ed25519PrivateExtendedKeyHex;
vkey: Crypto.Ed25519PublicKeyHex;
}
/**
* @returns passphrase used to decrypt root private key
*/
export type GetPassphrase = (noCache?: true) => Promise<Uint8Array>;
export type TxInId = OpaqueString<'TxInId'>;
export const TxInId = ({ txId, index }: Cardano.TxIn) => `${txId}_${index}` as TxInId;
export type TxInKeyPathMap = Partial<Record<TxInId, AccountKeyDerivationPath>>;
export type RewardAccountKeyPathMap = Partial<Record<Cardano.RewardAccount, AccountKeyDerivationPath>>;
export type KeyHashKeyPathMap = Partial<Record<Crypto.Ed25519KeyHashHex, AccountKeyDerivationPath>>;
/** The result of the transaction signer signing operation. */
export type TransactionSignerResult = {
/** The public key matching the private key that generate the signature. */
pubKey: Crypto.Ed25519PublicKeyHex;
/** The transaction signature. */
signature: Crypto.Ed25519SignatureHex;
};
/** Produces a Ed25519Signature of a transaction. */
export interface TransactionSigner {
/**
* Sings a transaction.
*
* @param txBody The transaction to be signed.
* @returns A Ed25519 transaction signature.
*/
sign(txBody: Serialization.TransactionBody): Promise<TransactionSignerResult>;
}
export interface SignTransactionOptions {
additionalKeyPaths?: AccountKeyDerivationPath[];
extraSigners?: TransactionSigner[];
stubSign?: boolean;
}
export interface SignTransactionContext {
txInKeyPathMap: TxInKeyPathMap;
knownAddresses: GroupedAddress[];
handleResolutions?: HandleResolution[];
dRepKeyHashHex?: Crypto.Ed25519KeyHashHex;
sender?: MessageSender;
scripts?: Cardano.Script[];
}
export type SignDataContext = Cip8SignDataContext & { sender?: MessageSender };
export interface KeyAgent {
get chainId(): Cardano.ChainId;
get accountIndex(): number;
get serializableData(): SerializableKeyAgentData;
get extendedAccountPublicKey(): Crypto.Bip32PublicKeyHex;
get bip32Ed25519(): Crypto.Bip32Ed25519;
get purpose(): KeyPurpose | undefined;
/**
* @throws AuthenticationError
*/
signBlob(derivationPath: AccountKeyDerivationPath, blob: HexBlob): Promise<SignBlobResult>;
/**
* @throws AuthenticationError
*/
signCip8Data(request: Cip8SignDataContext): Promise<Cip30DataSignature>;
/**
* @throws AuthenticationError
*/
signTransaction(
txBody: Serialization.TransactionBody,
context: SignTransactionContext,
options?: SignTransactionOptions
): Promise<Cardano.Signatures>;
/**
* @throws AuthenticationError
*/
derivePublicKey(derivationPath: AccountKeyDerivationPath): Promise<Crypto.Ed25519PublicKeyHex>;
/**
* Derives an address from the given payment key and stake key derivation path.
*
* @param paymentKeyDerivationPath The payment key derivation path.
* @param stakeKeyDerivationIndex The stake key index. This field is optional. If not provided it defaults to index 0.
* @param pure If set to true, the key agent will derive a new address without mutating its internal state.
*/
deriveAddress(
paymentKeyDerivationPath: AccountAddressDerivationPath,
stakeKeyDerivationIndex: number
): Promise<GroupedAddress>;
/**
* @throws AuthenticationError
*/
exportRootPrivateKey(): Promise<Crypto.Bip32PrivateKeyHex>;
}
export type AsyncKeyAgent = Pick<
KeyAgent,
'deriveAddress' | 'derivePublicKey' | 'signBlob' | 'signCip8Data' | 'signTransaction'
> & {
getChainId(): Promise<Cardano.ChainId>;
getBip32Ed25519(): Promise<Crypto.Bip32Ed25519>;
getExtendedAccountPublicKey(): Promise<Crypto.Bip32PublicKeyHex>;
getAccountIndex(): Promise<number>;
} & Shutdown;
export type WitnessOptions = SignTransactionOptions;
export interface WitnessedTx {
cbor: Serialization.TxCBOR;
tx: Cardano.Tx;
context: {
handleResolutions: HandleResolution[];
};
}
/** Interface for an entity capable of generating witness data for a transaction. */
export interface Witnesser {
/**
* Generates the witness data for a given transaction.
*
* @param transaction The transaction along with its hash for which the witness data is to be generated.
* @param context The witness sign transaction context
* @param options Optional additional parameters that may influence how the witness data is generated.
* @returns A promise that resolves to the transaction with the generated witness data.
*/
witness(
transaction: Serialization.Transaction,
context: SignTransactionContext,
options?: WitnessOptions
): Promise<WitnessedTx>;
/**
* @throws AuthenticationError
*/
signData(props: Cip30SignDataRequest): Promise<Cip30DataSignature>;
}