-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from soramitsu/feature/seed-factory
Seed factory
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import xxHash_Swift | ||
import IrohaCrypto | ||
|
||
public extension Data { | ||
func xxh128() -> Data { | ||
var hash1Value = XXH64.digest(self, seed: 0) | ||
let hash1 = Data(bytes: &hash1Value, count: MemoryLayout<UInt64>.size) | ||
|
||
var hash2Value = XXH64.digest(self, seed: 1) | ||
let hash2 = Data(bytes: &hash2Value, count: MemoryLayout<UInt64>.size) | ||
|
||
return hash1 + hash2 | ||
} | ||
|
||
func blake128Concat() throws -> Data { | ||
let hashed = try (self as NSData).blake2b(16) | ||
return hashed + self | ||
} | ||
|
||
func twox64Concat() -> Data { | ||
var hash1Value = XXH64.digest(self, seed: 0) | ||
return Data(bytes: &hash1Value, count: MemoryLayout<UInt64>.size) + self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
import IrohaCrypto | ||
|
||
public typealias SeedFactoryResult = (seed: Data, mnemonic: IRMnemonicProtocol) | ||
|
||
public protocol SeedFactoryProtocol { | ||
func createSeed(from password: String, strength: IRMnemonicStrength) throws -> SeedFactoryResult | ||
func deriveSeed(from mnemonicWords: String, password: String) throws -> SeedFactoryResult | ||
} | ||
|
||
public struct SeedFactory: SeedFactoryProtocol { | ||
private static let seedLength: Int = 32 | ||
|
||
private let seedFactory: SNBIP39SeedCreatorProtocol = SNBIP39SeedCreator() | ||
private let mnemonicCreator: IRMnemonicCreatorProtocol | ||
|
||
public init(mnemonicLanguage: IRMnemonicLanguage = .english) { | ||
mnemonicCreator = IRMnemonicCreator(language: mnemonicLanguage) | ||
} | ||
|
||
public func createSeed(from password: String, | ||
strength: IRMnemonicStrength) throws -> SeedFactoryResult { | ||
let mnemonic = try mnemonicCreator.randomMnemonic(strength) | ||
let seed = try seedFactory.deriveSeed(from: mnemonic.entropy(), passphrase: password) | ||
|
||
return SeedFactoryResult(seed: seed.prefix(Self.seedLength), mnemonic: mnemonic) | ||
} | ||
|
||
public func deriveSeed(from mnemonicWords: String, | ||
password: String) throws -> SeedFactoryResult { | ||
let mnemonic = try mnemonicCreator.mnemonic(fromList: mnemonicWords) | ||
let seed = try seedFactory.deriveSeed(from: mnemonic.entropy(), passphrase: password) | ||
|
||
return SeedFactoryResult(seed: seed.prefix(Self.seedLength), mnemonic: mnemonic) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import XCTest | ||
import FearlessUtils | ||
import IrohaCrypto | ||
|
||
class SeedFactoryTests: XCTestCase { | ||
func testGeneratedSeedMatchesDerived() throws { | ||
let passwords: [String] = ["", "password"] | ||
let strengths: [IRMnemonicStrength] = [ | ||
.entropy128, | ||
.entropy160, | ||
.entropy192, | ||
.entropy224, | ||
.entropy256, | ||
.entropy288, | ||
.entropy320 | ||
] | ||
|
||
let seedFactory = SeedFactory() | ||
|
||
for password in passwords { | ||
for strength in strengths { | ||
let expectedResult = try seedFactory.createSeed(from: password, strength: strength) | ||
let derivedResult = try seedFactory.deriveSeed(from: expectedResult.mnemonic.toString(), | ||
password: password) | ||
|
||
XCTAssertEqual(expectedResult.seed, derivedResult.seed) | ||
} | ||
} | ||
} | ||
} |