sonr/crypto/tecdsa/dklsv1/signserializers.go
Prad Nukala 807b2e86ec
feature/1220 origin handle exists method (#1241)
* feat: add docs and CI workflow for publishing to onsonr.dev

* (refactor): Move hway,motr executables to their own repos

* feat: simplify devnet and testnet configurations

* refactor: update import path for didcrypto package

* docs(networks): Add README with project overview, architecture, and community links

* refactor: Move network configurations to deploy directory

* build: update golang version to 1.23

* refactor: move logger interface to appropriate package

* refactor: Move devnet configuration to networks/devnet

* chore: improve release process with date variable

* (chore): Move Crypto Library

* refactor: improve code structure and readability in DID module

* feat: integrate Trunk CI checks

* ci: optimize CI workflow by removing redundant build jobs

---------

Co-authored-by: Darp Alakun <i@prad.nu>
2025-01-06 17:06:10 +00:00

123 lines
3.7 KiB
Go
Executable File

package dklsv1
import (
"bytes"
"encoding/gob"
"github.com/pkg/errors"
"github.com/onsonr/sonr/crypto/core/curves"
"github.com/onsonr/sonr/crypto/core/protocol"
"github.com/onsonr/sonr/crypto/tecdsa/dklsv1/sign"
)
func newSignProtocolMessage(payload []byte, round string, version uint) *protocol.Message {
return &protocol.Message{
Protocol: protocol.Dkls18Sign,
Version: version,
Payloads: map[string][]byte{payloadKey: payload},
Metadata: map[string]string{"round": round},
}
}
func encodeSignRound1Output(commitment [32]byte, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(&commitment); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "1", version), nil
}
func decodeSignRound2Input(m *protocol.Message) ([32]byte, error) {
if m.Version != protocol.Version1 {
return [32]byte{}, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := [32]byte{}
if err := dec.Decode(&decoded); err != nil {
return [32]byte{}, errors.WithStack(err)
}
return decoded, nil
}
func encodeSignRound2Output(output *sign.SignRound2Output, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(output); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "2", version), nil
}
func decodeSignRound3Input(m *protocol.Message) (*sign.SignRound2Output, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := &sign.SignRound2Output{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeSignRound3Output(output *sign.SignRound3Output, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(output); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "3", version), nil
}
func decodeSignRound4Input(m *protocol.Message) (*sign.SignRound3Output, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := &sign.SignRound3Output{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeSignature(signature *curves.EcdsaSignature, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(signature); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "signature", version), nil
}
// DecodeSignature serializes the signature.
func DecodeSignature(m *protocol.Message) (*curves.EcdsaSignature, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := &curves.EcdsaSignature{}
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}