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 signRawTransaction(t: EthTx): Promise {
const txFields = getTransactionFields(t);
t.v = toBuffer(t.getChainId());
t.r = toBuffer(0);
t.s = toBuffer(0);
try {
const ethApp = await makeApp();
if (t.getChainId() === 1) {
const tokenInfo = byContractAddress(t.to.toString('hex'));
if (tokenInfo) {
await ethApp.provideERC20TokenInformation(tokenInfo);
}
}
const result = await ethApp.signTransaction(this.getPath(), t.serialize().toString('hex'));
let v = result.v;
if (t.getChainId() > 0) {
// EIP155 support. check/recalc signature v value.
// Please see https://github.com/LedgerHQ/blue-app-eth/commit/8260268b0214810872dabd154b476f5bb859aac0
// currently, ledger returns only 1-byte truncated signatur_v
const rv = parseInt(v, 16);
let cv = t.getChainId() * 2 + 35; // calculated signature v, without signature bit.
/* tslint:disable no-bitwise */
if (rv !== cv && (rv & cv) !== rv) {
transaction,
coreTransaction,
onDeviceSignatureGranted,
onDeviceSignatureRequested
}) {
// sign with device
const hwApp = new Eth(transport);
const subAccount =
transaction.subAccountId && subAccounts
? subAccounts.find(t => t.id === transaction.subAccountId)
: null;
if (subAccount && subAccount.type === "TokenAccount") {
const { token } = subAccount;
const tokenInfo = byContractAddress(token.contractAddress);
invariant(
tokenInfo,
`contract ${token.contractAddress} data for ${token.id} ERC20 not found`
);
await hwApp.provideERC20TokenInformation(tokenInfo);
}
const serialized = await coreTransaction.serialize();
onDeviceSignatureRequested();
const result = await hwApp.signTransaction(freshAddressPath, serialized);
onDeviceSignatureGranted();
await coreTransaction.setSignature(result.v, result.r, result.s);
const signature = await coreTransaction.serialize();
public async signRawTransaction(t: EthTx): Promise {
const txFields = getTransactionFields(t);
t.v = toBuffer(t._chainId);
t.r = toBuffer(0);
t.s = toBuffer(0);
try {
const ethApp = await makeApp();
if (t.getChainId() === 1) {
const tokenInfo = byContractAddress(t.to.toString('hex'));
if (tokenInfo) {
await ethApp.provideERC20TokenInformation(tokenInfo);
}
}
const result = await ethApp.signTransaction(this.getPath(), t.serialize().toString('hex'));
let v = result.v;
if (t._chainId > 0) {
// EIP155 support. check/recalc signature v value.
// Please see https://github.com/LedgerHQ/blue-app-eth/commit/8260268b0214810872dabd154b476f5bb859aac0
// currently, ledger returns only 1-byte truncated signatur_v
const rv = parseInt(v, 16);
let cv = t._chainId * 2 + 35; // calculated signature v, without signature bit.
/* tslint:disable no-bitwise */
if (rv !== cv && (rv & cv) !== rv) {
const txSigner = async tx => {
tx = new Transaction(tx, {
common: commonGenerator(store.state.network)
});
const networkId = tx.getChainId();
tx.raw[6] = networkId;
tx.raw[7] = Buffer.from([]);
tx.raw[8] = Buffer.from([]);
const tokenInfo = byContractAddress('0x' + tx.to.toString('hex'));
if (tokenInfo) await this.ledger.provideERC20TokenInformation(tokenInfo);
const result = await this.ledger.signTransaction(
accountPath,
tx.serialize().toString('hex')
);
// EIP155 support. check/recalc signature v value.
let v = result.v;
const rv = parseInt(v, 16);
let cv = networkId * 2 + 35;
if (rv !== cv && (rv & cv) !== rv) {
cv += 1; // add signature v bit.
}
v = cv.toString(16);
tx.v = getBufferFromHex(v);
test("erc20 are all consistent with those on ledgerjs side", () => {
const normalList = listTokens();
const delistedList = listTokens({ withDelisted: true });
expect(delistedList.length).toBeGreaterThan(normalList.length);
for (const token of delistedList) {
if (token.delisted) {
expect(normalList.find(o => o.id === token.id)).toBeUndefined();
}
if (token.tokenType === "erc20") {
const tokenData = byContractAddress(token.contractAddress);
if (!tokenData) {
throw new Error(token.name + " not available in ledgerjs data");
}
expect(token.ticker.toLowerCase()).toBe(tokenData.ticker.toLowerCase());
expect(token.contractAddress.toLowerCase()).toBe(
tokenData.contractAddress.toLowerCase()
);
expect(token.units[0].magnitude).toBe(tokenData.decimals);
}
}
});
setERC20ContractAddress(contractAddress: string): Promise {
const info = byContractAddress(contractAddress)
if (info) {
return this.provideERC20TokenInformation(info)
}
return Promise.resolve()
}
}
transport: Transport<*>,
account: Account,
currency: CryptoCurrency,
subAccountId: ?string,
coreCurrency: CoreCurrency,
coreTransaction: CoreEthereumLikeTransaction
}) {
const hwApp = new Eth(transport);
const subAccount = subAccountId
? account.subAccounts &&
account.subAccounts.find(t => t.id === subAccountId)
: null;
if (subAccount && subAccount.type === "TokenAccount") {
const { token } = subAccount;
const tokenInfo = byContractAddress(token.contractAddress);
invariant(
tokenInfo,
`contract ${token.contractAddress} data for ${token.id} ERC20 not found`
);
await hwApp.provideERC20TokenInformation(tokenInfo);
}
const result = await hwApp.signTransaction(
account.freshAddressPath,
await coreTransaction.serialize()
);
await coreTransaction.setSignature(result.v, result.r, result.s);
const raw = await coreTransaction.serialize();
async signTransaction(tx, path) {
const app = await getEthApp();
const ethTx = new EthTx({
...tx,
v: Buffer.from([tx.chainId]),
r: toBuffer(0),
s: toBuffer(0)
});
if (ethTx.getChainId() === 1) {
const tokenInfo = byContractAddress(ethTx.to.toString('hex'));
if (tokenInfo) {
await app.provideERC20TokenInformation(tokenInfo);
}
}
const rsv = await app.signTransaction(path, ethTx.serialize().toString('hex'));
const signedTx = new EthTx({
...tx,
r: addHexPrefix(rsv.r),
s: addHexPrefix(rsv.s),
v: addHexPrefix(rsv.v)
});
return {
signedTransaction: signedTx.serialize().toString('hex')
};
},