muun-recovery/keys_generator.go

65 lines
1.9 KiB
Go
Raw Permalink Normal View History

2019-10-01 11:22:30 -04:00
package main
import (
2021-03-17 14:28:04 -04:00
"fmt"
2019-10-01 11:22:30 -04:00
"github.com/muun/libwallet"
2021-03-17 14:28:04 -04:00
"github.com/muun/libwallet/emergencykit"
2019-10-01 11:22:30 -04:00
)
2021-01-29 16:51:08 -05:00
var defaultNetwork = libwallet.Mainnet()
2021-03-17 14:28:04 -04:00
func decodeKeysFromInput(rawKey1 string, rawKey2 string) ([]*libwallet.EncryptedPrivateKeyInfo, error) {
key1, err := libwallet.DecodeEncryptedPrivateKey(rawKey1)
2020-11-09 08:05:29 -05:00
if err != nil {
2021-03-17 14:28:04 -04:00
return nil, fmt.Errorf("failed to decode first key: %w", err)
2020-11-09 08:05:29 -05:00
}
2019-10-01 11:22:30 -04:00
2021-03-17 14:28:04 -04:00
key2, err := libwallet.DecodeEncryptedPrivateKey(rawKey2)
2021-01-29 16:51:08 -05:00
if err != nil {
2021-03-17 14:28:04 -04:00
return nil, fmt.Errorf("failed to decode second key: %w", err)
2021-01-29 16:51:08 -05:00
}
2021-03-17 14:28:04 -04:00
return []*libwallet.EncryptedPrivateKeyInfo{key1, key2}, nil
}
func decodeKeysFromMetadata(meta *emergencykit.Metadata) ([]*libwallet.EncryptedPrivateKeyInfo, error) {
decodedKeys := make([]*libwallet.EncryptedPrivateKeyInfo, len(meta.EncryptedKeys))
for i, metaKey := range meta.EncryptedKeys {
decodedKeys[i] = &libwallet.EncryptedPrivateKeyInfo{
Version: meta.Version,
Birthday: meta.BirthdayBlock,
EphPublicKey: metaKey.DhPubKey,
CipherText: metaKey.EncryptedPrivKey,
Salt: metaKey.Salt,
}
2019-10-01 11:22:30 -04:00
}
2021-03-17 14:28:04 -04:00
return decodedKeys, nil
2019-10-01 11:22:30 -04:00
}
2021-03-17 14:28:04 -04:00
func decryptKeys(encryptedKeys []*libwallet.EncryptedPrivateKeyInfo, recoveryCode string) ([]*libwallet.DecryptedPrivateKey, error) {
// Always take the salt from the second key (the same salt was used for all keys, but our legacy
// key format did not include it in the first key):
salt := encryptedKeys[1].Salt
decryptionKey, err := libwallet.RecoveryCodeToKey(recoveryCode, salt)
if err != nil {
return nil, fmt.Errorf("failed to process recovery code: %w", err)
}
decryptedKeys := make([]*libwallet.DecryptedPrivateKey, len(encryptedKeys))
for i, encryptedKey := range encryptedKeys {
decryptedKey, err := decryptionKey.DecryptKey(encryptedKey, defaultNetwork)
if err != nil {
return nil, fmt.Errorf("failed to decrypt key %d: %w", i, err)
}
decryptedKeys[i] = decryptedKey
}
2019-10-01 11:22:30 -04:00
2021-03-17 14:28:04 -04:00
return decryptedKeys, nil
2019-10-01 11:22:30 -04:00
}