ethers.js/utils/contract-address.js

21 lines
652 B
JavaScript
Raw Normal View History

2017-02-24 22:57:46 +03:00
2017-04-05 00:21:59 +03:00
var getAddress = require('./address').getAddress;
var convert = require('./convert');
var keccak256 = require('./keccak256');
var RLP = require('./rlp');
2017-02-24 22:57:46 +03:00
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
function getContractAddress(transaction) {
if (!transaction.from) { throw new Error('missing from address'); }
var nonce = transaction.nonce;
2017-04-05 00:21:59 +03:00
return getAddress('0x' + keccak256(RLP.encode([
2017-02-24 22:57:46 +03:00
getAddress(transaction.from),
convert.stripZeros(convert.hexlify(nonce, 'nonce'))
2017-02-24 22:57:46 +03:00
])).substring(26));
}
module.exports = {
getContractAddress: getContractAddress,
}