繁体   English   中英

ethereum web3js 如何导入“crypto-js”?

[英]how does ethereum web3js imports “crypto-js”?

我对以太坊存储库的 web3.js 文件中使用的语法有点困惑,虽然没有名为 crypto-js 的文件,也没有任何 npm 或纱线,但这个导入是如何完成的? https://github.com/ethereum/go-ethereum/blob/master/internal/jsre/deps/web3.js#L1828

您正在查看的 javascript 文件 (web3.js) 是 web3 构建的结果,即整个 web3 项目及其依赖项的 browserify 包。 来自 npm 的整个 crypto-js 库都捆绑在该文件中 - 这就是为什么在 go-ethereum 项目中没有其他对 crypto-js 的引用。 让我们看一下包含您链接的代码的 object,它看起来像这样:

{
    //...
    19: [
        function(require, module, exports) {
            //...
            var CryptoJS = require('crypto-js');
            var sha3 = require('crypto-js/sha3');
            //...
        },
        {
            "crypto-js": 59,
            "crypto-js/sha3": 80
        }
    ]
    //...
}

这个键/值对代表一个模块。 19是包中模块的 ID。 该值是一个包含两个元素的数组:(1) 模块代码和 (2) 模块的依赖项。 依赖项以 object 的形式给出,带有模块名称键和模块 ID 值。 因此,可以在密钥59下的同一 object 中找到crypto-js模块,同样可以在密钥80下找到crypto-js/sha3

修改web3.js可以通过获取源码并重新构建来完成。 go-ethereum存储库中的版本似乎是 0.20.1,对应于web3 存储库中的提交996148d3 构建这个版本有点痛苦,因为当时 web3 没有提交package-lock.json 我能够通过强制使用 gulp 3.9 和节点 10 来构建它。至于替换crypto-js ,您可以编辑lib/utils/sha3.js并将其替换为不同的sha3实现。

重建 web3 后,将dist/web3-light.js复制到go-ethereum repo 中的internals/jsre/deps/web3.js并运行go generate以重新生成internals/jsre/deps/bindata.go 最后,构建geth

把这一切放在一起:

# Clone web3
git clone https://github.com/ChainSafe/web3.js
cd web3.js
git switch -c replace-crypto-js 996148d356570745ef20630b499bce37f8484920

# Edit the sha3 implementation
vim lib/utils/sha3.js

# Build using gulp 3.9 and node 10
sed -i 's/"gulp": ">=3.9.0"/"gulp": "^3.9.0"/' package.json
npm install
npm --no-save install node@10
PATH=./node_modules/.bin gulp

# Clone go-ethereum
cd ..
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum

# Copy new web3 and regenerate bindata.go
cp ../web3.js/dist/web3-light.js internal/jsre/deps/web3.js
make devtools
PATH=$PATH:$(go env GOPATH)/bin go generate internal/jsre/deps/deps.go

# Build geth and test out changes in console
make geth
./build/bin/geth console

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM