Skip to content

Commit c106bc0

Browse files
Merge pull request #196 from kpitapeersyst/address-codec/refactor/ed25519-encoding-type
refactor(address-codec): set ed25519 family seed prefix
2 parents 0601411 + c36136a commit c106bc0

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

address-codec/codec.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,10 @@ func EncodeSeed(entropy []byte, encodingType interfaces.CryptoImplementation) (s
118118
return "", &EncodeLengthError{Instance: "Entropy", Input: len(entropy), Expected: FamilySeedLength}
119119
}
120120

121-
if encodingType == crypto.ED25519() {
122-
prefix := []byte{0x01, 0xe1, 0x4b}
123-
return Encode(entropy, prefix, FamilySeedLength)
121+
if ed25519 := crypto.ED25519(); encodingType == ed25519 {
122+
return Encode(entropy, ed25519.FamilySeedPrefix(), FamilySeedLength)
124123
} else if secp256k1 := crypto.SECP256K1(); encodingType == secp256k1 {
125-
prefix := []byte{secp256k1.FamilySeedPrefix()}
126-
return Encode(entropy, prefix, FamilySeedLength)
124+
return Encode(entropy, secp256k1.FamilySeedPrefix(), FamilySeedLength)
127125
}
128126
return "", errors.New("encoding type must be `ed25519` or `secp256k1`")
129127

pkg/crypto/ed25519.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const (
1313
)
1414

1515
var (
16-
_ Algorithm = &ED25519CryptoAlgorithm{}
16+
_ Algorithm = &ED25519CryptoAlgorithm{}
17+
ed25519FamilySeedPrefix = []byte{0x01, 0xe1, 0x4b}
1718
)
1819

1920
// ED25519CryptoAlgorithm is the implementation of the ED25519 cryptographic algorithm.
2021
type ED25519CryptoAlgorithm struct {
21-
prefix byte
22-
familySeedPrefix byte
22+
prefix byte
2323
}
2424

2525
// ED25519 returns the ED25519 cryptographic algorithm.
@@ -35,8 +35,8 @@ func (c ED25519CryptoAlgorithm) Prefix() byte {
3535
}
3636

3737
// FamilySeedPrefix returns the family seed prefix for the ED25519 cryptographic algorithm.
38-
func (c ED25519CryptoAlgorithm) FamilySeedPrefix() byte {
39-
return c.familySeedPrefix
38+
func (c ED25519CryptoAlgorithm) FamilySeedPrefix() []byte {
39+
return ed25519FamilySeedPrefix
4040
}
4141

4242
// DeriveKeypair derives a keypair from a seed.

pkg/crypto/ed25519_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestED25519_Prefix(t *testing.T) {
1111
}
1212

1313
func TestED25519_FamilySeedPrefix(t *testing.T) {
14-
require.Zero(t, ED25519().FamilySeedPrefix())
14+
require.Equal(t, ed25519FamilySeedPrefix, ED25519().FamilySeedPrefix())
1515
}
1616

1717
func TestED25519DeriveKeypair(t *testing.T) {

pkg/crypto/secp256k1.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@ import (
1414
const (
1515
// SECP256K1 prefix - value is 0
1616
secp256K1Prefix byte = 0x00
17-
// SECP256K1 family seed prefix - value is 33
18-
secp256K1FamilySeedPrefix byte = 0x21
1917
)
2018

2119
var (
2220
_ Algorithm = SECP256K1CryptoAlgorithm{}
21+
// SECP256K1 family seed prefix - value is 33
22+
secp256K1FamilySeedPrefix = []byte{0x21}
2323
)
2424

2525
// SECP256K1CryptoAlgorithm is the implementation of the SECP256K1 algorithm.
2626
type SECP256K1CryptoAlgorithm struct {
27-
prefix byte
28-
familySeedPrefix byte
27+
prefix byte
2928
}
3029

3130
// SECP256K1 returns a new SECP256K1CryptoAlgorithm instance.
3231
func SECP256K1() SECP256K1CryptoAlgorithm {
3332
return SECP256K1CryptoAlgorithm{
34-
prefix: secp256K1Prefix,
35-
familySeedPrefix: secp256K1FamilySeedPrefix,
33+
prefix: secp256K1Prefix,
3634
}
3735
}
3836

@@ -42,8 +40,8 @@ func (c SECP256K1CryptoAlgorithm) Prefix() byte {
4240
}
4341

4442
// FamilySeedPrefix returns the family seed prefix for the SECP256K1 algorithm.
45-
func (c SECP256K1CryptoAlgorithm) FamilySeedPrefix() byte {
46-
return c.familySeedPrefix
43+
func (c SECP256K1CryptoAlgorithm) FamilySeedPrefix() []byte {
44+
return secp256K1FamilySeedPrefix
4745
}
4846

4947
// deriveScalar derives a scalar from a seed.

pkg/crypto/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package crypto
33
// Algorithm defines the interface for cryptographic algorithms used in XRPL key generation and signing.
44
type Algorithm interface {
55
Prefix() byte
6-
FamilySeedPrefix() byte
6+
FamilySeedPrefix() []byte
77
}

0 commit comments

Comments
 (0)