一、关键枚举
》路径协议类型
public enum Purpose: UInt32, CaseIterable {
case bip44 = 44 //
case bip49 = 49 //隔离见证
case bip84 = 84 //bc1开头的隔离见证方式
case bip1852 = 1852
}
》Coin类型
public enum CoinType: UInt32, CaseIterable {
case aeternity = 457
case aion = 425
case binance = 714
case bitcoin = 0
case bitcoinCash = 145
case bitcoinGold = 156
……
}
》签名类型
public enum Curve: UInt32, CaseIterable, CustomStringConvertible {
case secp256k1 = 0
case ed25519 = 1
case ed25519Blake2bNano = 2
case curve25519 = 3
case nist256p1 = 4
case ed25519Extended = 5
}
》公钥类型:
public enum PublicKeyType: UInt32, CaseIterable {
case secp256k1 = 0
case secp256k1Extended = 1
case nist256p1 = 2
case nist256p1Extended = 3
case ed25519 = 4
case ed25519Blake2b = 5
case curve25519 = 6
case ed25519Extended = 7
}
二、DerivationPath类:
》初始化方式:
1、对象初始化方式:
public init(purpose: Purpose, coin: UInt32, account: UInt32 = 0, change: UInt32 = 0, address: UInt32 = 0)
例如:
DerivationPath(purpose: .bip44, coin: bitcoin.slip44Id, account: 0, change: 0, address: 1).description
2、通过字符串(比如:m/10/0/2'/3
)初始化:
public init?(_ string: String)
三、HDWallet类(Generated)
》核心属性
seed:String
mnemonic:String
》初始化:
通过助记词和密码 (常用):
public init(mnemonic: String, passphrase: String)
//以下两个可以不用
public init(strength: Int32, passphrase: String)
//通过data流初始化
public init(data: Data, passphrase: String)
》主要类方法:
// 1、判断是否是合法的助记词:
public static func isValid(mnemonic: String) -> Bool
// 获取扩展公钥 xpub
public static func getPublicKeyFromExtended(extended: String, derivationPath: String) -> PublicKey?
》主要对象方法:
//获取主私钥
public func getMasterKey(curve: Curve) -> PrivateKey
//获取具体币种的私钥
public func getKeyForCoin(coin: CoinType) -> PrivateKey
//获取具体币种地址
public func getAddressForCoin(coin: CoinType) -> String
//获取通过路径字符串获取私钥
public func getKey(derivationPath: String) -> PrivateKey
//获取BIP44的私钥
public func getKeyBIP44(coin: CoinType, account: UInt32, change: UInt32, address: UInt32) -> PrivateKey
//获取扩展私钥
public func getExtendedPrivateKey(purpose: Purpose, coin: CoinType, version: HDVersion) -> String
//获取扩展公钥
public func getExtendedPublicKey(purpose: Purpose, coin: CoinType, version: HDVersion) -> String
四、StoredKey类
》属性
identifier: String
name: String
isMnemonic: Bool
accountCount: Int
》初始化
//创建
public init(name: String, password: Data)
//导入
public static func importPrivateKey(privateKey: Data, name: String, password: Data, coin: CoinType) -> StoredKey?
public static func importHDWallet(mnemonic: String, name: String, password: Data, coin: CoinType) -> StoredKey?
public static func importJSON(json: Data) -> StoredKey?
》其他方法
//获取索引账户
public func account(index: Int) -> Account?
//获取一个币的账户
public func accountForCoin(coin: CoinType, wallet: HDWallet?) -> Account?
//移除币种
public func removeAccountForCoin(coin: CoinType) -> Void
//增加一个账户
public func addAccount(address: String, derivationPath: String, extetndedPublicKey: String) -> Void
//解密私钥
public func decryptPrivateKey(password: Data) -> Data?
//保存
public func store(path: String) -> Bool
//解密助记词
public func decryptMnemonic(password: Data) -> String?
//通过密码获取私钥
public func privateKey(coin: CoinType, password: Data) -> PrivateKey?
//根据密码获取HD钱包对象
public func wallet(password: Data) -> HDWallet?
//导出json格式
public func exportJSON() -> Data?
// 加密密码从私钥重新派生地址。
public func fixAddresses(password: Data) -> Bool
五、Wallet 类 (Sources)
》核心属性
identifier: String
keyURL: URL -- 本地磁盘存储的路径
key: StoredKey --加密的key
accounts: [Account]
》初始化
public init(keyURL: URL, key: StoredKey)
》核心方法:
public func getAccount(password: String, coin: CoinType) throws -> Account
public func getAccounts(password: String, coins: [CoinType]) throws -> [Account]
public func privateKey(password: String, coin: CoinType) throws -> PrivateKey
六、Account 类 (Generated)
》核心属性:
public var address: String //地址
public var derivationPath: String //路径
public var extendedPublicKey: String //扩展公钥
public var coin: CoinType //币种类型
》初始化:
public init(address: String, derivationPath: String, extendedPublicKey: String)
PrivateKey 类(Generated)
》属性:
data:Data
》方法:
初始化:
public init?(data: Data)
public init?(key: PrivateKey)
判断私钥是否正确
public static func isValid(data: Data, curve: Curve) -> Bool
生成不同类型的公钥
public func getPublicKeySecp256k1(compressed: Bool) -> PublicKey
public func getPublicKeyNist256p1() -> PublicKey
public func getPublicKeyEd25519() -> PublicKey
public func getPublicKeyEd25519Blake2b() -> PublicKey
public func getPublicKeyEd25519Extended() -> PublicKey
public func getPublicKeyCurve25519() -> PublicKey
签名:
public func sign(digest: Data, curve: Curve) -> Data?
public func signAsDER(digest: Data, curve: Curve) -> Data?
public func signSchnorr(message: Data, curve: Curve) -> Data?
PublicKey 类(Generated)
》属性:
isCompressed: Bool
compressed: PublicKey
uncompressed: PublicKey
data: Data
keyType: PublicKeyType
》方法:
初始化:
public init?(data: Data, type: PublicKeyType)
判断公钥是否正确
public static func isValid(data: Data, type: PublicKeyType) -> Bool
签名验证
public func verify(signature: Data, message: Data) -> Bool
public func verifySchnorr(signature: Data, message: Data) -> Bool
AnySigner 类(Generated)
各种签名方式
public static func sign<SigningOutput: Message>(input: SigningInput, coin: CoinType) -> SigningOutput
public static func nativeSign(data: Data, coin: CoinType) -> Data
public static func supportsJSON(coin: CoinType) -> Bool
public static func signJSON(_ json: String, key: Data, coin: CoinType) -> String
public static func encode(input: SigningInput, coin: CoinType) -> Data
public static func nativeEncode(data: Data, coin: CoinType) -> Data
public static func decode(data: Data, coin: CoinType) -> Data
public static func plan<TransactionPlan: Message>(input: SigningInput, coin: CoinType) -> TransactionPlan
public static func nativePlan(data: Data, coin: CoinType) -> Data