-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsigner.ts
186 lines (164 loc) · 5.2 KB
/
signer.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
import type {
Network,
SignOnlySigner,
SignedTx,
Signer,
UnsignedTransaction,
} from '@wormhole-foundation/sdk-connect';
import {
PlatformNativeSigner,
chainToPlatform,
isNativeSigner,
} from '@wormhole-foundation/sdk-connect';
import type {
Signer as EthersSigner,
Provider,
TransactionRequest,
} from 'ethers';
import { NonceManager, Wallet } from 'ethers';
import { EvmPlatform } from './platform.js';
import type { EvmChains } from './types.js';
import { _platform } from './types.js';
export type EvmSignerOptions = {
// Whether or not to log messages
debug?: boolean;
// Override gas limit
gasLimit?: bigint;
// Do not exceed this gas limit
maxGasLimit?: bigint;
// Partially override specific transaction request fields
overrides?: Partial<TransactionRequest>;
};
export async function getEvmSigner(
rpc: Provider,
key: string | EthersSigner,
opts?: EvmSignerOptions & { chain?: EvmChains },
): Promise<Signer> {
const signer: EthersSigner =
typeof key === 'string' ? new Wallet(key, rpc) : key;
const chain = opts?.chain ?? (await EvmPlatform.chainFromRpc(rpc))[1];
const managedSigner = new NonceManager(signer);
if (managedSigner.provider === null) {
try {
managedSigner.connect(rpc);
} catch (e) {
console.error('Cannot connect to network for signer', e);
}
}
return new EvmNativeSigner(
chain,
await signer.getAddress(),
managedSigner,
opts,
);
}
// Get a SignOnlySigner for the EVM platform
export async function getEvmSignerForKey(
rpc: Provider,
privateKey: string,
): Promise<Signer> {
return getEvmSigner(rpc, privateKey);
}
// Get a SignOnlySigner for the EVM platform
export async function getEvmSignerForSigner(
signer: EthersSigner,
): Promise<Signer> {
if (!signer.provider) throw new Error('Signer must have a provider');
return getEvmSigner(signer.provider!, signer, {});
}
export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
extends PlatformNativeSigner<EthersSigner, N, C>
implements SignOnlySigner<N, C>
{
constructor(
_chain: C,
_address: string,
_signer: EthersSigner,
readonly opts?: EvmSignerOptions,
) {
super(_chain, _address, _signer);
}
chain(): C {
return this._chain;
}
address(): string {
return this._address;
}
async sign(tx: UnsignedTransaction<N, C>[]): Promise<SignedTx[]> {
const chain = this.chain();
const signed = [];
// Default gas values
let gasLimit = 500_000n;
let gasPrice = 100_000_000_000n; // 100gwei
let maxFeePerGas = 1_500_000_000n; // 1.5gwei
let maxPriorityFeePerGas = 100_000_000n; // 0.1gwei
// If no overrides were passed, we can get better
// gas values from the provider
if (this.opts?.overrides === undefined) {
// Celo does not support this call
if (chain !== 'Celo') {
const feeData = await this._signer.provider!.getFeeData();
gasPrice = feeData.gasPrice ?? gasPrice;
maxFeePerGas = feeData.maxFeePerGas ?? maxFeePerGas;
maxPriorityFeePerGas =
feeData.maxPriorityFeePerGas ?? maxPriorityFeePerGas;
}
}
if (this.opts?.gasLimit !== undefined) {
gasLimit = this.opts.gasLimit;
}
if (this.opts?.maxGasLimit !== undefined) {
// why doesnt math.min work for bigints?
gasLimit =
gasLimit > this.opts?.maxGasLimit ? this.opts?.maxGasLimit : gasLimit;
}
// Oasis throws malformed errors unless we
// set it to use legacy transaction parameters
const gasOpts =
chain === 'Oasis'
? { gasLimit, gasPrice, type: 0 } // Hardcoded to legacy transaction type
: { gasLimit, maxFeePerGas, maxPriorityFeePerGas };
for (const txn of tx) {
const { transaction, description } = txn;
if (this.opts?.debug)
console.log(`Signing: ${description} for ${this.address()}`);
const t: TransactionRequest = {
...transaction,
...gasOpts,
from: this.address(),
nonce: await this._signer.getNonce(),
// Override any existing values with those passed in the constructor
...this.opts?.overrides,
};
signed.push(await this._signer.signTransaction(t));
}
return signed;
}
}
export function isEvmNativeSigner<N extends Network>(
signer: Signer<N>,
): signer is EvmNativeSigner<N> {
return (
isNativeSigner(signer) &&
chainToPlatform(signer.chain()) === _platform &&
isEthersSigner(signer.unwrap())
);
}
// No type guard provided by ethers, instanceof checks will fail on even slightly different versions of ethers
function isEthersSigner(thing: any): thing is EthersSigner {
return (
'provider' in thing &&
typeof thing.connect === 'function' &&
typeof thing.getAddress === 'function' &&
typeof thing.getNonce === 'function' &&
typeof thing.populateCall === 'function' &&
typeof thing.populateTransaction === 'function' &&
typeof thing.estimateGas === 'function' &&
typeof thing.call === 'function' &&
typeof thing.resolveName === 'function' &&
typeof thing.signTransaction === 'function' &&
typeof thing.sendTransaction === 'function' &&
typeof thing.signMessage === 'function' &&
typeof thing.signTypedData === 'function'
);
}