20 golang语言实现eip712签名数组类型的数据

var Types = core.Types{
"EIP712Domain": {
{
Name: "name",
Type: "string",
},
{
Name: "version",
Type: "string",
},
{
Name: "chainId",
Type: "uint256",
},
{
Name: "verifyingContract",
Type: "address",
},
},
"Claim": {
{
Name: "users",
Type: "address[]",
},
{
Name: "amounts",
Type: "uint256[]",
},
{
Name: "user",
Type: "address",
},
{
Name: "nonce",
Type: "uint256",
},
{
Name: "deadline",
Type: "uint256",
},

},

}

func Sign(key *ecdsa.PrivateKey, data *core.TypedData) (hexutil.Bytes, error) {
domainSeparator, err := data.HashStruct("EIP712Domain", data.Domain.Map())
if err != nil {
return nil, err
}
typedDataHash, err := data.HashStruct(data.PrimaryType, data.Message)
if err != nil {
return nil, err
}
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
dataHash := crypto.Keccak256(rawData)

signature, err := crypto.Sign(dataHash, key)
if err != nil {
return nil, err
}

if signature[64] < 27 {
signature[64] += 27
}

return signature, nil

}

type Message struct {
Users []string
Amounts []string
User string
Nonce string
Deadline string
}

contents := utils.Message{
Users: []string {"0x111da67948Ef5Ed1f82D707B8cd7e3B1DFa87AEa"},
Amounts:  []string{"1"},
User:     address,
Nonce:    nonce,
Deadline: strconv.Itoa(int(duration)),
}

之前签过单个数据类型的比如address,uint256这样的,但是上面这个里面有address[]数组类型的,用下面的方发签名失败,报错data '[0x111da67948Ef5Ed1f82D707B8cd7e3B1DFa87AEa]' doesn't match type 'address[]'数据类型不匹配,那应该怎么写实匹配的呢

请先 登录 后评论

1 个回答

张小风
请先 登录 后评论
  • 0 关注
  • 0 收藏,3909 浏览
  • trybye 提出于 2021-08-05 11:40