Skip to content

Commit

Permalink
Add signature memos [#3454]
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Oct 18, 2023
1 parent 301747e commit 27a34a0
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 48 deletions.
23 changes: 15 additions & 8 deletions pkg/build/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,24 @@ func (p *parser) parseDataEntry(v ...any) protocol.DataEntry {
return &protocol.AccumulateDataEntry{Data: p.parseDataEntryParts(v...)}
}

func (p *parser) parseBytes(v any) []byte {
switch v := v.(type) {
case string:
return []byte(v)
case []byte:
return v
case [32]byte:
return v[:]
default:
p.errorf(errors.BadRequest, "cannot convert %T to bytes", v)
}
return nil
}

func (p *parser) parseDataEntryParts(v ...any) [][]byte {
var parts [][]byte
for _, v := range v {
switch v := v.(type) {
case string:
parts = append(parts, []byte(v))
case []byte:
parts = append(parts, v)
case [32]byte:
parts = append(parts, v[:])
case []string:
for _, v := range v {
parts = append(parts, []byte(v))
Expand All @@ -95,8 +103,7 @@ func (p *parser) parseDataEntryParts(v ...any) [][]byte {
parts = append(parts, v[:])
}
default:
p.errorf(errors.BadRequest, "cannot convert %T to bytes", v)
return nil
parts = append(parts, p.parseBytes(v))
}
}
return parts
Expand Down
10 changes: 10 additions & 0 deletions pkg/build/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (b SignatureBuilder) Signer(signer any) SignatureBuilder {
return b
}

func (b SignatureBuilder) Memo(s string) SignatureBuilder {
b.signer.Memo = s
return b
}

func (b SignatureBuilder) Metadata(v any) SignatureBuilder {
b.signer.Data = b.parseBytes(v)
return b
}

func (b SignatureBuilder) PrivateKey(key []byte) SignatureBuilder {
b.signer.Signer = signing.PrivateKey(key)
return b
Expand Down
22 changes: 22 additions & 0 deletions pkg/client/signing/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Builder struct {
Version uint64
Vote protocol.VoteType
Timestamp Timestamp
Memo string
Data []byte

// Ignore64Byte (when set) stops the signature builder from automatically
// correcting a transaction header or body that marshals to 64 bytes.
Expand All @@ -49,22 +51,32 @@ func (s *Builder) Import(sig protocol.Signature) (*Builder, error) {
s.Url = sig.Signer
s.Version = sig.SignerVersion
s.Timestamp = TimestampFromValue(sig.Timestamp)
s.Memo = sig.Memo
s.Data = sig.Data
case *protocol.RCD1Signature:
s.Url = sig.Signer
s.Version = sig.SignerVersion
s.Timestamp = TimestampFromValue(sig.Timestamp)
s.Memo = sig.Memo
s.Data = sig.Data
case *protocol.BTCSignature:
s.Url = sig.Signer
s.Version = sig.SignerVersion
s.Timestamp = TimestampFromValue(sig.Timestamp)
s.Memo = sig.Memo
s.Data = sig.Data
case *protocol.BTCLegacySignature:
s.Url = sig.Signer
s.Version = sig.SignerVersion
s.Timestamp = TimestampFromValue(sig.Timestamp)
s.Memo = sig.Memo
s.Data = sig.Data
case *protocol.ETHSignature:
s.Url = sig.Signer
s.Version = sig.SignerVersion
s.Timestamp = TimestampFromValue(sig.Timestamp)
s.Memo = sig.Memo
s.Data = sig.Data
case *protocol.DelegatedSignature:
_, err := s.Import(sig.Signature)
if err != nil {
Expand Down Expand Up @@ -220,6 +232,8 @@ func (s *Builder) prepare(init bool) (protocol.KeySignature, error) {
sig.SignerVersion = s.Version
sig.Timestamp = timestamp
sig.Vote = s.Vote
sig.Memo = s.Memo
sig.Data = s.Data
return sig, s.Signer.SetPublicKey(sig)

case protocol.SignatureTypeRCD1:
Expand All @@ -228,6 +242,8 @@ func (s *Builder) prepare(init bool) (protocol.KeySignature, error) {
sig.SignerVersion = s.Version
sig.Timestamp = timestamp
sig.Vote = s.Vote
sig.Memo = s.Memo
sig.Data = s.Data
return sig, s.Signer.SetPublicKey(sig)

case protocol.SignatureTypeBTC:
Expand All @@ -236,6 +252,8 @@ func (s *Builder) prepare(init bool) (protocol.KeySignature, error) {
sig.SignerVersion = s.Version
sig.Timestamp = timestamp
sig.Vote = s.Vote
sig.Memo = s.Memo
sig.Data = s.Data
return sig, s.Signer.SetPublicKey(sig)

case protocol.SignatureTypeBTCLegacy:
Expand All @@ -244,6 +262,8 @@ func (s *Builder) prepare(init bool) (protocol.KeySignature, error) {
sig.SignerVersion = s.Version
sig.Timestamp = timestamp
sig.Vote = s.Vote
sig.Memo = s.Memo
sig.Data = s.Data
return sig, s.Signer.SetPublicKey(sig)

case protocol.SignatureTypeETH:
Expand All @@ -252,6 +272,8 @@ func (s *Builder) prepare(init bool) (protocol.KeySignature, error) {
sig.SignerVersion = s.Version
sig.Timestamp = timestamp
sig.Vote = s.Vote
sig.Memo = s.Memo
sig.Data = s.Data
return sig, s.Signer.SetPublicKey(sig)

default:
Expand Down
30 changes: 30 additions & 0 deletions protocol/signatures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ RCD1Signature:
- name: TransactionHash
type: hash
optional: true
- name: Memo
type: string
optional: true
- name: Data
type: bytes
optional: true

ED25519Signature:
union: { type: signature }
Expand All @@ -67,6 +73,12 @@ ED25519Signature:
- name: TransactionHash
type: hash
optional: true
- name: Memo
type: string
optional: true
- name: Data
type: bytes
optional: true

BTCSignature:
union: { type: signature }
Expand All @@ -90,6 +102,12 @@ BTCSignature:
- name: TransactionHash
type: hash
optional: true
- name: Memo
type: string
optional: true
- name: Data
type: bytes
optional: true

BTCLegacySignature:
union: { type: signature }
Expand All @@ -113,6 +131,12 @@ BTCLegacySignature:
- name: TransactionHash
type: hash
optional: true
- name: Memo
type: string
optional: true
- name: Data
type: bytes
optional: true

ETHSignature:
union: { type: signature }
Expand All @@ -136,6 +160,12 @@ ETHSignature:
- name: TransactionHash
type: hash
optional: true
- name: Memo
type: string
optional: true
- name: Data
type: bytes
optional: true

ReceiptSignature:
union: { type: signature }
Expand Down
Loading

0 comments on commit 27a34a0

Please sign in to comment.