Skip to content

Commit

Permalink
feat: raw signer (#23)
Browse files Browse the repository at this point in the history
This PR adds a `Raw()` method to `Signer`, which allows encoding the
private key without multiformats tags.

It effectively allows a signer generated here to interop with Go
`crypto` and libp2p crypto.

The `Signature` type _already_ has a `Raw()` method (which does the same
thing) so this is in keeping with that.
  • Loading branch information
alanshaw authored Oct 9, 2024
1 parent f3261cb commit 2be8af8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions principal/ed25519/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ func (s Ed25519Signer) Encode() []byte {
return s
}

func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
func (s Ed25519Signer) Raw() []byte {
pk := make(ed25519.PrivateKey, ed25519.PrivateKeySize)
copy(pk[0:ed25519.PublicKeySize], s[privateTagSize:pubKeyOffset])
copy(pk[ed25519.PrivateKeySize-ed25519.PublicKeySize:ed25519.PrivateKeySize], s[pubKeyOffset+publicTagSize:pubKeyOffset+publicTagSize+keySize])
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(pk, msg)))
return pk
}

func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(s.Raw(), msg)))
}
14 changes: 14 additions & 0 deletions principal/ed25519/signer/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package signer

import (
"crypto/ed25519"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestGenerateEncodeDecode(t *testing.T) {
Expand Down Expand Up @@ -66,3 +69,14 @@ func TestVerify(t *testing.T) {
t.Fatalf("verify failed")
}
}

func TestSignerRaw(t *testing.T) {
s, err := Generate()
require.NoError(t, err)

msg := []byte{1, 2, 3}
raw := s.Raw()
sig := ed25519.Sign(raw, msg)

require.Equal(t, s.Sign(msg).Raw(), sig)
}
2 changes: 2 additions & 0 deletions principal/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Signer interface {
Code() uint64
Verifier() Verifier
Encode() []byte
// Raw encodes the bytes of the private key without multiformats tags.
Raw() []byte
}

// Verifier is the principal that issued a UCAN. In usually represents remote
Expand Down
5 changes: 5 additions & 0 deletions principal/rsa/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func (s rsasigner) Encode() []byte {
return s.bytes
}

func (s rsasigner) Raw() []byte {
b, _ := multiformat.UntagWith(Code, s.bytes, 0)
return b
}

func (s rsasigner) Sign(msg []byte) signature.SignatureView {
hash := sha256.New()
hash.Write(msg)
Expand Down
4 changes: 4 additions & 0 deletions principal/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (w wrapsgn) Encode() []byte {
return w.key.Encode()
}

func (w wrapsgn) Raw() []byte {
return w.key.Raw()
}

func (w wrapsgn) Sign(msg []byte) signature.SignatureView {
return w.key.Sign(msg)
}
Expand Down

0 comments on commit 2be8af8

Please sign in to comment.