Skip to content

Commit

Permalink
add support for Ed25519 signature scheme with custom (Keccak512) hash…
Browse files Browse the repository at this point in the history
… function

this is required for NEM support
  • Loading branch information
Jaguar0625 committed Aug 2, 2024
1 parent 348e01b commit e2b0086
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 40 deletions.
4 changes: 2 additions & 2 deletions asserter/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func CurveType(
curve types.CurveType,
) error {
switch curve {
case types.Secp256k1, types.Secp256r1, types.Edwards25519, types.Tweedle, types.Pallas:
case types.Secp256k1, types.Secp256r1, types.Edwards25519, types.Edwards25519_Keccak, types.Tweedle, types.Pallas:
return nil
default:
return ErrCurveTypeNotSupported
Expand Down Expand Up @@ -356,7 +356,7 @@ func SignatureType(
signature types.SignatureType,
) error {
switch signature {
case types.Ecdsa, types.EcdsaRecovery, types.Ed25519, types.Schnorr1, types.SchnorrPoseidon:
case types.Ecdsa, types.EcdsaRecovery, types.Ed25519, types.Ed25519_Keccak, types.Schnorr1, types.SchnorrPoseidon:
return nil
default:
return ErrSignatureTypeNotSupported
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/coinbase/rosetta-sdk-go

go 1.18
go 1.22.5

require (
github.com/DataDog/zstd v1.5.2
github.com/NemProject/nem/gocrypto v0.0.1
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20201201074141-dd0ecada1be6
github.com/btcsuite/btcd v0.22.1
github.com/cenkalti/backoff v2.2.1+incompatible
Expand All @@ -24,7 +25,7 @@ require (
)

require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/bwesterb/go-ristretto v1.2.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand All @@ -45,9 +46,14 @@ require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/coinbase/rosetta-sdk-go/types v1.0.0 => ./types

// temporary until gocrypto is published
replace github.com/NemProject/nem/gocrypto v0.0.1 => ../../NemProject/nem/gocrypto
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
Expand All @@ -14,6 +14,7 @@ github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13P
github.com/btcsuite/btcd v0.22.1 h1:CnwP9LM/M9xuRrGSCGeMVs9iv09uMqwsVX7EeIpgV2c=
github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
Expand All @@ -32,8 +33,6 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/coinbase/kryptology v1.8.0 h1:Aoq4gdTsJhSU3lNWsD5BWmFSz2pE0GlmrljaOxepdYY=
github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI=
github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA=
github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c=
github.com/consensys/bavard v0.1.8-0.20210915155054-088da2f7f54a/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.5.3 h1:4xLFGZR3NWEH2zy+YzvzHicpToQR8FXFbfLNvpGB+rE=
github.com/consensys/gnark-crypto v0.5.3/go.mod h1:hOdPlWQV1gDLp7faZVeg8Y0iEPFaOUnCc4XeCCk96p0=
Expand Down Expand Up @@ -87,6 +86,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77 h1:6xiz3+ZczT3M4+I+JLpcPGG1bQKm8067HktB17EDWEE=
Expand Down Expand Up @@ -155,14 +155,14 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
Expand All @@ -177,20 +177,22 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df h1:5Pf6pFKu98ODmgnpvkJ3kFUOQGGLIzLIkbzUHp47618=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
38 changes: 38 additions & 0 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import (
"fmt"
"math/big"

"golang.org/x/crypto/sha3"

"github.com/btcsuite/btcd/btcec"
"github.com/coinbase/kryptology/pkg/signatures/schnorr/mina"

"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"

nemcrypto "github.com/NemProject/nem/gocrypto"
)

// PrivKeyBytesLen are 32-bytes for all supported curvetypes
Expand Down Expand Up @@ -89,6 +93,19 @@ func ImportPrivateKey(privKeyHex string, curve types.CurveType) (*KeyPair, error
CurveType: curve,
}

keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
}
case types.Edwards25519_Keccak:
hasher := sha3.NewLegacyKeccak512()
rawPrivKey := nemcrypto.NewKeyFromSeed(privKey, hasher)

pubKey := &types.PublicKey{
Bytes: rawPrivKey.Public().(nemcrypto.PublicKey),
CurveType: curve,
}

keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
Expand Down Expand Up @@ -188,6 +205,25 @@ func GenerateKeypair(curve types.CurveType) (*KeyPair, error) {
CurveType: curve,
}

keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
}
case types.Edwards25519_Keccak:
hasher := sha3.NewLegacyKeccak512()
rawPubKey, rawPrivKey, err := nemcrypto.GenerateKey(nil, hasher)
if err != nil {
return nil, fmt.Errorf(
"failed to generate key pair for edwards25519 curve type: %w",
err,
)
}

pubKey := &types.PublicKey{
Bytes: rawPubKey,
CurveType: curve,
}

keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
Expand Down Expand Up @@ -265,6 +301,8 @@ func (k *KeyPair) Signer() (Signer, error) {
return &SignerSecp256k1{k}, nil
case types.Edwards25519:
return &SignerEdwards25519{k}, nil
case types.Edwards25519_Keccak:
return &SignerEdwards25519Keccak{k}, nil
case types.Secp256r1:
return &SignerSecp256r1{k}, nil
case types.Pallas:
Expand Down
26 changes: 23 additions & 3 deletions keys/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ func TestGenerateKeypairEdwards25519(t *testing.T) {
assert.Len(t, keypair.PrivateKey, PrivKeyBytesLen)
}

func TestGenerateKeypairEdwards25519Keccak(t *testing.T) {
curve := types.Edwards25519_Keccak
keypair, err := GenerateKeypair(curve)

assert.NoError(t, err)
assert.Equal(t, keypair.PublicKey.CurveType, curve)
assert.Len(t, keypair.PrivateKey, PrivKeyBytesLen)
}

func TestGenerateKeypairPallas(t *testing.T) {
curve := types.Pallas
keypair, err := GenerateKeypair(curve)
Expand Down Expand Up @@ -129,6 +138,11 @@ func TestImportPrivateKey(t *testing.T) {
types.Edwards25519,
nil,
},
"simple ed25519 (keccak)": {
"aeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebb",
types.Edwards25519_Keccak,
nil,
},
"simple Secp256k1": {
"0b188af56b25d007fbc4bbf2176cd2a54d876ce4774bb5df38b7c83349405b7a",
types.Secp256k1,
Expand All @@ -139,17 +153,23 @@ func TestImportPrivateKey(t *testing.T) {
types.Pallas,
nil,
},
"short ed25519": {"asd", types.Secp256k1, ErrPrivKeyUndecodable},
"short ed25519": {"asd", types.Edwards25519, ErrPrivKeyUndecodable},
"short ed25519 (keccak)": {"asd", types.Edwards25519_Keccak, ErrPrivKeyUndecodable},
"short Secp256k1": {"asd", types.Edwards25519, ErrPrivKeyUndecodable},
"short pallas": {"asd", types.Pallas, ErrPrivKeyUndecodable},
"long ed25519": {
"aeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebbaeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebb", // nolint:lll
types.Secp256k1,
types.Edwards25519,
ErrPrivKeyLengthInvalid,
},
"long ed25519 (keccak)": {
"aeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebbaeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebb", // nolint:lll
types.Edwards25519_Keccak,
ErrPrivKeyLengthInvalid,
},
"long Secp256k1": {
"0b188af56b25d007fbc4bbf2176cd2a54d876ce4774bb5df38b7c83349405b7a0b188af56b25d007fbc4bbf2176cd2a54d876ce4774bb5df38b7c83349405b7a", // nolint:lll
types.Edwards25519,
types.Secp256k1,
ErrPrivKeyLengthInvalid,
},
"long Pallas": {
Expand Down
108 changes: 108 additions & 0 deletions keys/signer_edwards25519_keccak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package keys

import (
"fmt"

"golang.org/x/crypto/sha3"

"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"

nemcrypto "github.com/NemProject/nem/gocrypto"
)

// SignerEdwards25519Keccak is initialized from a keypair
type SignerEdwards25519Keccak struct {
KeyPair *KeyPair
}

var _ Signer = (*SignerEdwards25519Keccak)(nil)

// PublicKey returns the PublicKey of the signer
func (s *SignerEdwards25519Keccak) PublicKey() *types.PublicKey {
return s.KeyPair.PublicKey
}

// Sign arbitrary payloads using a KeyPair
func (s *SignerEdwards25519Keccak) Sign(
payload *types.SigningPayload,
sigType types.SignatureType,
) (*types.Signature, error) {
err := s.KeyPair.IsValid()
if err != nil {
return nil, fmt.Errorf("key pair is invalid: %w", err)
}

if !(payload.SignatureType == types.Ed25519_Keccak || payload.SignatureType == "") {
return nil, fmt.Errorf(
"expected signing payload signature type %v but got %v: %w",
types.Ed25519_Keccak,
payload.SignatureType,
ErrSignUnsupportedPayloadSignatureType,
)
}

if sigType != types.Ed25519_Keccak {
return nil, fmt.Errorf(
"expected signature type %v but got %v: %w",
types.Ed25519_Keccak,
sigType,
ErrSignUnsupportedSignatureType,
)
}

hasher := sha3.NewLegacyKeccak512()
privKeyBytes := s.KeyPair.PrivateKey
privKey := nemcrypto.NewKeyFromSeed(privKeyBytes, hasher)
sig := nemcrypto.Sign(privKey, payload.Bytes, hasher)

return &types.Signature{
SigningPayload: payload,
PublicKey: s.KeyPair.PublicKey,
SignatureType: payload.SignatureType,
Bytes: sig,
}, nil
}

// Verify verifies a Signature, by checking the validity of a Signature,
// the SigningPayload, and the PublicKey of the Signature.
func (s *SignerEdwards25519Keccak) Verify(signature *types.Signature) error {
if signature.SignatureType != types.Ed25519_Keccak {
return fmt.Errorf(
"expected signing payload signature type %v but got %v: %w",
types.Ed25519_Keccak,
signature.SignatureType,
ErrVerifyUnsupportedPayloadSignatureType,
)
}

pubKey := signature.PublicKey.Bytes
message := signature.SigningPayload.Bytes
sig := signature.Bytes
err := asserter.Signatures([]*types.Signature{signature})
if err != nil {
return fmt.Errorf("signature is invalid: %w", err)
}

hasher := sha3.NewLegacyKeccak512()
verify := nemcrypto.Verify(pubKey, message, sig, hasher)
if !verify {
return ErrVerifyFailed
}

return nil
}
Loading

0 comments on commit e2b0086

Please sign in to comment.