如何获得合同创建者的地址

时间:2019-01-05 21:49:01

标签: blockchain smartcontracts web3js

如果我只知道合同地址和合同接口(ABI),我试图找出是否可以获取合同创建者的地址吗?

1 个答案:

答案 0 :(得分:1)

没有用于查找合同创建者地址的显式web3.js方法。如果要使用web3.js完成此操作,则必须从本质上遍历所有先前的块和交易,然后再通过web3.eth.getTransactionReceipt搜索交易收据。这将返回一个contractAddress属性,该属性可以与您拥有的合同地址进行比较。

以下是使用web3.js(v1.0.0-beta.37)的示例:

const contractAddress = '0x61a54d8f8a8ec8bf2ae3436ad915034a5b223f5a';

async function getContractCreatorAddress() {
    let currentBlockNum = await web3.eth.getBlockNumber();
    let txFound = false;

    while(currentBlockNum >= 0 && !txFound) {
        const block = await web3.eth.getBlock(currentBlockNum, true);
        const transactions = block.transactions;

        for(let j = 0; j < transactions.length; j++) {
            // We know this is a Contract deployment
            if(!transactions[j].to) {
                const receipt = await web3.eth.getTransactionReceipt(transactions[j].hash);
                if(receipt.contractAddress && receipt.contractAddress.toLowerCase() === contractAddress.toLowerCase()) {
                    txFound = true;
                    console.log(`Contract Creator Address: ${transactions[j].from}`);
                    break;
                }
            }
        }

        currentBlockNum--;
    }
}

getContractCreatorAddress();