Fixed getContractAddress for odd-length hex values (#572).

This commit is contained in:
Richard Moore 2019-09-08 02:39:00 -04:00
parent 6f4291f65f
commit 751793ea25
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 69 additions and 4 deletions

@ -4,7 +4,7 @@
import { BN } from "bn.js"; import { BN } from "bn.js";
import { arrayify, hexDataSlice, isHexString, stripZeros } from "@ethersproject/bytes"; import { arrayify, hexDataSlice, isHexString, stripZeros } from "@ethersproject/bytes";
import { BigNumberish } from "@ethersproject/bignumber"; import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { keccak256 } from "@ethersproject/keccak256"; import { keccak256 } from "@ethersproject/keccak256";
import { encode } from "@ethersproject/rlp"; import { encode } from "@ethersproject/rlp";
@ -140,7 +140,7 @@ export function getContractAddress(transaction: { from: string, nonce: BigNumber
logger.throwArgumentError("missing from address", "transaction", transaction); logger.throwArgumentError("missing from address", "transaction", transaction);
} }
let nonce = stripZeros(arrayify(transaction.nonce)); let nonce = stripZeros(arrayify(BigNumber.from(transaction.nonce).toHexString()));
return getAddress(hexDataSlice(keccak256(encode([ from, nonce ])), 12)); return getAddress(hexDataSlice(keccak256(encode([ from, nonce ])), 12));
} }

@ -28,17 +28,82 @@ describe('Test Contract Address Generation', function() {
let getContractAddress = ethers.utils.getContractAddress; let getContractAddress = ethers.utils.getContractAddress;
// Transaction: 0x939aa17985bc2a52a0c1cba9497ef09e092355a805a8150e30e24b753bac6864
let Tests = [ let Tests = [
// Transaction: 0x939aa17985bc2a52a0c1cba9497ef09e092355a805a8150e30e24b753bac6864
{ {
address: '0x3474627D4F63A678266BC17171D87f8570936622', address: '0x3474627D4F63A678266BC17171D87f8570936622',
name: 'tx-0x939aa17985bc2a52a0c1cba9497ef09e092355a805a8150e30e24b753bac6864', name: 'tx-0x939aa179 (number)',
tx: { tx: {
from: '0xb2682160c482eb985ec9f3e364eec0a904c44c23', from: '0xb2682160c482eb985ec9f3e364eec0a904c44c23',
nonce: 10, nonce: 10,
} }
}, },
{
address: '0x3474627D4F63A678266BC17171D87f8570936622',
name: 'tx-0x939aa179 (odd-zero-hex)',
tx: {
from: '0xb2682160c482eb985ec9f3e364eec0a904c44c23',
nonce: "0xa",
}
},
{
address: '0x3474627D4F63A678266BC17171D87f8570936622',
name: 'tx-0x939aa179 (even-zero-hex)',
tx: {
from: '0xb2682160c482eb985ec9f3e364eec0a904c44c23',
nonce: "0x0a",
}
},
// Ropsten: https://etherscan.io/tx/0x78d17f8ab31fb6ad688340634a9a29d8726feb6d588338a9b9b21a44159bc916
{
address: '0x271300790813f82638A8A6A8a86d65df6dF33c17',
name: 'tx-0x78d17f8a (odd-long-hex)',
tx: {
from: '0x8ba1f109551bd432803012645ac136ddd64dba72',
nonce: "0x200",
}
},
{
address: '0x271300790813f82638A8A6A8a86d65df6dF33c17',
name: 'tx-0x78d17f8a (even-long-hex)',
tx: {
from: '0x8ba1f109551bd432803012645ac136ddd64dba72',
nonce: "0x0200",
}
},
// https://ropsten.etherscan.io/tx/0x444ea8ae9890ac0ee5fd249512726abf9d23f44a378d5f45f727b65dc1b899c2
{
address: '0x995C25706C407a1F1E84b3777775e3e619764933',
name: 'tx-0x444ea8ae (even-long-hex)',
tx: {
from: '0x8ba1f109551bd432803012645ac136ddd64dba72',
nonce: "0x1d",
}
},
{
address: '0x995C25706C407a1F1E84b3777775e3e619764933',
name: 'tx-0x444ea8ae (padded-long-hex)',
tx: {
from: '0x8ba1f109551bd432803012645ac136ddd64dba72',
nonce: "0x001d",
}
},
{
address: '0x995C25706C407a1F1E84b3777775e3e619764933',
name: 'tx-0x444ea8ae (number)',
tx: {
from: '0x8ba1f109551bd432803012645ac136ddd64dba72',
nonce: 29,
}
},
// Ropsten: 0x5bdfd14fcc917abc2f02a30721d152a6f147f09e8cbaad4e0d5405d646c5c3e1 // Ropsten: 0x5bdfd14fcc917abc2f02a30721d152a6f147f09e8cbaad4e0d5405d646c5c3e1
{ {
address: '0x0CcCC7507aEDf9FEaF8C8D731421746e16b4d39D', address: '0x0CcCC7507aEDf9FEaF8C8D731421746e16b4d39D',