Skip to content

Commit

Permalink
added near signer and verifier (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed Feb 26, 2024
1 parent 57dd5d2 commit da2d0fb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/crypto/signature.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crypto

import (
"crypto/sha256"
"errors"
"fmt"
"strings"
Expand All @@ -19,6 +20,7 @@ const (
SignatureTypeSecp256k1Cometbft SignatureType = "secp256k1_cmt" // secp256k1 cometbft
SignatureTypeEd25519 SignatureType = "ed25519"
SignatureTypeSecp256k1Personal SignatureType = "secp256k1_ep" // secp256k1 ethereum personal_sign
SignatureTypeEd25519Near SignatureType = "ed25519_nr" // ed25519 near
)

const (
Expand All @@ -31,6 +33,7 @@ var SignatureTypeFromName = map[string]SignatureType{
"secp256k1_cmt": SignatureTypeSecp256k1Cometbft, // secp256k1 cometbft
"ed25519": SignatureTypeEd25519, // ed25519 standard, any better name?
"secp256k1_ep": SignatureTypeSecp256k1Personal, // secp256k1 ethereum personal_sign
"ed25519_nr": SignatureTypeEd25519Near, // ed25519 near
}

var (
Expand All @@ -51,7 +54,7 @@ func (s SignatureType) KeyType() KeyType {
switch s {
case SignatureTypeSecp256k1Cometbft, SignatureTypeSecp256k1Personal:
return Secp256k1
case SignatureTypeEd25519:
case SignatureTypeEd25519, SignatureTypeEd25519Near:
return Ed25519
default:
return UnknownKeyType
Expand Down Expand Up @@ -95,6 +98,14 @@ func (s *Signature) Verify(publicKey PublicKey, msg []byte) error {
}
// hash(sha512) is handled by downstream library
return publicKey.Verify(s.Signature, msg)
case SignatureTypeEd25519Near:
if len(s.Signature) != SignatureEd25519Length {
return errInvalidSignature
}

hash := sha256.Sum256(msg)

return publicKey.Verify(s.Signature, hash[:])
default:
return fmt.Errorf("%w: %s", errNotSupportedSignatureType, s.Type.String())
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/crypto/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestSignature_Verify(t *testing.T) {
ed25519Sig := "59b2db2d1e4ce6f8771453cfc78d1f943723528f00fa14adf574600f15c601d591fa2ba29c94d9ed694db324f9e8671bdfbcba4b8e10f6a8733682fa3d115f0c"
ed25519SigBytes, _ := hex.DecodeString(ed25519Sig)

// ed25519 near
ed25519NearSigHex := "089bcf52220dad77abc2cfcb1639bcb2944fdf64e0b173f40cd0d144bdbf7808f4eff3716eb3e98ed40be3ab126e1449d5f57efbe5626673059edc90e9cd9801"
ed25519NearSigBytes, _ := hex.DecodeString(ed25519NearSigHex)

type fields struct {
Signature []byte
Type SignatureType
Expand Down Expand Up @@ -152,6 +156,30 @@ func TestSignature_Verify(t *testing.T) {
},
wantErr: errVerifySignatureFailed,
},
{
name: "ed25519 near valid signature",
fields: fields{
Signature: ed25519NearSigBytes,
Type: SignatureTypeEd25519Near,
},
args: args{
publicKey: ed25519PublicKey,
msg: msg,
},
wantErr: nil,
},
{
name: "ed25519 near invalid signature",
fields: fields{
Signature: ed25519SigBytes,
Type: SignatureTypeEd25519Near,
},
args: args{
publicKey: ed25519PublicKey,
msg: msg,
},
wantErr: errVerifySignatureFailed,
},
{
name: "unsupported signature type",
fields: fields{
Expand Down
29 changes: 29 additions & 0 deletions pkg/crypto/signer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crypto

import (
"crypto/sha256"
"fmt"

ethAccount "github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -127,7 +128,35 @@ func (e *StdEd25519Signer) SignMsg(msg []byte) (*Signature, error) {
}, nil
}

func NewNearSigner(key *Ed25519PrivateKey) *NearEd25519Signer {
return &NearEd25519Signer{
key: key,
}
}

type NearEd25519Signer struct {
key *Ed25519PrivateKey
}

// DefaultSigner returns a default signer for the given private key.

func (n *NearEd25519Signer) PubKey() PublicKey {
return n.key.PubKey()
}

func (n *NearEd25519Signer) SignMsg(msg []byte) (*Signature, error) {
hash := sha256.Sum256(msg)

sig, err := n.key.Sign(hash[:])
if err != nil {
return nil, err
}

return &Signature{
Signature: sig,
Type: SignatureTypeEd25519Near,
}, nil
}
func DefaultSigner(key PrivateKey) Signer {
switch key.Type() {
case Secp256k1:
Expand Down

0 comments on commit da2d0fb

Please sign in to comment.