Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
const { chainId, ...strTx } = getTransactionFields(tx);
// stripHexPrefixAndLower identical to ethFuncs.getNakedAddress
const cleanedTx = mapValues(mapValues(strTx, stripHexPrefixAndLower), padLeftEven);
TrezorConnect.ethereumSignTransaction({
path: this.getPath(),
transaction: {
nonce: cleanedTx.nonce,
gasPrice: cleanedTx.gasPrice,
gasLimit: cleanedTx.gasLimit,
to: cleanedTx.to,
value: cleanedTx.value,
data: cleanedTx.data,
chainId
}
}).then((res: any) => {
if (!res.success) {
return reject(Error(res.error));
}
// check the returned signature_v and recalc signature_v if it needed
// see also https://github.com/trezor/trezor-mcu/pull/399
async signTransaction(txData: any, cb: (err: any, res?: any) => any): Promise {
const transaction = {
to: toTrezorHex(txData.to),
value: toTrezorHex(txData.value),
data: toTrezorHex(txData.data),
chainId: parseInt(network, 16),
gasLimit: toTrezorHex(txData.gas),
gasPrice: toTrezorHex(txData.gasPrice),
};
try {
const { v, s, r } = await TrezorConnect.ethereumSignTransaction({
path,
transaction,
});
const res = {
value: txData.value || "0x00",
data: addHexPrefix(txData.data),
gasPrice: parseInt(txData.gasPrice, 16),
nonce: parseInt(txData.nonce, 16),
gasLimit: txData.gas,
v,
s: "0x" + s,
r: "0x" + r,
};
const tx = new EthereumTx(res);
const txSigner = async tx => {
const locTx = Object.assign({}, tx);
locTx['chainId'] = new BigNumber(tx['chainId']).toNumber();
tx = new Transaction(locTx, {
common: commonGenerator(store.state.network)
});
const networkId = tx.getChainId();
const options = {
path: this.basePath + '/' + idx,
transaction: getHexTxObject(tx)
};
const result = await Trezor.ethereumSignTransaction(options);
if (!result.success) throw new Error(result.payload.error);
tx.v = getBufferFromHex(result.payload.v);
tx.r = getBufferFromHex(result.payload.r);
tx.s = getBufferFromHex(result.payload.s);
const signedChainId = calculateChainIdFromV(tx.v);
if (signedChainId !== networkId)
throw new Error(
'Invalid networkId signature returned. Expected: ' +
networkId +
', Got: ' +
signedChainId,
'InvalidNetworkId'
);
return getSignTransactionObject(tx);
};
const msgSigner = async msg => {
async sign(transaction) {
if (transaction.value) {
transaction.value = transaction.value.toHexString();
}
transaction.gasPrice = utils.hexlify(transaction.gasPrice);
if (!transaction.chainId) {
transaction.chainId = this.chainId;
}
const tx = new EthereumTx(transaction);
const txFields = getTransactionFields(tx);
const signature = await TrezorConnect.ethereumSignTransaction({
transaction: txFields,
path: this.derivationPath + this.addressIndex,
});
const txToSerialize = {
...txFields,
...signature.payload,
};
return '0x' + new EthereumTx(txToSerialize).serialize().toString('hex');
}
const txSigner = async tx => {
tx = new Transaction(tx, {
common: commonGenerator(store.state.network)
});
const networkId = tx.getChainId();
const options = {
path: this.basePath + '/' + idx,
transaction: getHexTxObject(tx)
};
const result = await Trezor.ethereumSignTransaction(options);
if (!result.success) throw new Error(result.payload.error);
tx.v = getBufferFromHex(result.payload.v);
tx.r = getBufferFromHex(result.payload.r);
tx.s = getBufferFromHex(result.payload.s);
const signedChainId = calculateChainIdFromV(tx.v);
if (signedChainId !== networkId)
throw new Error(
'Invalid networkId signature returned. Expected: ' +
networkId +
', Got: ' +
signedChainId,
'InvalidNetworkId'
);
return getSignTransactionObject(tx);
};
const msgSigner = async msg => {
sign = async (transaction: TransactionRequest) => {
this.openModal();
const tx = await buildTransaction(transaction, this.address, this.provider);
const result = await TrezorConnect.ethereumSignTransaction({
path: this.path,
transaction: tx,
});
if (result.success) {
const sig = {
v: parseInt(result.payload.v.substring(2), 16),
r: result.payload.r,
s: result.payload.s,
};
this.dispatch(closeModal());
return utils.serializeTransaction(tx, sig);
}
this.dispatch(
signTx = async params => {
const txProps = await utils.resolveProperties(params)
txProps.value = txProps.value || utils.hexlify(0)
const { success, payload } = await TrezorConnect.ethereumSignTransaction({
path: this.path,
transaction: txProps,
})
if (success) {
const signature = {
r: payload.r,
s: payload.s,
v: parseInt(payload.v),
}
const txSigned = utils.serializeTransaction(txProps, signature)
return txSigned
} else {
throw new Error(payload.error)
const trezorSignTransaction = async (txn, hdpath) => {
const trezorFormattedTxn = {
to: txn.to.toString('hex'),
value: txn.value.toString('hex'),
data: txn.data.toString('hex'),
gasLimit: txn.gasLimit.toString('hex'),
gasPrice: txn.gasPrice.toString('hex'),
nonce: txn.nonce.length === 0 ? '00' : txn.nonce.toString('hex'),
chainId: formatChainId(txn.getChainId()),
};
const sig = await TrezorConnect.ethereumSignTransaction({
path: hdpath,
transaction: trezorFormattedTxn,
});
if (!sig.success) {
throw new Error(sig.payload.error);
}
const payload = sig.payload;
txn.v = Buffer.from(payload.v.slice(2), 'hex');
txn.r = Buffer.from(payload.r.slice(2), 'hex');
txn.s = Buffer.from(payload.s.slice(2), 'hex');
return txn;
};