Skip to content

Commit

Permalink
applied patch
Browse files Browse the repository at this point in the history
  • Loading branch information
bunfield committed Jul 9, 2024
1 parent 501e7ce commit ffc3586
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions protocol/signature_eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package protocol
import (
_ "embed"
"encoding/json"
"fmt"

"gitlab.com/accumulatenetwork/accumulate/pkg/types/encoding"
)
Expand All @@ -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}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit ffc3586

Please sign in to comment.