Skip to content

Commit b26e379

Browse files
committed
Add initial prefixed signer changes
1 parent 2e08944 commit b26e379

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

pkg/types/core/keystore.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto"
66
"crypto/ed25519"
77
"crypto/rand"
8+
"crypto/sha256"
89
"encoding/hex"
910
"fmt"
1011
"io"
@@ -72,28 +73,40 @@ func (s *Ed25519Signer) Sign(r io.Reader, digest []byte, opts crypto.SignerOpts)
7273
}
7374

7475
var P2PAccountKey = "P2P_SIGNER"
76+
var StandardCapabilitiesPrefix = "STANDARD_CAPABILITIES_MESSAGE_"
7577

76-
// singleAccountSigner implements Keystore for a single account.
77-
type singleAccountSigner struct {
78+
// prefixedSingleAccountSigner implements Keystore for a single account.
79+
type prefixedSingleAccountSigner struct {
80+
prefix string
7881
account *string
7982
signer crypto.Signer
8083
}
8184

82-
var _ Keystore = &singleAccountSigner{}
85+
var _ Keystore = &prefixedSingleAccountSigner{}
8386

84-
func NewSingleAccountSigner(account *string, signer crypto.Signer) (*singleAccountSigner, error) {
85-
return &singleAccountSigner{account: account, signer: signer}, nil
87+
func NewPrefixedSingleAccountSigner(account *string, signer crypto.Signer, prefix string) (*prefixedSingleAccountSigner, error) {
88+
return &prefixedSingleAccountSigner{account: account, signer: signer, prefix: prefix}, nil
8689
}
87-
func (c *singleAccountSigner) Accounts(ctx context.Context) (accounts []string, err error) {
90+
91+
func (c *prefixedSingleAccountSigner) Accounts(ctx context.Context) (accounts []string, err error) {
8892
if c.account == nil {
8993
return nil, fmt.Errorf("account is nil")
9094
}
9195

9296
return []string{*c.account}, nil
9397
}
94-
func (c *singleAccountSigner) Sign(ctx context.Context, account string, data []byte) (signed []byte, err error) {
98+
99+
// Sign returns data signed by the single account.
100+
// Data is prefixed with c.prefix and then hashed with SHA-256 before signing.
101+
func (c *prefixedSingleAccountSigner) Sign(ctx context.Context, account string, data []byte) (signed []byte, err error) {
95102
if c.account != nil && *c.account == account {
96-
return c.signer.Sign(rand.Reader, data, crypto.Hash(0))
103+
return c.signer.Sign(rand.Reader, CalcPrefixedHash(c.prefix, data), crypto.Hash(0))
97104
}
98105
return nil, fmt.Errorf("account not found: %s", account)
99106
}
107+
108+
func CalcPrefixedHash(prefix string, data []byte) []byte {
109+
prefixedData := append([]byte(prefix), data...)
110+
hash := sha256.Sum256(prefixedData)
111+
return hash[:]
112+
}

0 commit comments

Comments
 (0)