diff --git a/protocol/signature_eip712.go b/protocol/signature_eip712.go index d56aed8eb..75e1a55c6 100644 --- a/protocol/signature_eip712.go +++ b/protocol/signature_eip712.go @@ -3,6 +3,7 @@ package protocol import ( _ "embed" "encoding/json" + "fmt" "gitlab.com/accumulatenetwork/accumulate/pkg/types/encoding" ) @@ -11,12 +12,24 @@ func init() { //need to handle the edge cases for Key page operations and data entries encoding.RegisterEnumeratedTypeInterface(NewKeyPageOperation) encoding.RegisterEnumeratedTypeInterface(NewDataEntry) + + encoding.RegisterTypeDefinition(&[]*encoding.TypeField{ + encoding.NewTypeField("publicKey", "bytes"), + encoding.NewTypeField("signer", "string"), + encoding.NewTypeField("signerVersion", "uint64"), + encoding.NewTypeField("timestamp", "uint64"), + encoding.NewTypeField("vote", "string"), + encoding.NewTypeField("memo", "string"), + encoding.NewTypeField("data", "bytes"), + encoding.NewTypeField("delegators", "string[]"), + }, "SignatureMetadata", "signatureMetadata") } func NewEip712TransactionDefinition(txn *Transaction) *encoding.TypeDefinition { txnSchema := &[]*encoding.TypeField{ encoding.NewTypeField("header", "TransactionHeader"), encoding.NewTypeField("body", txn.Body.Type().String()), + encoding.NewTypeField("signature", "SignatureMetadata"), } return &encoding.TypeDefinition{txnSchema} @@ -55,8 +68,33 @@ func MarshalEip712(transaction Transaction) (ret []byte, err error) { return j, nil } // -func Eip712Hasher(txn *Transaction) ([]byte, error) { - j, err := txn.MarshalJSON() +func Eip712Hasher(txn *Transaction, sig Signature) ([]byte, error) { + var delegators []string + var inner *Eip712TypedDataSignature + for inner != nil { + switch s := sig.(type) { + case *DelegatedSignature: + delegators = append(delegators, s.Delegator.String()) + sig = s.Signature + case *Eip712TypedDataSignature: + inner = s + default: + return nil, fmt.Errorf("unsupported signature type %v", s.Type()) + } + } + + j, err := json.Marshal(inner.Metadata()) + if err != nil { + return nil, err + } + var jsig map[string]any + err = json.Unmarshal(j, &jsig) + if err != nil { + return nil, err + } + jsig["delegators"] = delegators + + j, err = txn.MarshalJSON() if err != nil { return nil, err } @@ -66,6 +104,7 @@ func Eip712Hasher(txn *Transaction) ([]byte, error) { if err != nil { return nil, err } + jtx["signature"] = jsig h, err := encoding.Eip712Hash(jtx, "Transaction", NewEip712TransactionDefinition(txn)) if err != nil {