mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-26 05:32:17 +02:00
aezeed: fix typos and formatting
This commit is contained in:
@@ -62,7 +62,7 @@ const (
|
|||||||
// be seen as the size of the equivalent MAC.
|
// be seen as the size of the equivalent MAC.
|
||||||
CipherTextExpansion = 4
|
CipherTextExpansion = 4
|
||||||
|
|
||||||
// EntropySize is the number of bytes of entropy we'll use the generate
|
// EntropySize is the number of bytes of entropy we'll use to generate
|
||||||
// the seed.
|
// the seed.
|
||||||
EntropySize = 16
|
EntropySize = 16
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ const (
|
|||||||
|
|
||||||
// adSize is the size of the encoded associated data that will be
|
// adSize is the size of the encoded associated data that will be
|
||||||
// passed into aez when enciphering and deciphering the seed. The AD
|
// passed into aez when enciphering and deciphering the seed. The AD
|
||||||
// itself (associated data) is just the CipherSeedVersion and salt.
|
// itself (associated data) is just the cipher seed version and salt.
|
||||||
adSize = 6
|
adSize = 6
|
||||||
|
|
||||||
// checkSumSize is the size of the checksum applied to the final
|
// checkSumSize is the size of the checksum applied to the final
|
||||||
@@ -93,7 +93,7 @@ const (
|
|||||||
// We encode our mnemonic using 24 words, so 264 bits (33 bytes).
|
// We encode our mnemonic using 24 words, so 264 bits (33 bytes).
|
||||||
BitsPerWord = 11
|
BitsPerWord = 11
|
||||||
|
|
||||||
// saltOffset is the index within an enciphered cipherseed that marks
|
// saltOffset is the index within an enciphered cipher seed that marks
|
||||||
// the start of the salt.
|
// the start of the salt.
|
||||||
saltOffset = EncipheredCipherSeedSize - checkSumSize - saltSize
|
saltOffset = EncipheredCipherSeedSize - checkSumSize - saltSize
|
||||||
|
|
||||||
@@ -103,8 +103,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Below at the default scrypt parameters that are tied to
|
// Below at the default scrypt parameters that are tied to cipher seed
|
||||||
// CipherSeedVersion zero.
|
// version zero.
|
||||||
scryptN = 32768
|
scryptN = 32768
|
||||||
scryptR = 8
|
scryptR = 8
|
||||||
scryptP = 1
|
scryptP = 1
|
||||||
@@ -129,7 +129,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CipherSeed is a fully decoded instance of the aezeed scheme. At a high
|
// CipherSeed is a fully decoded instance of the aezeed scheme. At a high
|
||||||
// level, the encoded cipherseed is the enciphering of: a version byte, a set
|
// level, the encoded cipher seed is the enciphering of: a version byte, a set
|
||||||
// of bytes for a timestamp, the entropy which will be used to directly
|
// of bytes for a timestamp, the entropy which will be used to directly
|
||||||
// construct the HD seed, and finally a checksum over the rest. This scheme was
|
// construct the HD seed, and finally a checksum over the rest. This scheme was
|
||||||
// created as the widely used schemes in the space lack two critical traits: a
|
// created as the widely used schemes in the space lack two critical traits: a
|
||||||
@@ -151,7 +151,7 @@ var (
|
|||||||
// users can encrypt the raw "plaintext" seed under distinct passwords to
|
// users can encrypt the raw "plaintext" seed under distinct passwords to
|
||||||
// produce unique mnemonic phrases.
|
// produce unique mnemonic phrases.
|
||||||
type CipherSeed struct {
|
type CipherSeed struct {
|
||||||
// InternalVersion is the version of the plaintext cipherseed. This is
|
// InternalVersion is the version of the plaintext cipher seed. This is
|
||||||
// to be used by wallets to determine if the seed version is compatible
|
// to be used by wallets to determine if the seed version is compatible
|
||||||
// with the derivation schemes they know.
|
// with the derivation schemes they know.
|
||||||
InternalVersion uint8
|
InternalVersion uint8
|
||||||
@@ -178,7 +178,7 @@ type CipherSeed struct {
|
|||||||
func New(internalVersion uint8, entropy *[EntropySize]byte,
|
func New(internalVersion uint8, entropy *[EntropySize]byte,
|
||||||
now time.Time) (*CipherSeed, error) {
|
now time.Time) (*CipherSeed, error) {
|
||||||
|
|
||||||
// TODO(roasbeef): pass randomness source? to make fully determinsitc?
|
// TODO(roasbeef): pass randomness source? to make fully deterministic?
|
||||||
|
|
||||||
// If a set of entropy wasn't provided, then we'll read a set of bytes
|
// If a set of entropy wasn't provided, then we'll read a set of bytes
|
||||||
// from the CSPRNG of our operating platform.
|
// from the CSPRNG of our operating platform.
|
||||||
@@ -272,11 +272,13 @@ func extractAD(encipheredSeed [EncipheredCipherSeedSize]byte) [adSize]byte {
|
|||||||
return ad
|
return ad
|
||||||
}
|
}
|
||||||
|
|
||||||
// encipher takes a fully populated cipherseed instance, and enciphers the
|
// encipher takes a fully populated cipher seed instance, and enciphers the
|
||||||
// encoded seed, then appends a randomly generated seed used to stretch the
|
// encoded seed, then appends a randomly generated seed used to stretch the
|
||||||
// passphrase out into an appropriate key, then computes a checksum over the
|
// passphrase out into an appropriate key, then computes a checksum over the
|
||||||
// preceding.
|
// preceding.
|
||||||
func (c *CipherSeed) encipher(pass []byte) ([EncipheredCipherSeedSize]byte, error) {
|
func (c *CipherSeed) encipher(pass []byte) ([EncipheredCipherSeedSize]byte,
|
||||||
|
error) {
|
||||||
|
|
||||||
var cipherSeedBytes [EncipheredCipherSeedSize]byte
|
var cipherSeedBytes [EncipheredCipherSeedSize]byte
|
||||||
|
|
||||||
// If the passphrase wasn't provided, then we'll use the string
|
// If the passphrase wasn't provided, then we'll use the string
|
||||||
@@ -295,7 +297,7 @@ func (c *CipherSeed) encipher(pass []byte) ([EncipheredCipherSeedSize]byte, erro
|
|||||||
return cipherSeedBytes, err
|
return cipherSeedBytes, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, we'll encode the serialized plaintext cipherseed into a buffer
|
// Next, we'll encode the serialized plaintext cipher seed into a buffer
|
||||||
// that we'll use for encryption.
|
// that we'll use for encryption.
|
||||||
var seedBytes bytes.Buffer
|
var seedBytes bytes.Buffer
|
||||||
if err := c.encode(&seedBytes); err != nil {
|
if err := c.encode(&seedBytes); err != nil {
|
||||||
@@ -335,7 +337,9 @@ func (c *CipherSeed) encipher(pass []byte) ([EncipheredCipherSeedSize]byte, erro
|
|||||||
|
|
||||||
// cipherTextToMnemonic converts the aez ciphertext appended with the salt to a
|
// cipherTextToMnemonic converts the aez ciphertext appended with the salt to a
|
||||||
// 24-word mnemonic pass phrase.
|
// 24-word mnemonic pass phrase.
|
||||||
func cipherTextToMnemonic(cipherText [EncipheredCipherSeedSize]byte) (Mnemonic, error) {
|
func cipherTextToMnemonic(cipherText [EncipheredCipherSeedSize]byte) (Mnemonic,
|
||||||
|
error) {
|
||||||
|
|
||||||
var words [NumMnemonicWords]string
|
var words [NumMnemonicWords]string
|
||||||
|
|
||||||
// First, we'll convert the ciphertext itself into a bitstream for easy
|
// First, we'll convert the ciphertext itself into a bitstream for easy
|
||||||
@@ -356,7 +360,7 @@ func cipherTextToMnemonic(cipherText [EncipheredCipherSeedSize]byte) (Mnemonic,
|
|||||||
return words, nil
|
return words, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToMnemonic maps the final enciphered cipher seed to a human readable 24-word
|
// ToMnemonic maps the final enciphered cipher seed to a human-readable 24-word
|
||||||
// mnemonic phrase. The password is optional, as if it isn't specified aezeed
|
// mnemonic phrase. The password is optional, as if it isn't specified aezeed
|
||||||
// will be used in its place.
|
// will be used in its place.
|
||||||
func (c *CipherSeed) ToMnemonic(pass []byte) (Mnemonic, error) {
|
func (c *CipherSeed) ToMnemonic(pass []byte) (Mnemonic, error) {
|
||||||
@@ -374,7 +378,9 @@ func (c *CipherSeed) ToMnemonic(pass []byte) (Mnemonic, error) {
|
|||||||
|
|
||||||
// Encipher maps the cipher seed to an aez ciphertext using an optional
|
// Encipher maps the cipher seed to an aez ciphertext using an optional
|
||||||
// passphrase.
|
// passphrase.
|
||||||
func (c *CipherSeed) Encipher(pass []byte) ([EncipheredCipherSeedSize]byte, error) {
|
func (c *CipherSeed) Encipher(pass []byte) ([EncipheredCipherSeedSize]byte,
|
||||||
|
error) {
|
||||||
|
|
||||||
return c.encipher(pass)
|
return c.encipher(pass)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +391,7 @@ func (c *CipherSeed) BirthdayTime() time.Time {
|
|||||||
return BitcoinGenesisDate.Add(offset)
|
return BitcoinGenesisDate.Add(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mnemonic is a 24-word passphrase as of CipherSeedVersion zero. This
|
// Mnemonic is a 24-word passphrase as of cipher seed version zero. This
|
||||||
// passphrase encodes an encrypted seed triple (version, birthday, entropy).
|
// passphrase encodes an encrypted seed triple (version, birthday, entropy).
|
||||||
// Additionally, we also encode the salt used with scrypt to derive the key
|
// Additionally, we also encode the salt used with scrypt to derive the key
|
||||||
// that the cipher text is encrypted with, and the version which tells us how
|
// that the cipher text is encrypted with, and the version which tells us how
|
||||||
@@ -465,7 +471,9 @@ func decipherCipherSeed(cipherSeedBytes [EncipheredCipherSeedSize]byte,
|
|||||||
|
|
||||||
// Before we perform any crypto operations, we'll re-create and verify
|
// Before we perform any crypto operations, we'll re-create and verify
|
||||||
// the checksum to ensure that the user input the proper set of words.
|
// the checksum to ensure that the user input the proper set of words.
|
||||||
freshChecksum := crc32.Checksum(cipherSeedBytes[:checkSumOffset], crcTable)
|
freshChecksum := crc32.Checksum(
|
||||||
|
cipherSeedBytes[:checkSumOffset], crcTable,
|
||||||
|
)
|
||||||
if freshChecksum != binary.BigEndian.Uint32(checksum) {
|
if freshChecksum != binary.BigEndian.Uint32(checksum) {
|
||||||
return plainSeed, ErrIncorrectMnemonic
|
return plainSeed, ErrIncorrectMnemonic
|
||||||
}
|
}
|
||||||
@@ -499,7 +507,8 @@ func decipherCipherSeed(cipherSeedBytes [EncipheredCipherSeedSize]byte,
|
|||||||
// Decipher attempts to decipher the encoded mnemonic by first mapping to the
|
// Decipher attempts to decipher the encoded mnemonic by first mapping to the
|
||||||
// original ciphertext, then applying our deciphering scheme. ErrInvalidPass
|
// original ciphertext, then applying our deciphering scheme. ErrInvalidPass
|
||||||
// will be returned if the passphrase is incorrect.
|
// will be returned if the passphrase is incorrect.
|
||||||
func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte, error) {
|
func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte,
|
||||||
|
error) {
|
||||||
|
|
||||||
// Before we attempt to map the mnemonic back to the original
|
// Before we attempt to map the mnemonic back to the original
|
||||||
// ciphertext, we'll ensure that all the word are actually a part of
|
// ciphertext, we'll ensure that all the word are actually a part of
|
||||||
@@ -512,7 +521,7 @@ func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte, error)
|
|||||||
for i, word := range m {
|
for i, word := range m {
|
||||||
if _, ok := wordDict[word]; !ok {
|
if _, ok := wordDict[word]; !ok {
|
||||||
emptySeed := [DecipheredCipherSeedSize]byte{}
|
emptySeed := [DecipheredCipherSeedSize]byte{}
|
||||||
return emptySeed, ErrUnknownMnenomicWord{
|
return emptySeed, ErrUnknownMnemonicWord{
|
||||||
Word: word,
|
Word: word,
|
||||||
Index: uint8(i),
|
Index: uint8(i),
|
||||||
}
|
}
|
||||||
@@ -537,20 +546,20 @@ func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ChangePass takes an existing mnemonic, and passphrase for said mnemonic and
|
// ChangePass takes an existing mnemonic, and passphrase for said mnemonic and
|
||||||
// re-enciphers the plaintext cipher seed into a brand new mnemonic. This can
|
// re-enciphers the plaintext cipher seed into a brand-new mnemonic. This can
|
||||||
// be used to allow users to re-encrypt the same seed with multiple pass
|
// be used to allow users to re-encrypt the same seed with multiple pass
|
||||||
// phrases, or just change the passphrase on an existing seed.
|
// phrases, or just change the passphrase on an existing seed.
|
||||||
func (m *Mnemonic) ChangePass(oldPass, newPass []byte) (Mnemonic, error) {
|
func (m *Mnemonic) ChangePass(oldPass, newPass []byte) (Mnemonic, error) {
|
||||||
var newmnemonic Mnemonic
|
var newMnemonic Mnemonic
|
||||||
|
|
||||||
// First, we'll try to decrypt the current mnemonic using the existing
|
// First, we'll try to decrypt the current mnemonic using the existing
|
||||||
// passphrase. If this fails, then we can't proceed any further.
|
// passphrase. If this fails, then we can't proceed any further.
|
||||||
cipherSeed, err := m.ToCipherSeed(oldPass)
|
cipherSeed, err := m.ToCipherSeed(oldPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newmnemonic, err
|
return newMnemonic, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the deciperhing was successful, then we'll now re-encipher using
|
// If the deciphering was successful, then we'll now re-encipher using
|
||||||
// the new user provided passphrase.
|
// the new user provided passphrase.
|
||||||
return cipherSeed.ToMnemonic(newPass)
|
return cipherSeed.ToMnemonic(newPass)
|
||||||
}
|
}
|
||||||
|
@@ -84,9 +84,9 @@ func assertCipherSeedEqual(t *testing.T, cipherSeed *CipherSeed,
|
|||||||
func TestAezeedVersion0TestVectors(t *testing.T) {
|
func TestAezeedVersion0TestVectors(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// To minimize the number of tests that need to be run,
|
// To minimize the number of tests that need to be run, go through all
|
||||||
// go through all test vectors in the same test and also check
|
// test vectors in the same test and also check the birthday calculation
|
||||||
// the birthday calculation while we're at it.
|
// while we're at it.
|
||||||
for _, v := range version0TestVectors {
|
for _, v := range version0TestVectors {
|
||||||
// First, we create new cipher seed with the given values
|
// First, we create new cipher seed with the given values
|
||||||
// from the test vector.
|
// from the test vector.
|
||||||
@@ -95,12 +95,12 @@ func TestAezeedVersion0TestVectors(t *testing.T) {
|
|||||||
t.Fatalf("unable to create seed: %v", err)
|
t.Fatalf("unable to create seed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then we need to set the salt to the pre-defined value, otherwise
|
// Then we need to set the salt to the pre-defined value,
|
||||||
// we'll end up with randomness in our mnemonics.
|
// otherwise we'll end up with randomness in our mnemonics.
|
||||||
cipherSeed.salt = testSalt
|
cipherSeed.salt = testSalt
|
||||||
|
|
||||||
// Now that the seed has been created, we'll attempt to convert it to a
|
// Now that the seed has been created, we'll attempt to convert
|
||||||
// valid mnemonic.
|
// it to a valid mnemonic.
|
||||||
mnemonic, err := cipherSeed.ToMnemonic(v.password)
|
mnemonic, err := cipherSeed.ToMnemonic(v.password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create mnemonic: %v", err)
|
t.Fatalf("unable to create mnemonic: %v", err)
|
||||||
@@ -189,7 +189,7 @@ func TestManualEntropyGeneration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestInvalidPassphraseRejection tests if a caller attempts to use the
|
// TestInvalidPassphraseRejection tests if a caller attempts to use the
|
||||||
// incorrect passprhase for an enciphered seed, then the proper error is
|
// incorrect passphrase for an enciphered seed, then the proper error is
|
||||||
// returned.
|
// returned.
|
||||||
func TestInvalidPassphraseRejection(t *testing.T) {
|
func TestInvalidPassphraseRejection(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -228,7 +228,7 @@ func TestRawEncipherDecipher(t *testing.T) {
|
|||||||
t.Fatalf("unable to create seed: %v", err)
|
t.Fatalf("unable to create seed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the cipherseed obtained, we'll now use the raw encipher method
|
// With the cipher seed obtained, we'll now use the raw encipher method
|
||||||
// to obtain our final cipher text.
|
// to obtain our final cipher text.
|
||||||
cipherText, err := cipherSeed.Encipher(pass)
|
cipherText, err := cipherSeed.Encipher(pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -270,7 +270,7 @@ func TestInvalidExternalVersion(t *testing.T) {
|
|||||||
t.Fatalf("unable to create seed: %v", err)
|
t.Fatalf("unable to create seed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the cipherseed obtained, we'll now use the raw encipher method
|
// With the cipher seed obtained, we'll now use the raw encipher method
|
||||||
// to obtain our final cipher text.
|
// to obtain our final cipher text.
|
||||||
pass := []byte("newpasswhodis")
|
pass := []byte("newpasswhodis")
|
||||||
cipherText, err := cipherSeed.Encipher(pass)
|
cipherText, err := cipherSeed.Encipher(pass)
|
||||||
@@ -312,16 +312,16 @@ func TestChangePassphrase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now that have the mnemonic, we'll attempt to re-encipher the
|
// Now that have the mnemonic, we'll attempt to re-encipher the
|
||||||
// passphrase in order to get a brand new mnemonic.
|
// passphrase in order to get a brand-new mnemonic.
|
||||||
newPass := []byte("strongerpassyeh!")
|
newPass := []byte("strongerpassyeh!")
|
||||||
newmnemonic, err := mnemonic.ChangePass(pass, newPass)
|
newMnemonic, err := mnemonic.ChangePass(pass, newPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to change passphrase: %v", err)
|
t.Fatalf("unable to change passphrase: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll now attempt to decipher the new mnemonic using the new
|
// We'll now attempt to decipher the new mnemonic using the new
|
||||||
// passphrase to arrive at (what should be) the original cipher seed.
|
// passphrase to arrive at (what should be) the original cipher seed.
|
||||||
newCipherSeed, err := newmnemonic.ToCipherSeed(newPass)
|
newCipherSeed, err := newMnemonic.ToCipherSeed(newPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to decipher cipher seed: %v", err)
|
t.Fatalf("unable to decipher cipher seed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -332,7 +332,7 @@ func TestChangePassphrase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestChangePassphraseWrongPass tests that if we have a valid enciphered
|
// TestChangePassphraseWrongPass tests that if we have a valid enciphered
|
||||||
// cipherseed, but then try to change the password with the *wrong* password,
|
// cipher seed, but then try to change the password with the *wrong* password,
|
||||||
// then we get an error.
|
// then we get an error.
|
||||||
func TestChangePassphraseWrongPass(t *testing.T) {
|
func TestChangePassphraseWrongPass(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -352,7 +352,7 @@ func TestChangePassphraseWrongPass(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now that have the mnemonic, we'll attempt to re-encipher the
|
// Now that have the mnemonic, we'll attempt to re-encipher the
|
||||||
// passphrase in order to get a brand new mnemonic. However, we'll be
|
// passphrase in order to get a brand-new mnemonic. However, we'll be
|
||||||
// using the *wrong* passphrase. This should result in an
|
// using the *wrong* passphrase. This should result in an
|
||||||
// ErrInvalidPass error.
|
// ErrInvalidPass error.
|
||||||
wrongPass := []byte("kek")
|
wrongPass := []byte("kek")
|
||||||
@@ -397,7 +397,7 @@ func TestMnemonicEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestEncipherDecipher is a property-based test that ensures that given a
|
// TestEncipherDecipher is a property-based test that ensures that given a
|
||||||
// version, entropy, and birthday, then we're able to map that to a cipherseed
|
// version, entropy, and birthday, then we're able to map that to a cipher seed
|
||||||
// mnemonic, then back to the original plaintext cipher seed.
|
// mnemonic, then back to the original plaintext cipher seed.
|
||||||
func TestEncipherDecipher(t *testing.T) {
|
func TestEncipherDecipher(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -406,7 +406,7 @@ func TestEncipherDecipher(t *testing.T) {
|
|||||||
// ensure that given a random seed tuple (internal version, entropy,
|
// ensure that given a random seed tuple (internal version, entropy,
|
||||||
// and birthday) we're able to convert that to a valid cipher seed.
|
// and birthday) we're able to convert that to a valid cipher seed.
|
||||||
// Additionally, we should be able to decipher the final mnemonic, and
|
// Additionally, we should be able to decipher the final mnemonic, and
|
||||||
// recover the original cipherseed.
|
// recover the original cipher seed.
|
||||||
mainScenario := func(version uint8, entropy [EntropySize]byte,
|
mainScenario := func(version uint8, entropy [EntropySize]byte,
|
||||||
nowInt int64, pass [20]byte) bool {
|
nowInt int64, pass [20]byte) bool {
|
||||||
|
|
||||||
@@ -458,7 +458,7 @@ func TestEncipherDecipher(t *testing.T) {
|
|||||||
// arbitrary raw seed.
|
// arbitrary raw seed.
|
||||||
func TestSeedEncodeDecode(t *testing.T) {
|
func TestSeedEncodeDecode(t *testing.T) {
|
||||||
// mainScenario is the primary driver of our property-based test. We'll
|
// mainScenario is the primary driver of our property-based test. We'll
|
||||||
// ensure that given a random cipher seed, we can encode it an decode
|
// ensure that given a random cipher seed, we can encode it and decode
|
||||||
// it precisely.
|
// it precisely.
|
||||||
mainScenario := func(version uint8, nowInt int64,
|
mainScenario := func(version uint8, nowInt int64,
|
||||||
entropy [EntropySize]byte) bool {
|
entropy [EntropySize]byte) bool {
|
||||||
@@ -506,10 +506,10 @@ func TestSeedEncodeDecode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDecipherUnknownMnenomicWord tests that if we obtain a mnemonic, the
|
// TestDecipherUnknownMnemonicWord tests that if we obtain a mnemonic, then
|
||||||
// modify one of the words to not be within the word list, then it's detected
|
// modify one of the words to not be within the word list, then it's detected
|
||||||
// when we attempt to map it back to the original cipher seed.
|
// when we attempt to map it back to the original cipher seed.
|
||||||
func TestDecipherUnknownMnenomicWord(t *testing.T) {
|
func TestDecipherUnknownMnemonicWord(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// First, we'll create a new cipher seed with "test" ass a password.
|
// First, we'll create a new cipher seed with "test" ass a password.
|
||||||
@@ -532,15 +532,15 @@ func TestDecipherUnknownMnenomicWord(t *testing.T) {
|
|||||||
mnemonic[randIndex] = "kek"
|
mnemonic[randIndex] = "kek"
|
||||||
|
|
||||||
// If we attempt to map back to the original cipher seed now, then we
|
// If we attempt to map back to the original cipher seed now, then we
|
||||||
// should get ErrUnknownMnenomicWord.
|
// should get ErrUnknownMnemonicWord.
|
||||||
_, err = mnemonic.ToCipherSeed(pass)
|
_, err = mnemonic.ToCipherSeed(pass)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected ErrUnknownMnenomicWord error")
|
t.Fatalf("expected ErrUnknownMnemonicWord error")
|
||||||
}
|
}
|
||||||
|
|
||||||
wordErr, ok := err.(ErrUnknownMnenomicWord)
|
wordErr, ok := err.(ErrUnknownMnemonicWord)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("expected ErrUnknownMnenomicWord instead got %T", err)
|
t.Fatalf("expected ErrUnknownMnemonicWord instead got %T", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wordErr.Word != "kek" {
|
if wordErr.Word != "kek" {
|
||||||
@@ -551,24 +551,24 @@ func TestDecipherUnknownMnenomicWord(t *testing.T) {
|
|||||||
randIndex, wordErr.Index)
|
randIndex, wordErr.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the mnemonic includes a word that is not in the englishList
|
// If the mnemonic includes a word that is not in the englishList it
|
||||||
// it fails, even when it is a substring of a valid word
|
// fails, even when it is a substring of a valid word Example: `heart`
|
||||||
// Example: `heart` is in the list, `hear` is not
|
// is in the list, `hear` is not.
|
||||||
mnemonic[randIndex] = "hear"
|
mnemonic[randIndex] = "hear"
|
||||||
|
|
||||||
// If we attempt to map back to the original cipher seed now, then we
|
// If we attempt to map back to the original cipher seed now, then we
|
||||||
// should get ErrUnknownMnenomicWord.
|
// should get ErrUnknownMnemonicWord.
|
||||||
_, err = mnemonic.ToCipherSeed(pass)
|
_, err = mnemonic.ToCipherSeed(pass)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected ErrUnknownMnenomicWord error")
|
t.Fatalf("expected ErrUnknownMnemonicWord error")
|
||||||
}
|
}
|
||||||
_, ok = err.(ErrUnknownMnenomicWord)
|
_, ok = err.(ErrUnknownMnemonicWord)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("expected ErrUnknownMnenomicWord instead got %T", err)
|
t.Fatalf("expected ErrUnknownMnemonicWord instead got %T", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDecipherIncorrectMnemonic tests that if we obtain a cipherseed, but then
|
// TestDecipherIncorrectMnemonic tests that if we obtain a cipher seed, but then
|
||||||
// swap out words, then checksum fails.
|
// swap out words, then checksum fails.
|
||||||
func TestDecipherIncorrectMnemonic(t *testing.T) {
|
func TestDecipherIncorrectMnemonic(t *testing.T) {
|
||||||
// First, we'll create a new cipher seed with "test" ass a password.
|
// First, we'll create a new cipher seed with "test" ass a password.
|
||||||
@@ -593,7 +593,7 @@ func TestDecipherIncorrectMnemonic(t *testing.T) {
|
|||||||
|
|
||||||
// If we attempt to decrypt now, we should get a checksum failure.
|
// If we attempt to decrypt now, we should get a checksum failure.
|
||||||
// If we attempt to map back to the original cipher seed now, then we
|
// If we attempt to map back to the original cipher seed now, then we
|
||||||
// should get ErrUnknownMnenomicWord.
|
// should get ErrIncorrectMnemonic.
|
||||||
_, err = mnemonic.ToCipherSeed(pass)
|
_, err = mnemonic.ToCipherSeed(pass)
|
||||||
if err != ErrIncorrectMnemonic {
|
if err != ErrIncorrectMnemonic {
|
||||||
t.Fatalf("expected ErrIncorrectMnemonic error")
|
t.Fatalf("expected ErrIncorrectMnemonic error")
|
||||||
|
@@ -18,9 +18,9 @@ var (
|
|||||||
"match")
|
"match")
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrUnknownMnenomicWord is returned when attempting to decipher and
|
// ErrUnknownMnemonicWord is returned when attempting to decipher and
|
||||||
// enciphered mnemonic, but a word encountered isn't a member of our word list.
|
// enciphered mnemonic, but a word encountered isn't a member of our word list.
|
||||||
type ErrUnknownMnenomicWord struct {
|
type ErrUnknownMnemonicWord struct {
|
||||||
// Word is the unknown word in the mnemonic phrase.
|
// Word is the unknown word in the mnemonic phrase.
|
||||||
Word string
|
Word string
|
||||||
|
|
||||||
@@ -29,8 +29,8 @@ type ErrUnknownMnenomicWord struct {
|
|||||||
Index uint8
|
Index uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns a human readable string describing the error.
|
// Error returns a human-readable string describing the error.
|
||||||
func (e ErrUnknownMnenomicWord) Error() string {
|
func (e ErrUnknownMnemonicWord) Error() string {
|
||||||
return fmt.Sprintf("word %v isn't a part of default word list "+
|
return fmt.Sprintf("word %v isn't a part of default word list "+
|
||||||
"(index=%v)", e.Word, e.Index)
|
"(index=%v)", e.Word, e.Index)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user