-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance wallet class #159
Enhance wallet class #159
Conversation
let vout: UInt32 | ||
let sequence: UInt32 | ||
let scriptSig: ScriptSig | ||
// let addr: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are they comment out? Please comment the reason somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are part of API response, but not used.
So I commented them out.
import Foundation | ||
|
||
public protocol AddressProvider { | ||
// GET API: reload utxos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is inappropriate, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it isn't. I'll fix it.
import Foundation | ||
|
||
public protocol TransactionBuilder { | ||
func build(destinations: [(address: Address, amount: UInt64)], utxos: [UnspentTransaction]) throws -> UnsignedTransaction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name might be confusing.
TransactionBuilder
should build Transaction
not UnsignedTransaction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name is okay with this. But I think the name of Transaction
and UnsignedTransaction
are the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UnsignedTransaction is almost just a wrap of Transaction and UnspentOutput
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
|
||
import Foundation | ||
|
||
public protocol TransactionProvider { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this one is also confusing.
How about TransactionHistoryProvider
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true. I'll change it.
import Foundation | ||
|
||
public protocol TransactionProvider { | ||
// GET API: reload transactions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function would not be only with API, but also SPV or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
import Foundation | ||
|
||
public protocol UtxoProvider { | ||
// GET API: reload utxos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as TransactionProvider
.
Do not need to limit only API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
|
||
// MARK: - WalletDataStoreProtocol Extension | ||
internal enum WalletDataStoreKey: String { | ||
case wif, internalIndex, extenralIndex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This protocol is not for HDWallet, isn't it?
Then, I think internalIndex
and extenralIndex
are not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This protocol will be for HDWallet as well.
|
||
public protocol AddressProvider { | ||
// GET API: reload utxos | ||
func reload(keys: [PrivateKey], completion: (([Address]) -> Void)?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot understand the reason why this function is necessary.
Is it enough to convert PrivateKey
into Address
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, converting PrivateKey
to Address
is not enough.
Mainly this protocol is for P2SH.
Someone may want to compute new P2SH addresses using the keys outside as well as the keys inside by using API or some other way.
|
||
public func reload(keys: [PrivateKey], completion: (([Address]) -> Void)?) { | ||
let data = try? JSONEncoder().encode(keys.map { $0.publicKey().toCashaddr() }) | ||
userDefaults.set(data, forKey: UserDefaultsKey.cashaddrs.rawValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to add completion?()
?
utxoSelector: UtxoSelector = StandardUtxoSelector.default, | ||
transactionBuilder: TransactionBuilder = StandardTransactionBuilder.default, | ||
transactionSigner: TransactionSigner = StandardTransactionSigner.default) { | ||
guard let privkey = try? PrivateKey(wif: wif) else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
guard let wif = walletDataStore.getString(forKey: .wif) else { | ||
return nil | ||
} | ||
guard let privkey = try? PrivateKey(wif: wif) else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
|
||
func createWalletIfNeeded() { | ||
if wallet == nil { | ||
let privkey = PrivateKey(network: .testnet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
qrCodeImageView.image = wallet?.address.qrImage() | ||
addressLabel.text = wallet?.address.cashaddr | ||
if let balance = wallet?.balance() { | ||
balanceLabel.text = "残高:\(balance) satoshi" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use English
// ViewController.swift | ||
// WalletExample | ||
// | ||
// Created by Shun Usami on 2018/09/18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change copyright
public struct BitcoinComEndpoint { | ||
public static let testnet: BitcoinComEndpoint = BitcoinComEndpoint(baseUrl: "https://trest.bitcoin.com/v1/") | ||
public static let mainnet: BitcoinComEndpoint = BitcoinComEndpoint(baseUrl: "https://rest.bitcoin.com/v1/") | ||
public struct ApiEndPoint { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you modify file name too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description of the Change
Implement high level class
Wallet
.Now everyone can send/receive Bitcoin with only 10 lines.
AddressProvider, UtxoProvider, TransactionHistoryProvider
Basically, both
reload()
andlist()
are methods for retrieving addresses, utxos and transactions. The difference between them is thatreload()
is async method, whilelist()
is sync method.Alternate Designs
None.
Benefits
More beginner welcome!
Possible Drawbacks
None.
Applicable Issues
closes #156