Skip to content

Commit 5a0d487

Browse files
holimanpranksteess
andauthored
signer/core: fix complex typed data sign (EIP712) (#24220)
Co-authored-by: specerxi <[email protected]>
1 parent 2d20fed commit 5a0d487

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

signer/core/apitypes/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage
262262

263263
// Dependencies returns an array of custom types ordered by their hierarchical reference tree
264264
func (typedData *TypedData) Dependencies(primaryType string, found []string) []string {
265+
primaryType = strings.TrimSuffix(primaryType, "[]")
265266
includes := func(arr []string, str string) bool {
266267
for _, obj := range arr {
267268
if obj == str {
@@ -364,7 +365,7 @@ func (typedData *TypedData) EncodeData(primaryType string, data map[string]inter
364365
if err != nil {
365366
return nil, err
366367
}
367-
arrayBuffer.Write(encodedData)
368+
arrayBuffer.Write(crypto.Keccak256(encodedData))
368369
} else {
369370
bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
370371
if err != nil {

signer/core/signed_data_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,153 @@ func TestGnosisCustomDataWithChainId(t *testing.T) {
662662
t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
663663
}
664664
}
665+
666+
var complexTypedData = `
667+
{
668+
"types": {
669+
"EIP712Domain": [
670+
{
671+
"name": "chainId",
672+
"type": "uint256"
673+
},
674+
{
675+
"name": "name",
676+
"type": "string"
677+
},
678+
{
679+
"name": "verifyingContract",
680+
"type": "address"
681+
},
682+
{
683+
"name": "version",
684+
"type": "string"
685+
}
686+
],
687+
"Action": [
688+
{
689+
"name": "action",
690+
"type": "string"
691+
},
692+
{
693+
"name": "params",
694+
"type": "string"
695+
}
696+
],
697+
"Cell": [
698+
{
699+
"name": "capacity",
700+
"type": "string"
701+
},
702+
{
703+
"name": "lock",
704+
"type": "string"
705+
},
706+
{
707+
"name": "type",
708+
"type": "string"
709+
},
710+
{
711+
"name": "data",
712+
"type": "string"
713+
},
714+
{
715+
"name": "extraData",
716+
"type": "string"
717+
}
718+
],
719+
"Transaction": [
720+
{
721+
"name": "DAS_MESSAGE",
722+
"type": "string"
723+
},
724+
{
725+
"name": "inputsCapacity",
726+
"type": "string"
727+
},
728+
{
729+
"name": "outputsCapacity",
730+
"type": "string"
731+
},
732+
{
733+
"name": "fee",
734+
"type": "string"
735+
},
736+
{
737+
"name": "action",
738+
"type": "Action"
739+
},
740+
{
741+
"name": "inputs",
742+
"type": "Cell[]"
743+
},
744+
{
745+
"name": "outputs",
746+
"type": "Cell[]"
747+
},
748+
{
749+
"name": "digest",
750+
"type": "bytes32"
751+
}
752+
]
753+
},
754+
"primaryType": "Transaction",
755+
"domain": {
756+
"chainId": "56",
757+
"name": "da.systems",
758+
"verifyingContract": "0x0000000000000000000000000000000020210722",
759+
"version": "1"
760+
},
761+
"message": {
762+
"DAS_MESSAGE": "SELL mobcion.bit FOR 100000 CKB",
763+
"inputsCapacity": "1216.9999 CKB",
764+
"outputsCapacity": "1216.9998 CKB",
765+
"fee": "0.0001 CKB",
766+
"digest": "0x53a6c0f19ec281604607f5d6817e442082ad1882bef0df64d84d3810dae561eb",
767+
"action": {
768+
"action": "start_account_sale",
769+
"params": "0x00"
770+
},
771+
"inputs": [
772+
{
773+
"capacity": "218 CKB",
774+
"lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
775+
"type": "account-cell-type,0x01,0x",
776+
"data": "{ account: mobcion.bit, expired_at: 1670913958 }",
777+
"extraData": "{ status: 0, records_hash: 0x55478d76900611eb079b22088081124ed6c8bae21a05dd1a0d197efcc7c114ce }"
778+
}
779+
],
780+
"outputs": [
781+
{
782+
"capacity": "218 CKB",
783+
"lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
784+
"type": "account-cell-type,0x01,0x",
785+
"data": "{ account: mobcion.bit, expired_at: 1670913958 }",
786+
"extraData": "{ status: 1, records_hash: 0x55478d76900611eb079b22088081124ed6c8bae21a05dd1a0d197efcc7c114ce }"
787+
},
788+
{
789+
"capacity": "201 CKB",
790+
"lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
791+
"type": "account-sale-cell-type,0x01,0x",
792+
"data": "0x1209460ef3cb5f1c68ed2c43a3e020eec2d9de6e...",
793+
"extraData": ""
794+
}
795+
]
796+
}
797+
}
798+
`
799+
800+
func TestComplexTypedData(t *testing.T) {
801+
var td apitypes.TypedData
802+
err := json.Unmarshal([]byte(complexTypedData), &td)
803+
if err != nil {
804+
t.Fatalf("unmarshalling failed '%v'", err)
805+
}
806+
_, sighash, err := sign(td)
807+
if err != nil {
808+
t.Fatal(err)
809+
}
810+
expSigHash := common.FromHex("0x42b1aca82bb6900ff75e90a136de550a58f1a220a071704088eabd5e6ce20446")
811+
if !bytes.Equal(expSigHash, sighash) {
812+
t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
813+
}
814+
}

0 commit comments

Comments
 (0)