我一直试图在浏览器中使用ethereumjs。我已经从这里下载了浏览器构建,https://github.com/ethereumjs/browser-builds/tree/master/dist。
这是我的代码片段
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript" src="ethereumjs-tx.js"></script>
</head>
<body>
<script>
var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');
var rawTx = {
nonce: '00',
gasPrice: '09184e72a000',
gasLimit: '2710',
to: '0000000000000000000000000000000000000000',
value: '00',
data: '7f7465737432000000000000000000000000000000000000000000000000000000600057'
};
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
</script>但是,它总是在控制台未定义的ReferenceError: require中抛出错误。
发布于 2017-08-02 08:56:52
试试这个..。
<html>
<head>
<!--
<script src="https://github.com/ethereumjs/browser-builds/blob/master/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
-->
<script src="https://cdn.jsdelivr.net/gh/ethereumjs/browser-builds/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
<script>
console.log('typeof ethereumjs:', (typeof ethereumjs))
console.log('Object.keys(ethereumjs):', Object.keys(ethereumjs))
console.log('typeof ethereumjs.Tx:', (typeof ethereumjs.Tx))
console.log('typeof ethereumjs.RLP:', (typeof ethereumjs.RLP))
console.log('typeof ethereumjs.Util:', (typeof ethereumjs.Util))
console.log('typeof ethereumjs.Buffer:', (typeof ethereumjs.Buffer))
console.log('typeof ethereumjs.Buffer.Buffer:', (typeof ethereumjs.Buffer.Buffer))
{
let privateKey = new ethereumjs.Buffer.Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
let txParams = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}
let tx = new ethereumjs.Tx(txParams)
tx.sign(privateKey)
let serializedTx = tx.serialize().toString('hex')
console.log('serializedTx:', serializedTx)
}
</script>
</head>
<body>
</body>
</html>发布于 2016-09-09 04:29:25
这并不是非常具体的虚空相关,但什么都可以。
请求()不是一个有效的JS函数客户端。当Meaning...your浏览器到达require行时,它不知道该做什么。
有一个名为require.js的漂亮包,它将需求功能扩展到浏览器。
我不熟悉他们的浏览器构建,但可能是因为您使用的代码不是针对这些构建的。如果我是您,我将尝试删除"require“行,并查看它是否运行,因为您正在头上加载ethereumjs。
此外,如果这是一个HTML片段,您应该将您的JS封装在<script>标记中,并且可能是console.log(serializedTx)或任何您想要得到的东西,这样您就可以在它运行时实际看到结果。;)
https://ethereum.stackexchange.com/questions/8579
复制相似问题