Skip to content

Commit

Permalink
fix: specify network for restoreFromRemoteServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonvdb committed Oct 11, 2023
1 parent fbf5c27 commit ff95592
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
4 changes: 3 additions & 1 deletion example/Dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import {
import Clipboard from '@react-native-clipboard/clipboard';
import {
backupAccount,
getAddressBalance,
importAccount,
setupLdk,
syncLdk,
getAddressBalance,
updateHeader,
} from './ldk';
import { connectToElectrum, subscribeToHeader } from './electrum';
import ldk from '@synonymdev/react-native-ldk/dist/ldk';
import lm, {
EEventTypes,
ENetworks,
TChannelManagerClaim,
TChannelManagerPaymentPathFailed,
TChannelManagerPaymentPathSuccessful,
Expand Down Expand Up @@ -646,6 +647,7 @@ const Dev = (): ReactElement => {
const restoreRes = await lm.restoreFromRemoteServer({
account,
serverDetails: backupServerDetails,
network: ENetworks.regtest,
overwrite: true,
});

Expand Down
17 changes: 7 additions & 10 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1144,25 +1144,22 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
@ReactMethod
fun backupSetup(seed: String, network: String, server: String, serverPubKey: String, promise: Promise) {
val seedBytes = seed.hexa()
if (keysManager == null) {
val nanoSeconds = System.currentTimeMillis() * 1000
val seconds = nanoSeconds / 1000 / 1000
if (seedBytes.count() != 32) {
return handleReject(promise, LdkErrors.invalid_seed_hex)
}

keysManager = KeysManager.of(seedBytes, seconds, nanoSeconds.toInt())
if (seedBytes.count() != 32) {
return handleReject(promise, LdkErrors.invalid_seed_hex)
}

val pubKeyRes = keysManager!!.as_NodeSigner().get_node_id(Recipient.LDKRecipient_Node)
val nanoSeconds = System.currentTimeMillis() * 1000
val seconds = nanoSeconds / 1000 / 1000
val keysManager = KeysManager.of(seedBytes, seconds, nanoSeconds.toInt())
val pubKeyRes = keysManager.as_NodeSigner().get_node_id(Recipient.LDKRecipient_Node)
if (!pubKeyRes.is_ok) {
return handleReject(promise, LdkErrors.backup_setup_failed)
}

try {
BackupClient.skipRemoteBackup = false
BackupClient.setup(
keysManager!!._node_secret_key,
keysManager._node_secret_key,
(pubKeyRes as Result_PublicKeyNoneZ_OK).res,
network,
server,
Expand Down
24 changes: 13 additions & 11 deletions lib/ios/Ldk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1230,25 +1230,27 @@ class Ldk: NSObject {
@objc
func backupSetup(_ seed: NSString, network: NSString, server: NSString, serverPubKey: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let seedBytes = String(seed).hexaBytes
if keysManager == nil {
let seconds = UInt64(NSDate().timeIntervalSince1970)
let nanoSeconds = UInt32.init(truncating: NSNumber(value: seconds * 1000 * 1000))

guard seedBytes.count == 32 else {
return handleReject(reject, .invalid_seed_hex)
}

keysManager = KeysManager(seed: String(seed).hexaBytes, startingTimeSecs: seconds, startingTimeNanos: nanoSeconds)

guard seedBytes.count == 32 else {
return handleReject(reject, .invalid_seed_hex)
}

let seconds = UInt64(NSDate().timeIntervalSince1970)
let nanoSeconds = UInt32.init(truncating: NSNumber(value: seconds * 1000 * 1000))
let keysManager = KeysManager(
seed: String(seed).hexaBytes,
startingTimeSecs: seconds,
startingTimeNanos: nanoSeconds
)

guard let pubKey = keysManager!.asNodeSigner().getNodeId(recipient: .Node).getValue() else {
guard let pubKey = keysManager.asNodeSigner().getNodeId(recipient: .Node).getValue() else {
return handleReject(reject, .backup_setup_failed, "Failed to get nodeID from keysManager")
}

do {
BackupClient.skipRemoteBackup = false
try BackupClient.setup(
secretKey: keysManager!.getNodeSecretKey(),
secretKey: keysManager.getNodeSecretKey(),
pubKey: pubKey,
network: String(network),
server: String(server),
Expand Down
4 changes: 3 additions & 1 deletion lib/src/lightning-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,12 @@ class LightningManager {
restoreFromRemoteServer = async ({
account,
serverDetails,
network,
overwrite = false,
}: {
account: TAccount;
serverDetails: TBackupServerDetails;
network: ENetworks;
overwrite?: boolean;
}): Promise<Result<string>> => {
//Make sure LDK is not running.
Expand All @@ -924,7 +926,7 @@ class LightningManager {

const backupSetupRes = await ldk.backupSetup({
seed: account.seed,
network: this.network,
network,
details: serverDetails,
});
if (backupSetupRes.isErr()) {
Expand Down

0 comments on commit ff95592

Please sign in to comment.