Skip to content

Commit

Permalink
refactor Intent.isValidSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Feb 6, 2024
1 parent f386eae commit 6cf62ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 57 deletions.
28 changes: 15 additions & 13 deletions intents/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type KeyType int
const (
KeyTypeSECP256K1 KeyType = iota
KeyTypeSECP256R1
KeyTypeUnknown
)

type Intent struct {
Expand Down Expand Up @@ -130,32 +131,33 @@ func (intent *Intent) IsValid() (bool, error) {
return true, nil
}

func (intent *Intent) isValidP256R1Session(sessionId string, signature string) bool {
func (intent *Intent) keyType(sessionId string) KeyType {
// handle empty session ids
if len(sessionId) == 0 {
return KeyTypeUnknown
}

// handle old session ids
if len(sessionId) <= 42 {
return false
return KeyTypeSECP256K1
}

// handle key typed session ids
sessionIdBytes := common.FromHex(sessionId)
switch KeyType(sessionIdBytes[0]) {
return KeyType(sessionIdBytes[0])
}

func (intent *Intent) isValidSignature(sessionId string, signature string) bool {
switch intent.keyType(sessionId) {
case KeyTypeSECP256K1:
return false
return intent.isValidSignatureP256K1(sessionId, signature)
case KeyTypeSECP256R1:
return true
return intent.isValidSignatureP256R1(sessionId, signature)
default:
return false
}
}

func (intent *Intent) isValidSignature(session string, signature string) bool {
if intent.isValidP256R1Session(session, signature) {
return intent.isValidSignatureP256R1(session, signature)
} else {
return intent.isValidSignatureP256K1(session, signature)
}
}

// isValidSignatureP256K1 checks if the signature is valid for the given secp256k1 session
func (intent *Intent) isValidSignatureP256K1(sessionAddress string, signature string) bool {
// handle typed session address
Expand Down
44 changes: 0 additions & 44 deletions intents/intent_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package intents

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"encoding/json"
"fmt"
"math/big"
"testing"

"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -153,41 +147,3 @@ func TestParseAndRecoverIntent_SessionKeyP256K1Typed(t *testing.T) {
assert.NotNil(t, hash2)
assert.Equal(t, common.Bytes2Hex(hash2), "893060f818437f8e3d9b4d8e103c5eb3c325fa25dd0221fb7b61cca6dd03a79e")
}

func TestECDSAP256SessionSig(t *testing.T) {
//sessionId := "r1:0x04d0ca179fec17ca13e0a05dfee4dd6e56c4f14975001ab093f6306f96f915c04d437047e1ca0244080fb2741d0518fe78c9e893c2a5b7d0549816198414c39335"
//message := "0x7b2270726f6a6563744964223a31312c226964546f6b656e223a2265794a68624763694f694a53557a49314e694973496d74705a434936496a67315a5455314d5441334e445932596a646c4d6a6b344d7a59784f546c6a4e54686a4e7a55344d575931596a6b794d324a6c4e4451694c434a30655841694f694a4b5631516966512e65794a7063334d694f694a6f64485277637a6f764c32466a59323931626e527a4c6d6476623264735a53356a623230694c434a68656e41694f6949354e7a41354f4463334e5459324e6a41744d7a56684e6e526a4e44686f646d6b34593256324f574e75613235774d476c315a335935634739684d6a4d75595842776379356e6232396e624756316332567959323975644756756443356a623230694c434a68645751694f6949354e7a41354f4463334e5459324e6a41744d7a56684e6e526a4e44686f646d6b34593256324f574e75613235774d476c315a335935634739684d6a4d75595842776379356e6232396e624756316332567959323975644756756443356a623230694c434a7a645749694f6949784d5451324f4455344d5467784e4441784f44597a4e446b304d4451694c434a6c6257467062434936496d3168636d6c75627a4d355147647459576c734c6d4e7662534973496d567459576c7358335a6c636d6c6d6157566b496a7030636e566c4c434a75596d59694f6a45334d4459334f44677a4f546773496d3568625755694f694a4e59584a6a6157346752384f7a636e70357859527a61326b694c434a7761574e3064584a6c496a6f696148523063484d364c79397361444d755a3239765a32786c64584e6c636d4e76626e526c626e5175593239744c32457651554e6e4f47396a536b464c623031435956424e5a3064494e3246355a6e644e54485236596931786431706f526b524b527a645656485a70556c705854566f7850584d354e69316a496977695a326c325a573566626d46745a534936496b3168636d4e7062694973496d5a6862576c73655639755957316c496a6f6952384f7a636e70357859527a61326b694c434a7362324e68624755694f694a7762434973496d6c68644349364d5463774e6a63344f4459354f4377695a586877496a6f784e7a41324e7a6b794d6a6b344c434a7164476b694f6949314e4759315a5745334d6a646a4f546b314f544a684e446b784e6d497a59574d31593245344f4755334e7a637a596a677a4e546b35496e302e4d346c73304f434d6a30586d4152354c7358385852707074644f55724e4a34346845364d476e4e664770334d444e495642396e7043524567386d64415033674f427171784e4d4e3464566d50734d334e5a54385779575a546871616e736254484a6d586e556d355245767357675f456d7954525a436d486545414c4e6877636e57477a386d375636664a7a7050366543554474534942722d376832756f364f637a6b704c534b382d74324569796b533773706c54475952486f42427265364d787762685044377074574a767957466c6f35594a617a74574c347454376c795957504264504470334456555743687679622d707944654e2d5066756530716a333165326b7448756c6b6a576b7a35325a5a73723758314876734164767642436838614c5f3553354f2d314a3649475f455f4e497168373254596f4141683968374e7639474c706e746a6d5a566764687769755f6f485067222c2273657373696f6e41646472657373223a2272313a307830346430636131373966656331376361313365306130356466656534646436653536633466313439373530303161623039336636333036663936663931356330346434333730343765316361303234343038306662323734316430353138666537386339653839336332613562376430353439383136313938343134633339333335222c22667269656e646c794e616d65223a22f09f95b72072697475616c2076696c6c616765222c22696e74656e744a534f4e223a227b5c2276657273696f6e5c223a5c22302e302e302d646576315c222c5c227061636b65745c223a7b5c226973737565645c223a313730363738383639392c5c22657870697265735c223a313730363738383939392c5c22636f64655c223a5c226f70656e53657373696f6e5c222c5c2273657373696f6e5c223a5c2272313a3078303464306361313739666563313763613133653061303564666565346464366535366334663134393735303031616230393366363330366639366639313563303464343337303437653163613032343430383066623237343164303531386665373863396538393363326135623764303534393831363139383431346333393333355c222c5c2270726f6f665c223a7b5c226964546f6b656e5c223a5c2265794a68624763694f694a53557a49314e694973496d74705a434936496a67315a5455314d5441334e445932596a646c4d6a6b344d7a59784f546c6a4e54686a4e7a55344d575931596a6b794d324a6c4e4451694c434a30655841694f694a4b5631516966512e65794a7063334d694f694a6f64485277637a6f764c32466a59323931626e527a4c6d6476623264735a53356a623230694c434a68656e41694f6949354e7a41354f4463334e5459324e6a41744d7a56684e6e526a4e44686f646d6b34593256324f574e75613235774d476c315a335935634739684d6a4d75595842776379356e6232396e624756316332567959323975644756756443356a623230694c434a68645751694f6949354e7a41354f4463334e5459324e6a41744d7a56684e6e526a4e44686f646d6b34593256324f574e75613235774d476c315a335935634739684d6a4d75595842776379356e6232396e624756316332567959323975644756756443356a623230694c434a7a645749694f6949784d5451324f4455344d5467784e4441784f44597a4e446b304d4451694c434a6c6257467062434936496d3168636d6c75627a4d355147647459576c734c6d4e7662534973496d567459576c7358335a6c636d6c6d6157566b496a7030636e566c4c434a75596d59694f6a45334d4459334f44677a4f546773496d3568625755694f694a4e59584a6a6157346752384f7a636e70357859527a61326b694c434a7761574e3064584a6c496a6f696148523063484d364c79397361444d755a3239765a32786c64584e6c636d4e76626e526c626e5175593239744c32457651554e6e4f47396a536b464c623031435956424e5a3064494e3246355a6e644e54485236596931786431706f526b524b527a645656485a70556c705854566f7850584d354e69316a496977695a326c325a573566626d46745a534936496b3168636d4e7062694973496d5a6862576c73655639755957316c496a6f6952384f7a636e70357859527a61326b694c434a7362324e68624755694f694a7762434973496d6c68644349364d5463774e6a63344f4459354f4377695a586877496a6f784e7a41324e7a6b794d6a6b344c434a7164476b694f6949314e4759315a5745334d6a646a4f546b314f544a684e446b784e6d497a59574d31593245344f4755334e7a637a596a677a4e546b35496e302e4d346c73304f434d6a30586d4152354c7358385852707074644f55724e4a34346845364d476e4e664770334d444e495642396e7043524567386d64415033674f427171784e4d4e3464566d50734d334e5a54385779575a546871616e736254484a6d586e556d355245767357675f456d7954525a436d486545414c4e6877636e57477a386d375636664a7a7050366543554474534942722d376832756f364f637a6b704c534b382d74324569796b533773706c54475952486f42427265364d787762685044377074574a767957466c6f35594a617a74574c347454376c795957504264504470334456555743687679622d707944654e2d5066756530716a333165326b7448756c6b6a576b7a35325a5a73723758314876734164767642436838614c5f3553354f2d314a3649475f455f4e497168373254596f4141683968374e7639474c706e746a6d5a566764687769755f6f4850675c227d7d2c5c227369676e6174757265735c223a5b5d7d227d"
//signature := "r1:0x5ca339d779449af10bc0dd74eb7e1ff6f9b584a52681bbb6dc27bb8923ec3ef629cdb9b17d4b6ed44fd125c4bb9376f869a2478513326613b03eb80a2bbfaeae"

sessionId := "r1:0x040714f2ed82b5748ba30e3d81df81d481371b20c43cdbec81a89cbdb74e149e73ee083a1306328236c7de6d26b6f8d4494951d7423946422a04700ed182092a45"
message := "0x7a7e5a0913e63cac5886afcafedba93b17baae3eb4066534ffdd5e3da3e8c714"
signature := "r1:0x4038376385b045c19754bb69fa6cde925674778e6a1a78b8fa3135ec96b695aef5b8126c78dc17a2cc0be522a4e6154bf5152c908d763fb1c28e47cf419a3ea5"

fmt.Println(len(sessionId))

// get public key from sessionId
sessionIdBuff := common.FromHex(sessionId[3:])

x, y := elliptic.Unmarshal(elliptic.P256(), sessionIdBuff)
assert.NotNil(t, x)

pub := ecdsa.PublicKey{
Curve: elliptic.P256(),
X: x,
Y: y,
}

spew.Dump(pub)

// get message hash
messageBytes := common.FromHex(message)
messageHash := sha256.Sum256(messageBytes)

// get signature
signatureBytes := common.FromHex(signature[3:])

r := new(big.Int).SetBytes(signatureBytes[:32])
s := new(big.Int).SetBytes(signatureBytes[32:64])
verified := ecdsa.Verify(&pub, messageHash[:], r, s)
assert.True(t, verified)
}

0 comments on commit 6cf62ee

Please sign in to comment.