簡體   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