Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Signature Error #296

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package starknetgo
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"math/big"
"regexp"
Expand Down Expand Up @@ -35,30 +36,37 @@ type Definition struct {
}

type TypedMessage interface {
FmtDefinitionEncoding(string) []*big.Int
FmtDefinitionEncoding(string) ([]*big.Int, error)
}

/*
encoding definition for standard StarkNet Domain messages
*/
func (dm Domain) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int) {
processStrToBig := func(fieldVal string) {
felt := strToFelt(fieldVal)
bigInt, ok := feltToBig(felt)
if ok {
fmtEnc = append(fmtEnc, bigInt)
func (dm Domain) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int, err error) {

strToBig := func(str string) (*big.Int, error) {
bigInt, ok := new(big.Int).SetString(str, 0)
if !ok {
err = errors.New("Error setting string to big.Int in FmtDefinitionEncoding()")
return nil, err
}
return bigInt, nil
}

var bigInt *big.Int
switch field {
case "name":
processStrToBig(dm.Name)
bigInt, err = strToBig(dm.Name)
case "version":
processStrToBig(dm.Version)
bigInt, err = strToBig(dm.Version)
case "chainId":
processStrToBig(dm.ChainId)
bigInt, err = strToBig(dm.ChainId)
}

if err != nil {
fmtEnc = append(fmtEnc, bigInt)
}
return fmtEnc
return fmtEnc, err
}

// strToFelt converts a string containing a decimal, hexadecimal or UTF8 charset into a Felt.
Expand Down Expand Up @@ -140,15 +148,23 @@ func (td TypedData) GetTypedMessageHash(inType string, msg TypedMessage, sc Star

for _, def := range prim.Definitions {
if def.Type == "felt" {
fmtDefinitions := msg.FmtDefinitionEncoding(def.Name)
fmtDefinitions, errFmt := msg.FmtDefinitionEncoding(def.Name)
if errFmt != nil {
err = errFmt
return
}
elements = append(elements, fmtDefinitions...)
continue
}

innerElements := []*big.Int{}
encType := td.Types[def.Type]
innerElements = append(innerElements, encType.Encoding)
fmtDefinitions := msg.FmtDefinitionEncoding(def.Name)
fmtDefinitions, errFmt := msg.FmtDefinitionEncoding(def.Name)
if errFmt != nil {
err = errFmt
return
}
innerElements = append(innerElements, fmtDefinitions...)
innerElements = append(innerElements, big.NewInt(int64(len(innerElements))))

Expand Down
4 changes: 2 additions & 2 deletions typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Person struct {
Wallet string
}

func (mail Mail) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int) {
func (mail Mail) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int, err error) {
if field == "from" {
fmtEnc = append(fmtEnc, types.UTF8StrToBig(mail.From.Name))
fmtEnc = append(fmtEnc, types.HexToBN(mail.From.Wallet))
Expand All @@ -29,7 +29,7 @@ func (mail Mail) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int) {
} else if field == "contents" {
fmtEnc = append(fmtEnc, types.UTF8StrToBig(mail.Contents))
}
return fmtEnc
return fmtEnc, nil
}

func MockTypedData() (ttd TypedData) {
Expand Down
Loading