我遇到问题的两个包:
npm i @metamask/eth-sig-util
npm i @ethereumjs/tx
它们依赖的共同包:ethereumjs-util
但是,前者支持的版本是: 6.2.1 ,后者支持:7.1.2
@ethereumjs/tx使用案例:
import Common, {Chain} from "@ethereumjs/common"
import { Transaction } from "@ethereumjs/tx"
try {
let privateKeyStr = 'cb241a7ee5fdecfa2dff1e46457e56fed41cc7560594e0fbf6fcf3872a392549'
let privateKey = Buffer.from(privateKeyStr, 'hex');
const txParams2 = {
nonce: '0x3f',
to: '0x50ee674689d75c0f88e8f83cfe8c4b69e8fd590d',
value: '0x2386f26fc10000',
gasLimit: '0x5208',
gasPrice: '0xba43b7400',
}
const common = new Common({chain: "ropsten"})
const tx2 = Transaction.fromTxData(txParams2, {common})
const signedTx = tx2.sign(privateKey)
const serializedTxStr = signedTx.serialize().toString('hex')
console.log('serializedTxStr========', serializedTxStr)
}catch(err){ console.log(err.message) }
正确输出结果:serializedTxStr======== f86b3f850ba43b74008252089450ee674689d75c0f88e8f83cfe8c4b69e8fd590d872386f26fc100008029a06021a421ccd55ed37175669346c0b6ea7634e41f49c085a0d341b82a0fe203b2a03ba904ec1352bca5deaae1551cd765d3ed025e70379a64c334b0f7c45f899fad
@metamask/eth-sig-util使用案例:
import { signTypedData,SignTypedDataVersion } from "@metamask/eth-sig-util"
try{
let getData = "{\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"verifyingContract\",\"type\":\"address\"}],\"RelayRequest\":[{\"name\":\"target\",\"type\":\"address\"},{\"name\":\"encodedFunction\",\"type\":\"bytes\"},{\"name\":\"gasData\",\"type\":\"GasData\"},{\"name\":\"relayData\",\"type\":\"RelayData\"}],\"GasData\":[{\"name\":\"gasLimit\",\"type\":\"uint256\"},{\"name\":\"gasPrice\",\"type\":\"uint256\"},{\"name\":\"pctRelayFee\",\"type\":\"uint256\"},{\"name\":\"baseRelayFee\",\"type\":\"uint256\"}],\"RelayData\":[{\"name\":\"senderAddress\",\"type\":\"address\"},{\"name\":\"senderNonce\",\"type\":\"uint256\"},{\"name\":\"relayWorker\",\"type\":\"address\"},{\"name\":\"paymaster\",\"type\":\"address\"}]},\"domain\":{\"name\":\"GSN Relayed Transaction\",\"version\":\"1\",\"chainId\":42,\"verifyingContract\":\"0x6453D37248Ab2C16eBd1A8f782a2CBC65860E60B\"},\"primaryType\":\"RelayRequest\",\"message\":{\"target\":\"0x9cf40ef3d1622efe270fe6fe720585b4be4eeeff\",\"encodedFunction\":\"0xa9059cbb0000000000000000000000002e0d94754b348d208d64d52d78bcd443afa9fa520000000000000000000000000000000000000000000000000000000000000007\",\"gasData\":{\"gasLimit\":\"39507\",\"gasPrice\":\"1700000000\",\"pctRelayFee\":\"70\",\"baseRelayFee\":\"0\"},\"relayData\":{\"senderAddress\":\"0x22d491bde2303f2f43325b2108d26f1eaba1e32b\",\"senderNonce\":\"3\",\"relayWorker\":\"0x3baee457ad824c94bd3953183d725847d023a2cf\",\"paymaster\":\"0x957F270d45e9Ceca5c5af2b49f1b5dC1Abb0421c\"}}}"
const privateKey = Buffer.from(
'cb241a7ee5fdecfa2dff1e46457e56fed41cc7560594e0fbf6fcf3872a392549',
'hex',
)
let result = await signTypedData({
privateKey,
data: JSON.parse(getData),
version: SignTypedDataVersion.V4,
})
console.log('result=',result)
}catch(err){ console.log(err.message) }
->正确结果:result= 0x3c1014908969c1b4e867ef8d24b4cee7951e6e6ad2d798e7ef3a872242c48ab7132a88741f730096c378b98a8d62f2879a629c932d932cc7eed6feb4f94a832c1c
2 个回复
uniapp2022 (作者)
我遇到问题的两个包:
npm i @metamask/eth-sig-util
npm i @ethereumjs/tx
它们依赖的共同包:ethereumjs-util
但是,前者支持的版本是: 6.2.1 ,后者支持:7.1.2
@ethereumjs/tx使用案例:
@metamask/eth-sig-util使用案例:
->正确结果:result= 0x3c1014908969c1b4e867ef8d24b4cee7951e6e6ad2d798e7ef3a872242c48ab7132a88741f730096c378b98a8d62f2879a629c932d932cc7eed6feb4f94a832c1c
DCloud_UNI_GSQ
是 HBuilderX 创建的 uni-app 项目吗?
uniapp2022 (作者)
是的。
2021-10-13 10:43
DCloud_UNI_GSQ
回复 r***@qq.com: 试试迁移成 CLI 的项目
2021-10-14 20:38