Bip39.swift
Swift 的跨平台 BIP39 密语实现。
安装
Bip39.swift 可以部署到 macOS、iOS、tvOS、watchOS 和 Linux。它只在最新的操作系统版本上进行了测试,但是,由于该模块使用的平台提供的 API 非常少,因此与早期版本应该只有很少的问题。
设置说明
-
Swift 包管理器:将以下内容添加到您的
Package.swift
清单的依赖部分.package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.2.0")
-
CocoaPods:将以下内容放入您的
Podfile
pod 'Bip39.swift', '~> 0.2'
使用示例
随机生成的助记词
import Bip39
// 128bit mnemonic by default
let mnemonic = try! Mnemonic()
// Obtaining phrase. English by default. Returns string array
let phrase = mnemonic.mnemonic().joined(separator: " ")
print("Mnemonic: ", phrase)
从Bip39字符串生成助记词
import Bip39
// Mnemonic phrase
let phrase = "rally speed budget undo purpose orchard hero news crunch flush wine finger"
// Creating mnemonic. English wordlist by default
let mnemonic = try! Mnemonic(mnemonic: phrase.components(separatedBy: " "))
// 128 bit entropy
print("Entropy: ", mnemonic.entropy)
从助记词生成种子
import Bip39
// Mnemonic phrase
let phrase = "rally speed budget undo purpose orchard hero news crunch flush wine finger"
// Creating mnemonic. English wordlist by default
let mnemonic = try! Mnemonic(mnemonic: phrase.components(separatedBy: " "))
// Empty password and English wordlist by default
let seed = mnemonic.seed()
print("Seed: ", seed)
检查Bip39字符串是否有效
import Bip39
// Mnemonic phrase
let phrase = "rally speed budget undo purpose orchard hero news crunch flush wine finger"
// Validation. English wordlist by default
let isValid = Mnemonic.isValid(phrase: phrase.components(separatedBy: " "))
print("Valid: ", isValid)
许可证
Bip39.swift 可在 Apache 2.0 许可证下使用、分发和修改。