2019-10-01 11:22:30 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-29 16:51:08 -05:00
|
|
|
"encoding/hex"
|
2019-10-01 11:22:30 -04:00
|
|
|
log "log"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
"github.com/muun/libwallet"
|
|
|
|
)
|
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
var defaultNetwork = libwallet.Mainnet()
|
|
|
|
|
|
|
|
func buildExtendedKeys(rawKey1, rawKey2, recoveryCode string) (
|
|
|
|
*libwallet.DecryptedPrivateKey,
|
|
|
|
*libwallet.DecryptedPrivateKey) {
|
|
|
|
|
|
|
|
// Always take the salt from the second key (the same salt was used, but our older key format
|
|
|
|
// is missing the salt on the first key):
|
|
|
|
salt := extractSalt(rawKey2)
|
2019-10-01 11:22:30 -04:00
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
decryptionKey, err := libwallet.RecoveryCodeToKey(recoveryCode, salt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to process recovery code: %v", err)
|
|
|
|
}
|
2019-10-01 11:22:30 -04:00
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
key1, err := decryptionKey.DecryptKey(rawKey1, defaultNetwork)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to decrypt first key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key2, err := decryptionKey.DecryptKey(rawKey2, defaultNetwork)
|
2019-10-01 11:22:30 -04:00
|
|
|
if err != nil {
|
2021-01-29 16:51:08 -05:00
|
|
|
log.Fatalf("failed to decrypt second key: %v", err)
|
2019-10-01 11:22:30 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
return key1, key2
|
2019-10-01 11:22:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
func extractSalt(rawKey string) string {
|
2019-10-01 11:22:30 -04:00
|
|
|
bytes := base58.Decode(rawKey)
|
2020-11-09 08:05:29 -05:00
|
|
|
saltBytes := bytes[len(bytes)-8:]
|
2019-10-01 11:22:30 -04:00
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
return hex.EncodeToString(saltBytes)
|
2019-10-01 11:22:30 -04:00
|
|
|
}
|