Skip to content

Commit 2be8af8

Browse files
authored
feat: raw signer (#23)
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.
1 parent f3261cb commit 2be8af8

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

principal/ed25519/signer/signer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ func (s Ed25519Signer) Encode() []byte {
112112
return s
113113
}
114114

115-
func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
115+
func (s Ed25519Signer) Raw() []byte {
116116
pk := make(ed25519.PrivateKey, ed25519.PrivateKeySize)
117117
copy(pk[0:ed25519.PublicKeySize], s[privateTagSize:pubKeyOffset])
118118
copy(pk[ed25519.PrivateKeySize-ed25519.PublicKeySize:ed25519.PrivateKeySize], s[pubKeyOffset+publicTagSize:pubKeyOffset+publicTagSize+keySize])
119-
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(pk, msg)))
119+
return pk
120+
}
121+
122+
func (s Ed25519Signer) Sign(msg []byte) signature.SignatureView {
123+
return signature.NewSignatureView(signature.NewSignature(signature.EdDSA, ed25519.Sign(s.Raw(), msg)))
120124
}

principal/ed25519/signer/signer_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package signer
22

33
import (
4+
"crypto/ed25519"
45
"fmt"
56
"testing"
7+
8+
"github.com/stretchr/testify/require"
69
)
710

811
func TestGenerateEncodeDecode(t *testing.T) {
@@ -66,3 +69,14 @@ func TestVerify(t *testing.T) {
6669
t.Fatalf("verify failed")
6770
}
6871
}
72+
73+
func TestSignerRaw(t *testing.T) {
74+
s, err := Generate()
75+
require.NoError(t, err)
76+
77+
msg := []byte{1, 2, 3}
78+
raw := s.Raw()
79+
sig := ed25519.Sign(raw, msg)
80+
81+
require.Equal(t, s.Sign(msg).Raw(), sig)
82+
}

principal/lib.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Signer interface {
1212
Code() uint64
1313
Verifier() Verifier
1414
Encode() []byte
15+
// Raw encodes the bytes of the private key without multiformats tags.
16+
Raw() []byte
1517
}
1618

1719
// Verifier is the principal that issued a UCAN. In usually represents remote

principal/rsa/signer/signer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ func (s rsasigner) Encode() []byte {
111111
return s.bytes
112112
}
113113

114+
func (s rsasigner) Raw() []byte {
115+
b, _ := multiformat.UntagWith(Code, s.bytes, 0)
116+
return b
117+
}
118+
114119
func (s rsasigner) Sign(msg []byte) signature.SignatureView {
115120
hash := sha256.New()
116121
hash.Write(msg)

principal/signer/signer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (w wrapsgn) Encode() []byte {
3737
return w.key.Encode()
3838
}
3939

40+
func (w wrapsgn) Raw() []byte {
41+
return w.key.Raw()
42+
}
43+
4044
func (w wrapsgn) Sign(msg []byte) signature.SignatureView {
4145
return w.key.Sign(msg)
4246
}

0 commit comments

Comments
 (0)