From 37757fe95d2ef17a0780eb963851684347eaaeb6 Mon Sep 17 00:00:00 2001 From: yuzushioh Date: Wed, 17 Oct 2018 17:37:40 +0900 Subject: [PATCH] add sign methods in HD Wallet --- EthereumKit/Wallet/HDWallet.swift | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/EthereumKit/Wallet/HDWallet.swift b/EthereumKit/Wallet/HDWallet.swift index f4d7e82..05438cc 100644 --- a/EthereumKit/Wallet/HDWallet.swift +++ b/EthereumKit/Wallet/HDWallet.swift @@ -39,3 +39,78 @@ public final class HDWallet { .derived(at: change.rawValue) } } + +// MARK: - Sign Transaction + +extension HDWallet { + + /// Sign signs rlp encoding hash of specified raw transaction + /// + /// - Parameters: + /// - rawTransaction: raw transaction to hash + /// - index: index of a private key which will be used to sign + /// - Returns: signiture in hex format + /// - Throws: EthereumKitError.failedToEncode when failed to encode + public func sign(rawTransaction: RawTransaction, withPrivateKeyAtIndex index: UInt32) throws -> String { + let wallet = Wallet(network: network, privateKey: try privateKey(at: index), debugPrints: false) + return try wallet.sign(rawTransaction: rawTransaction) + } +} + +// MARK :- Sign message + +extension HDWallet { + + /// Sign a provided hex string + /// + /// - Parameters: + /// - hex: hex value to sign (hex format) + /// - index: index of a private key which will be used to sign + /// - Returns: signature in string format + /// - Throws: EthereumKitError.failedToEncode when failed to encode + public func sign(hex: String, withPrivateKeyAtIndex index: UInt32) throws -> String { + let wallet = Wallet(network: network, privateKey: try privateKey(at: index), debugPrints: false) + return try wallet.sign(hex: hex) + } + + /// Sign a provided message + /// + /// - Parameters: + /// - message: message to sign (string format) + /// - index: index of a private key which will be used to sign + /// - Returns: signature in string format + /// - Throws: EthereumKitError.failedToEncode when failed to encode + public func sign(message: String, withPrivateKeyAtIndex index: UInt32) throws -> String { + let wallet = Wallet(network: network, privateKey: try privateKey(at: index), debugPrints: false) + return try wallet.sign(message: message) + } +} + +extension HDWallet { + + /// Sign calculates an Ethereum ECDSA signature for: keccack256("\x19Ethereum Signed Message:\n" + len(message) + message)) + /// See also: https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign + /// + /// - Parameters: + /// - hex: message in hex format to sign + /// - index: index of a private key which will be used to sign + /// - Returns: signiture in hex format + /// - Throws: EthereumKitError.failedToEncode when failed to encode + public func personalSign(hex: String, withPrivateKeyAtIndex index: UInt32) throws -> String { + let wallet = Wallet(network: network, privateKey: try privateKey(at: index), debugPrints: false) + return try wallet.personalSign(hex: hex) + } + + /// Sign calculates an Ethereum ECDSA signature for: keccack256("\x19Ethereum Signed Message:\n" + len(message) + message)) + /// See also: https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign + /// + /// - Parameters: + /// - message: message to sign + /// - index: index of a private key which will be used to sign + /// - Returns: signiture in hex format + /// - Throws: EthereumKitError.failedToEncode when failed to encode + public func personalSign(message: String, withPrivateKeyAtIndex index: UInt32) throws -> String { + let wallet = Wallet(network: network, privateKey: try privateKey(at: index), debugPrints: false) + return try wallet.personalSign(message: message) + } +}