Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions Bitkit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
4AFCA3702E05933800205CAE /* Zip in Frameworks */ = {isa = PBXBuildFile; productRef = 4AFCA36F2E05933800205CAE /* Zip */; };
4AFCA3722E0596D900205CAE /* Zip in Frameworks */ = {isa = PBXBuildFile; productRef = 4AFCA3712E0596D900205CAE /* Zip */; };
961058E32C355B5500E1F1D8 /* BitkitNotification.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 961058DC2C355B5500E1F1D8 /* BitkitNotification.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
96204B762DE9A91A007BAA26 /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = 96204B752DE9A91A007BAA26 /* SQLite */; };
96204B782DE9AA43007BAA26 /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = 96204B772DE9AA43007BAA26 /* SQLite */; };
966DE6702C51210000A7B0EF /* LightningDevKit in Frameworks */ = {isa = PBXBuildFile; productRef = 966DE66F2C51210000A7B0EF /* LightningDevKit */; };
968FDF162DFAFE230053CD7F /* LDKNode in Frameworks */ = {isa = PBXBuildFile; productRef = 9613018B2C5022D700878183 /* LDKNode */; };
968FE1402DFB016B0053CD7F /* LDKNode in Frameworks */ = {isa = PBXBuildFile; productRef = 968FE13F2DFB016B0053CD7F /* LDKNode */; };
96DEA03A2DE8BBA1009932BF /* BitkitCore in Frameworks */ = {isa = PBXBuildFile; productRef = 96DEA0392DE8BBA1009932BF /* BitkitCore */; };
Expand Down Expand Up @@ -164,10 +161,8 @@
files = (
4AFCA3702E05933800205CAE /* Zip in Frameworks */,
968FDF162DFAFE230053CD7F /* LDKNode in Frameworks */,
96204B782DE9AA43007BAA26 /* SQLite in Frameworks */,
18D65E002EB964B500252335 /* VssRustClientFfi in Frameworks */,
96E493A42C942FD1000E8BC2 /* secp256k1 in Frameworks */,
966DE6702C51210000A7B0EF /* LightningDevKit in Frameworks */,
96DEA03A2DE8BBA1009932BF /* BitkitCore in Frameworks */,
96E20CD42CB6D91A00C24149 /* CodeScanner in Frameworks */,
4AAB08CA2E1FE77600BA63DF /* Lottie in Frameworks */,
Expand All @@ -178,7 +173,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
96204B762DE9A91A007BAA26 /* SQLite in Frameworks */,
96E493A62C94317D000E8BC2 /* secp256k1 in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -270,11 +264,9 @@
name = Bitkit;
packageProductDependencies = (
9613018B2C5022D700878183 /* LDKNode */,
966DE66F2C51210000A7B0EF /* LightningDevKit */,
96E493A32C942FD1000E8BC2 /* secp256k1 */,
96E20CD32CB6D91A00C24149 /* CodeScanner */,
96DEA0392DE8BBA1009932BF /* BitkitCore */,
96204B772DE9AA43007BAA26 /* SQLite */,
4AFCA36F2E05933800205CAE /* Zip */,
4AAB08C92E1FE77600BA63DF /* Lottie */,
18D65DFF2EB964B500252335 /* VssRustClientFfi */,
Expand Down Expand Up @@ -302,7 +294,6 @@
name = BitkitTests;
packageProductDependencies = (
96E493A52C94317D000E8BC2 /* secp256k1 */,
96204B752DE9A91A007BAA26 /* SQLite */,
);
productName = BitkitTests;
productReference = 96FE1F722C2DE6AC006D0C8B /* BitkitTests.xctest */;
Expand Down Expand Up @@ -379,8 +370,6 @@
);
mainGroup = 96FE1F582C2DE6AA006D0C8B;
packageReferences = (
961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */,
966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */,
96E493A22C942FD1000E8BC2 /* XCRemoteSwiftPackageReference "swift-secp256k1" */,
96E20CD22CB6D91A00C24149 /* XCRemoteSwiftPackageReference "CodeScanner" */,
96DEA0382DE8BBA1009932BF /* XCRemoteSwiftPackageReference "bitkit-core" */,
Expand Down Expand Up @@ -902,14 +891,6 @@
minimumVersion = 2.1.2;
};
};
961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/stephencelis/SQLite.swift.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.15.3;
};
};
962045C92DE998F1007BAA26 /* XCRemoteSwiftPackageReference "ldk-node" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/lightningdevkit/ldk-node";
Expand All @@ -918,14 +899,6 @@
kind = branch;
};
};
966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/lightningdevkit/ldk-swift/";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.0.123;
};
};
968FE13E2DFB016B0053CD7F /* XCRemoteSwiftPackageReference "ldk-node" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/synonymdev/ldk-node";
Expand Down Expand Up @@ -991,21 +964,6 @@
package = 962045C92DE998F1007BAA26 /* XCRemoteSwiftPackageReference "ldk-node" */;
productName = LDKNode;
};
96204B752DE9A91A007BAA26 /* SQLite */ = {
isa = XCSwiftPackageProductDependency;
package = 961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */;
productName = SQLite;
};
96204B772DE9AA43007BAA26 /* SQLite */ = {
isa = XCSwiftPackageProductDependency;
package = 961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */;
productName = SQLite;
};
966DE66F2C51210000A7B0EF /* LightningDevKit */ = {
isa = XCSwiftPackageProductDependency;
package = 966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */;
productName = LightningDevKit;
};
968FE13F2DFB016B0053CD7F /* LDKNode */ = {
isa = XCSwiftPackageProductDependency;
package = 968FE13E2DFB016B0053CD7F /* XCRemoteSwiftPackageReference "ldk-node" */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 4 additions & 65 deletions Bitkit/Services/RNBackupClient.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CommonCrypto
import CryptoKit
import Foundation
import LightningDevKit
import LDKNode

enum RNBackupError: Error, LocalizedError {
case notSetup
Expand Down Expand Up @@ -63,9 +62,9 @@ class RNBackupClient {
private init() {}

func setup(mnemonic: String, passphrase: String?) async throws {
let seed = try deriveSeed(mnemonic: mnemonic, passphrase: passphrase)
secretKey = seed
publicKey = try Crypto.getPublicKey(privateKey: seed)
let secretKeyBytes = try deriveNodeSecretFromMnemonic(mnemonic: mnemonic, passphrase: passphrase)
secretKey = Data(secretKeyBytes)
publicKey = try Crypto.getPublicKey(privateKey: secretKey!)
serverHost = Env.rnBackupServerHost
network = networkString()
cachedBearer = nil
Expand All @@ -87,66 +86,6 @@ class RNBackupClient {
}
}

private func deriveSeed(mnemonic: String, passphrase: String?) throws -> Data {
let mnemonicData = Data(mnemonic.utf8)
let salt = "mnemonic" + (passphrase ?? "")
let saltData = Data(salt.utf8)

var bip39Seed = [UInt8](repeating: 0, count: 64)
let pbkdfResult = bip39Seed.withUnsafeMutableBytes { seedPtr in
mnemonicData.withUnsafeBytes { mnemonicPtr in
saltData.withUnsafeBytes { saltPtr in
CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
mnemonicPtr.bindMemory(to: Int8.self).baseAddress!,
mnemonicData.count,
saltPtr.bindMemory(to: UInt8.self).baseAddress!,
saltData.count,
CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA512),
2048,
seedPtr.bindMemory(to: UInt8.self).baseAddress!,
64
)
}
}
}

guard pbkdfResult == kCCSuccess else {
throw RNBackupError.authFailed
}

let hmacKey = Data("Bitcoin seed".utf8)
let seedData = Data(bip39Seed)

var hmacOutput = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
hmacKey.withUnsafeBytes { keyPtr in
seedData.withUnsafeBytes { seedPtr in
CCHmac(
CCHmacAlgorithm(kCCHmacAlgSHA512),
keyPtr.baseAddress!,
hmacKey.count,
seedPtr.baseAddress!,
seedData.count,
&hmacOutput
)
}
}

let bip32Seed = [UInt8](hmacOutput.prefix(32))

let currentTime = Date()
let seconds = UInt64(currentTime.timeIntervalSince1970)
let nanoSeconds = UInt32((currentTime.timeIntervalSince1970.truncatingRemainder(dividingBy: 1)) * 1_000_000_000)

let keysManager = KeysManager(
seed: bip32Seed,
startingTimeSecs: seconds,
startingTimeNanos: nanoSeconds
)

return Data(keysManager.getNodeSecretKey())
}

// MARK: - Public API

func listFiles(fileGroup: String? = "ldk") async throws -> RNBackupListResponse {
Expand Down
11 changes: 3 additions & 8 deletions Bitkit/Utilities/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
case nodeNotSetup
case nodeNotStarted
case onchainWalletNotInitialized
case ldkNodeSqliteAlreadyExists
case ldkToLdkNodeMigration
case mnemonicNotFound
case nodeStillRunning
case onchainWalletStillRunning
Expand Down Expand Up @@ -79,12 +77,6 @@
case .onchainWalletNotInitialized:
message = "Onchain wallet not created"
debugMessage = nil
case .ldkNodeSqliteAlreadyExists:
message = "LDK-node SQLite file already exists"
debugMessage = nil
case .ldkToLdkNodeMigration:
message = "LDK to LDK-node migration issue"
debugMessage = nil
case .mnemonicNotFound:
message = "Mnemonic not found"
debugMessage = nil
Expand Down Expand Up @@ -338,7 +330,7 @@
case let .InvalidNodeAlias(message: ldkMessage):
message = "Invalid node alias"
debugMessage = ldkMessage
case let .InvalidCustomTlvs(message: ldkMessage):

Check warning on line 333 in Bitkit/Utilities/Errors.swift

View workflow job for this annotation

GitHub Actions / Run Tests

case is already handled by previous patterns; consider removing it
message = "Invalid custom TLVs"
debugMessage = ldkMessage
case let .InvalidDateTime(message: ldkMessage):
Expand All @@ -362,6 +354,9 @@
case let .CoinSelectionFailed(ldkMessage):
message = "Coin selection failed"
debugMessage = ldkMessage
case let .InvalidMnemonic(ldkMessage):
message = "Invalid mnemonic"
debugMessage = ldkMessage
}
Logger.error("\(message) [\(debugMessage ?? "")]", context: "ldk-node error")
}
Expand Down
Loading