2020-01-10 02:50:09 -05:00
|
|
|
"use strict";
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
import { ethers } from "ethers";
|
|
|
|
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new ethers.utils.Logger(version);
|
2020-01-10 02:50:09 -05:00
|
|
|
|
|
|
|
import Eth from "@ledgerhq/hw-app-eth";
|
|
|
|
|
|
|
|
// We store these in a separated import so it is easier to swap them out
|
|
|
|
// at bundle time; browsers do not get HID, for example. This maps a string
|
|
|
|
// "type" to a Transport with create.
|
|
|
|
import { transports } from "./ledger-transport";
|
|
|
|
|
|
|
|
const defaultPath = "m/44'/60'/0'/0/0";
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
function waiter(duration: number): Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, duration);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LedgerSigner extends ethers.Signer {
|
2020-01-10 02:50:09 -05:00
|
|
|
readonly type: string;
|
|
|
|
readonly path: string
|
|
|
|
|
|
|
|
readonly _eth: Promise<Eth>;
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
constructor(provider?: ethers.providers.Provider, type?: string, path?: string) {
|
2020-01-10 02:50:09 -05:00
|
|
|
super();
|
|
|
|
if (path == null) { path = defaultPath; }
|
|
|
|
if (type == null) { type = "default"; }
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
ethers.utils.defineReadOnly(this, "path", path);
|
|
|
|
ethers.utils.defineReadOnly(this, "type", type);
|
|
|
|
ethers.utils.defineReadOnly(this, "provider", provider || null);
|
2020-01-10 02:50:09 -05:00
|
|
|
|
|
|
|
const transport = transports[type];
|
2020-04-22 02:42:25 -04:00
|
|
|
if (!transport) { logger.throwArgumentError("unknown or unsupported type", "type", type); }
|
2020-01-10 02:50:09 -05:00
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
ethers.utils.defineReadOnly(this, "_eth", transport.create().then((transport) => {
|
2020-01-10 02:50:09 -05:00
|
|
|
const eth = new Eth(transport);
|
|
|
|
return eth.getAppConfiguration().then((config) => {
|
|
|
|
return eth;
|
|
|
|
}, (error) => {
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
|
|
|
}, (error) => {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
_retry<T = any>(callback: (eth: Eth) => Promise<T>, timeout?: number): Promise<T> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
if (timeout && timeout > 0) {
|
|
|
|
setTimeout(() => { reject(new Error("timeout")); }, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
const eth = await this._eth;
|
|
|
|
|
|
|
|
// Wait up to 5 seconds
|
|
|
|
for (let i = 0; i < 50; i++) {
|
|
|
|
try {
|
|
|
|
const result = await callback(eth);
|
|
|
|
return resolve(result);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.id !== "TransportLocked") {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await waiter(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reject(new Error("timeout"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:50:09 -05:00
|
|
|
async getAddress(): Promise<string> {
|
2020-01-10 19:59:20 -05:00
|
|
|
const account = await this._retry((eth) => eth.getAddress(this.path));
|
|
|
|
return ethers.utils.getAddress(account.address);
|
2020-01-10 02:50:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
async signMessage(message: ethers.utils.Bytes | string): Promise<string> {
|
2020-01-10 02:50:09 -05:00
|
|
|
if (typeof(message) === 'string') {
|
2020-01-10 19:59:20 -05:00
|
|
|
message = ethers.utils.toUtf8Bytes(message);
|
2020-01-10 02:50:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
const messageHex = ethers.utils.hexlify(message).substring(2);
|
2020-01-10 02:50:09 -05:00
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
const sig = await this._retry((eth) => eth.signPersonalMessage(this.path, messageHex));
|
2020-01-10 02:50:09 -05:00
|
|
|
sig.r = '0x' + sig.r;
|
|
|
|
sig.s = '0x' + sig.s;
|
2020-01-10 19:59:20 -05:00
|
|
|
return ethers.utils.joinSignature(sig);
|
2020-01-10 02:50:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
async signTransaction(transaction: ethers.providers.TransactionRequest): Promise<string> {
|
2020-09-05 04:37:47 -04:00
|
|
|
const tx = await ethers.utils.resolveProperties(transaction);
|
|
|
|
const baseTx: ethers.utils.UnsignedTransaction = {
|
|
|
|
chainId: (tx.chainId || undefined),
|
|
|
|
data: (tx.data || undefined),
|
|
|
|
gasLimit: (tx.gasLimit || undefined),
|
|
|
|
gasPrice: (tx.gasPrice || undefined),
|
|
|
|
nonce: (tx.nonce ? ethers.BigNumber.from(tx.nonce).toNumber(): undefined),
|
|
|
|
to: (tx.to || undefined),
|
|
|
|
value: (tx.value || undefined),
|
|
|
|
};
|
|
|
|
|
|
|
|
const unsignedTx = ethers.utils.serializeTransaction(baseTx).substring(2);
|
2020-01-10 19:59:20 -05:00
|
|
|
const sig = await this._retry((eth) => eth.signTransaction(this.path, unsignedTx));
|
|
|
|
|
2020-09-05 04:37:47 -04:00
|
|
|
return ethers.utils.serializeTransaction(baseTx, {
|
|
|
|
v: ethers.BigNumber.from("0x" + sig.v).toNumber(),
|
2020-01-10 19:59:20 -05:00
|
|
|
r: ("0x" + sig.r),
|
|
|
|
s: ("0x" + sig.s),
|
2020-01-10 02:50:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:59:20 -05:00
|
|
|
connect(provider: ethers.providers.Provider): ethers.Signer {
|
2020-01-10 02:50:09 -05:00
|
|
|
return new LedgerSigner(provider, this.type, this.path);
|
|
|
|
}
|
|
|
|
}
|