Skip to content
Draft
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
1 change: 1 addition & 0 deletions FlipcashCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let package = Package(
name: "FlipcashCore",
platforms: [
.iOS(.v18),
.macOS(.v15),
],
products: [
.library(
Expand Down
82 changes: 8 additions & 74 deletions FlipcashCore/Sources/FlipcashCore/Solana/Keys/Derive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,19 @@
//

import Foundation
import SharedCoreKit

public typealias KeyDescriptor = (key: Data, chain: Data)

/// Deterministic wallet generation for ED25519 curve using SLIP-0010 spec
/// Reference: https://github.com/satoshilabs/slips/blob/master/slip-0010.md
///
public enum Derive {

private static let curve: Data = Data("ed25519 seed".utf8)
private static let hardenedOffset: UInt32 = 0x80000000

static func masterKey(seed: Data) -> KeyDescriptor {
seed.hmac(using: curve).split32()
}

static func path(path: Path, seed: Data) -> (keyPair: KeyPair, chaincode: Data) {
var descriptor = masterKey(seed: seed)

path.indexes.forEach { index in
descriptor = CKDPriv(keyDescriptor: descriptor, index: hardenedOffset + index.value)
}

return (
KeyPair(seed: try! Seed32(descriptor.key)),
descriptor.chain
)
}

private static func CKDPriv(keyDescriptor: KeyDescriptor, index: UInt32) -> KeyDescriptor {
var entropy = Data()
entropy.append(0x00)
entropy.append(keyDescriptor.key)
entropy.append(contentsOf: index.bigEndian.bytes)

return entropy.hmac(using: keyDescriptor.chain).split32()
}
}
private static let hardenedOffset: Int64 = 0x8000_0000

/// Deterministic key derivation using BIP39
/// Reference: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
///
extension Derive {

static func seedUsingBIP39(phrase: [String], password: String = "") -> Key64 {
let phrase = phrase.joined(separator: " ")
let salt = "mnemonic\(password)"

let bytes = PBKDF.deriveKey(
algorithm: .sha512,
password: phrase,
salt: salt
)

return try! Key64(bytes)
try! Key64(SharedDerivation.seed(mnemonic: phrase, passphrase: password))
}

public static func keyPairUsingBIP39(path: Path, phrase: [String], password: String = "") -> KeyPair {
let key64 = seedUsingBIP39(
phrase: phrase,
password: password
)

return Derive.path(path: path, seed: key64.data).keyPair
}
}

// MARK: - Data -

private extension Data {

func split32() -> (Data, Data) {
let lhs = Data(self[0..<32])
let rhs = Data(self[32..<64])
return (lhs, rhs)
}

func hmac(using key: Data) -> Data {
var hmac = HMAC(algorithm: .sha512, key: key)
hmac.update(self)
return hmac.digestData()
public static func keyPairUsingBIP39(path: Path, phrase: [String], password: String = "") -> KeyPair {
let seed = seedUsingBIP39(phrase: phrase, password: password)
let hardenedIndexes = path.indexes.map { hardenedOffset + Int64($0.value) }
let derivedKey = SharedDerivation.derivedKey(seed: seed.data, hardenedIndexes: hardenedIndexes)
return KeyPair(seed: try! Seed32(derivedKey))
}
}
2 changes: 1 addition & 1 deletion FlipcashCoreVectors/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import PackageDescription
let package = Package(
name: "FlipcashCoreVectors",
platforms: [
.macOS("14.0"), // FlipcashCore's transitive deps (BigDecimal 13.3, grpc/nio) need >= 13.3
.macOS("15.0"), // FlipcashCore's transitive deps (BigDecimal 13.3, grpc/nio) need >= 13.3
.iOS("18.0"),
],
dependencies: [
Expand Down
Loading