Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static fromRaw (hex: string, chainId: any): ITransaction {
if (chainId === 62 || chainId === 61) {
// Because ethereumjs-tx doesn't support MORDEN and ETC
const custom = Common.forCustomChain(1, {
chainId
}, 'byzantium');
return new EthereumTx(new EthTx(hex, { common: custom }));
}
return new EthereumTx(new EthTx(hex, { chain: chainId }));
}
public internalTx: EthTx;
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);
function signTransaction(rawTx, privateKey) {
// check if privateKey->address matches rawTx.from
var publicKey = (0, _publicKeyByPrivateKey2['default'])(privateKey);
var address = (0, _publicKey.toAddress)(publicKey);
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey');
var privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex');
var tx = new _ethereumjsTx.Transaction(rawTx);
tx.sign(privateKeyBuffer);
var serializedTx = tx.serialize().toString('hex');
return serializedTx;
}
export default function signTransaction(rawTx, privateKey) {
// check if privateKey->address matches rawTx.from
var publicKey = publicKeyByPrivateKey(privateKey);
var address = addressByPublicKey(publicKey);
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey');
var privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex');
var tx = new Transaction(rawTx);
tx.sign(privateKeyBuffer);
var serializedTx = tx.serialize().toString('hex');
return serializedTx;
}
signTx(txData: EthTxData): string {
const tx = new EthereumTx(txData);
// tx.gasLimit = txData.gasLimit;
// tx.gasPrice = txData.gasPrice;
// tx.nonce = txData.nonce;
// tx.value = txData.value;
// tx.data = txData.data;
// tx.chainId = txData.chainId;
tx.sign(this.wallet.getPrivateKey());
return `0x${tx.serialize().toString('hex')}`;
}
}
result => {
if (!result.success) {
return reject(Error(result.error));
}
// TODO: Explain what's going on here? Add tests? Adapted from:
// https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
const txToSerialize: TxData = {
...strTx,
v: addHexPrefix(new BN(result.v).toString(16)),
r: addHexPrefix(result.r.toString()),
s: addHexPrefix(result.s)
};
const eTx = new EthTx(txToSerialize);
const serializedTx = eTx.serialize();
resolve(serializedTx);
}
);
// check the returned signature_v and recalc signature_v if it needed
// see also https://github.com/trezor/trezor-mcu/pull/399
if (Number(res.payload.v) <= 1) {
// for larger chainId, only signature_v returned. simply recalc signature_v
res.payload.v += 2 * chainId + 35;
}
// TODO: Explain what's going on here? Add tests? Adapted from:
// https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
const txToSerialize: TxData = {
...strTx,
v: res.payload.v,
r: res.payload.r,
s: res.payload.s
};
const eTx = new EthTx(txToSerialize);
const serializedTx = eTx.serialize();
resolve(serializedTx);
});
});
// (rv !== cv) : for v is truncated byte case
// (rv & cv): make cv to truncated byte
// (rv & cv) !== rv: signature v bit needed
cv += 1; // add signature v bit.
}
v = cv.toString(16);
}
const txToSerialize: TxData = {
...txFields,
v: addHexPrefix(v),
r: addHexPrefix(result.r),
s: addHexPrefix(result.s)
};
return new EthTx(txToSerialize).serialize();
} catch (err) {
throw Error(err + '. Check to make sure contract data is on');
}
}
it('returns a signed tx object', async () => {
await keyring.deserialize([ privateKey ])
const txParams = {
from: address,
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: address,
value: '0x1000',
}
const tx = new EthereumTx(txParams)
const signed = await keyring.signTransaction(address, tx)
assert.ok(signed.raw, 'has a raw signature')
})
})
.then(signed => {
console.log(signed);
const _tx = new Transaction(signed);
const signedChainId = calculateChainIdFromV(_tx.v);
if (signedChainId !== networkId)
throw new Error(
'Invalid networkId signature returned. Expected: ' +
networkId +
', Got: ' +
signedChainId,
'InvalidNetworkId'
);
resolve(getSignTransactionObject(_tx));
})
.catch(reject);