Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions principal/signer/signer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package signer

import (
"bytes"
"fmt"
"strings"

"github.com/multiformats/go-varint"
"github.com/storacha/go-ucanto/did"
"github.com/storacha/go-ucanto/principal"
ed25519signer "github.com/storacha/go-ucanto/principal/ed25519/signer"
rsasigner "github.com/storacha/go-ucanto/principal/rsa/signer"
"github.com/storacha/go-ucanto/principal/verifier"
"github.com/storacha/go-ucanto/ucan/crypto/signature"
)
Expand Down Expand Up @@ -74,3 +78,19 @@ func Wrap(key principal.Signer, id did.DID) (WrappedSigner, error) {
}
return wrapsgn{key, vrf}, nil
}

func Decode(encoded []byte) (principal.Signer, error) {
code, err := varint.ReadUvarint(bytes.NewReader(encoded))
if err != nil {
return nil, fmt.Errorf("reading signer codec: %w", err)
}

switch code {
case ed25519signer.Code:
return ed25519signer.Decode(encoded)
case rsasigner.Code:
return rsasigner.Decode(encoded)
default:
return nil, fmt.Errorf("unsupported signer codec: 0x%x", code)
}
}
22 changes: 22 additions & 0 deletions principal/verifier/verifier.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package verifier

import (
"bytes"
"fmt"
"strings"

"github.com/multiformats/go-varint"
"github.com/storacha/go-ucanto/did"
"github.com/storacha/go-ucanto/principal"
ed25519verifier "github.com/storacha/go-ucanto/principal/ed25519/verifier"
rsaverifier "github.com/storacha/go-ucanto/principal/rsa/verifier"
"github.com/storacha/go-ucanto/ucan/crypto/signature"
)

Expand Down Expand Up @@ -57,3 +61,21 @@ func Wrap(key principal.Verifier, id did.DID) (WrappedVerifier, error) {
}
return wrapvf{id, key}, nil
}

// decodes a multiformat encoded verifier back to the appropriate
// implementation (Ed25519 or RSA) based on the codec prefix.
func Decode(encoded []byte) (principal.Verifier, error) {
code, err := varint.ReadUvarint(bytes.NewReader(encoded))
if err != nil {
return nil, fmt.Errorf("reading verifier codec: %w", err)
}

switch code {
case ed25519verifier.Code:
return ed25519verifier.Decode(encoded)
case rsaverifier.Code:
return rsaverifier.Decode(encoded)
default:
return nil, fmt.Errorf("unsupported verifier codec: 0x%x", code)
}
}