mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-02-24 08:30:11 +08:00
use struct for SignedEnvelope instead of exposing protobuf directly
This commit is contained in:
parent
f4c9da5023
commit
133f891544
@ -6,58 +6,80 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
||||||
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeEnvelope(privateKey PrivKey, domain string, typeHint []byte, contents []byte) (*pb.SignedEnvelope, error) {
|
type SignedEnvelope struct {
|
||||||
pubKey, err := PublicKeyToProto(privateKey.GetPublic())
|
PublicKey PubKey
|
||||||
if err != nil {
|
TypeHint []byte
|
||||||
return nil, err
|
contents []byte
|
||||||
}
|
signature []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeEnvelope(privateKey PrivKey, domain string, typeHint []byte, contents []byte) (*SignedEnvelope, error) {
|
||||||
toSign := makeSigBuffer(domain, typeHint, contents)
|
toSign := makeSigBuffer(domain, typeHint, contents)
|
||||||
sig, err := privateKey.Sign(toSign)
|
sig, err := privateKey.Sign(toSign)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &pb.SignedEnvelope{
|
return &SignedEnvelope{
|
||||||
PublicKey: pubKey,
|
PublicKey: privateKey.GetPublic(),
|
||||||
TypeHint: typeHint,
|
TypeHint: typeHint,
|
||||||
Contents: contents,
|
contents: contents,
|
||||||
Signature: sig,
|
signature: sig,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateEnvelope(domain string, envelope *pb.SignedEnvelope) (bool, error) {
|
func UnmarshalEnvelope(serializedEnvelope []byte) (*SignedEnvelope, error) {
|
||||||
key, err := PublicKeyFromProto(envelope.PublicKey)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
toVerify := makeSigBuffer(domain, envelope.TypeHint, envelope.Contents)
|
|
||||||
return key.Verify(toVerify, envelope.Signature)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MarshalEnvelope(envelope *pb.SignedEnvelope) ([]byte, error) {
|
|
||||||
return proto.Marshal(envelope)
|
|
||||||
}
|
|
||||||
|
|
||||||
func UnmarshalEnvelope(serializedEnvelope []byte) (*pb.SignedEnvelope, error) {
|
|
||||||
e := pb.SignedEnvelope{}
|
e := pb.SignedEnvelope{}
|
||||||
if err := proto.Unmarshal(serializedEnvelope, &e); err != nil {
|
if err := proto.Unmarshal(serializedEnvelope, &e); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &e, nil
|
key, err := PublicKeyFromProto(e.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &SignedEnvelope{
|
||||||
|
PublicKey: key,
|
||||||
|
TypeHint: e.TypeHint,
|
||||||
|
contents: e.Contents,
|
||||||
|
signature: e.Signature,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenEnvelope(domain string, envelope *pb.SignedEnvelope) ([]byte, error) {
|
func (e *SignedEnvelope) SignerID() (peer.ID, error) {
|
||||||
valid, err := ValidateEnvelope(domain, envelope)
|
return peer.IDFromPublicKey(e.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SignedEnvelope) Validate(domain string) (bool, error) {
|
||||||
|
toVerify := makeSigBuffer(domain, e.TypeHint, e.contents)
|
||||||
|
return e.PublicKey.Verify(toVerify, e.signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SignedEnvelope) Marshal() ([]byte, error) {
|
||||||
|
key, err := PublicKeyToProto(e.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
msg := pb.SignedEnvelope{
|
||||||
|
PublicKey: key,
|
||||||
|
TypeHint: e.TypeHint,
|
||||||
|
Contents: e.contents,
|
||||||
|
Signature: e.signature,
|
||||||
|
}
|
||||||
|
return proto.Marshal(&msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SignedEnvelope) Open(domain string) ([]byte, error) {
|
||||||
|
valid, err := e.Validate(domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !valid {
|
if !valid {
|
||||||
return nil, errors.New("invalid signature")
|
return nil, errors.New("invalid signature or incorrect domain")
|
||||||
}
|
}
|
||||||
return envelope.Contents, nil
|
return e.contents, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeSigBuffer(domain string, typeHint []byte, content []byte) []byte {
|
func makeSigBuffer(domain string, typeHint []byte, content []byte) []byte {
|
||||||
|
Loading…
Reference in New Issue
Block a user