Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async initialize(): Promise<{app: AppEth; close(): void}> {
if (!Ledger.allowParallel && Ledger.initialized) {
throw new Error('another ledger wallet call is already initialized');
}
Ledger.initialized = true;
const transport = await Ledger.Transport.create();
// transport.setDebugMode(true);
return {
app: new AppEth(transport),
close: () => {
Ledger.initialized = false;
transport.close();
},
};
}
async function signTransaction(txData) {
const path = addressToPathMap[txData.from.toLowerCase()];
if (!path) throw new Error("address unknown '" + txData.from + "'");
const transport = await getTransport();
try {
const eth = new AppEth(transport);
const tx = new EthereumTx(txData);
// Set the EIP155 bits
tx.raw[6] = Buffer.from([networkId]); // v
tx.raw[7] = Buffer.from([]); // r
tx.raw[8] = Buffer.from([]); // s
// Pass hex-rlp to ledger for signing
const result = await eth.signTransaction(
path,
tx.serialize().toString("hex")
);
// Store signature in transaction
tx.v = Buffer.from(result.v, "hex");
tx.r = Buffer.from(result.r, "hex");
async pollDevice() {
const { setWallet, setWalletHdPath } = this.props;
const { hdpath } = this.state;
const transport = await Transport.create();
const eth = new Eth(transport);
const path = chopHdPrefix(hdpath);
eth.getAddress(path, false, true).then(
info => {
const publicKey = Buffer.from(info.publicKey, 'hex');
const chainCode = Buffer.from(info.chainCode, 'hex');
const pub = secp256k1.publicKeyConvert(publicKey, true);
const hd = bip32.fromPublicKey(pub, chainCode);
setWallet(Maybe.Just(hd));
setWalletHdPath(addHdPrefix(hdpath));
},
_ => {
setWallet(Maybe.Nothing());
}
);
}
onGetLedgerEthereumAddress = async () => {
try {
this.setState({ error: null });
const transport = await TransportU2F.create();
const btc = new Eth(transport);
const { address } = await btc.getAddress("44'/60'/0'/0'/0");
this.setState({ address });
} catch (error) {
this.setState({ error });
}
};
render() {
async getAppConfig() {
const transport = await this.getTransport();
try {
const eth = new Ledger(transport);
const appConfig = await eth.getAppConfiguration();
return appConfig;
} catch (e) {
throw e;
} finally {
transport
.close()
.then(() => {
this.connectionOpened = false;
})
.catch(error => {
throw error;
});
}
}
constructor (device, emit) {
this.device = device
this.id = this.device.id
this.eth = new AppEth(device)
this.emit = emit
this.setup()
}
export const newEth = (transport) => new Eth(transport);
async _signPersonalMessage(msgData) {
const path = await this.checkIfKnownAddress(msgData);
const transport = await this.getTransport();
try {
const thisMessage = msgData.data ? msgData.data : msgData;
const eth = new Ledger(transport);
const result = await eth.signPersonalMessage(
path,
Buffer.from(thisMessage).toString('hex')
);
const v = parseInt(result.v, 10) - 27;
let vHex = v.toString(16);
if (vHex.length < 2) {
vHex = `0${v}`;
}
return `0x${result.r}${result.s}${vHex}`;
} finally {
transport
.close()
.then(() => {
this.connectionOpened = false;
})