Send Money with the Coinbase Swift SDK


Photo by Thought Catalog / Unsplash

I recently added the ability to send money using the Coinbase Swift SDK that we're building alongside Quids.

When going through the OAuth flow you need to add the CoinbaseAPIClient.Scope.createTransactions to your list of scopes:

let authScopes: [CoinbaseAPIClient.Scope] = [
    .readAccounts,
    .readTransactions,
    .createTransactions(sendLimit: 500.0, currencyCode: "USD", period: .day)
]

Then once you have the required permission you will need to build a SendMoney request and pass that to your CoinbaseAPIClient:

let request = CoinbaseAPIClient.SendMoney(to: ethAddress, amount: 2.5, currencyCode: "ETH")

coinbase.send(money: request, from: accountID, twoFactorCode: nil) { (transaction, errors) in
    // ...
}

You'll notice this function can also take a 2FA code. If Coinbase returns a CoinbaseAPIClient.APIError.twoFactorRequired error, you should ask your user for a 2FA code and then re-call the send money function with the 2FA code.

Here is an example of how we're doing this in Quids:
Screen-Shot-2018-06-25-at-07.02.56

Coinbase Swift SDK

Over the weekend I released the Coinbase Swift SDK I've been building alongside Quids. It is still under heavy development so expect breaking changes!

Install

Carthage

github "reddavis/Coinbase"

Examples

Initialization

The framework gives you the flexibility of how you store the oAuth credentials. You will need to implement a class that conforms to the CoinbaseAPIClientAuthStore protocol. var isAuthenticated, var hasExpired and func delete() all have default implementations, so you will only need to implement var auth.

The library will handle the refreshing of tokens.

public protocol CoinbaseAPIClientAuthStore: class
{
    var auth: CoinbaseAPIClient.Auth? { get set }
    var isAuthenticated: Bool { get }
    var hasExpired: Bool { get }

    func delete()
}



public extension CoinbaseAPIClientAuthStore
{
    public var isAuthenticated: Bool {
        return self.auth != nil
    }

    public var hasExpired: Bool {
        guard let unwrappedAuth = self.auth else
        {
            return true
        }

        return unwrappedAuth.expiresAt < Date()
    }

    public func delete()
    {
        self.auth = nil
    }
}

Auth

Firstly you need to initialize the auth flow:

let scopes: [CoinbaseAPIClient.Scope] = [
    .readAccounts,
    .readAddresses,
    .createAddresses,
    .readTransactions,
    .readPaymentMethods,
    .readUser,
    .updateUser,
    .readUserEmailAddress
]

let url = self.coinbaseAPIClient.authorizeURL(scopes: scopes)
NSWorkspace.shared.open(url)

Then you need to get the oAuth token:

let redirectURL = URL(string: "quids://coinbase/auth")!

self.coinbaseAPIClient.authenticate(code: code, redirectURL: redirectURL) { (success, error) in
    self.authCompletionHandler?(success, error)
}

Requests

Only a few requests are currently supported, more will be added as we add features to Quids.

public func fetchAccounts(_ completionHandler: @escaping (_ accounts: [Account]?, _ errors: [Error]?) -> Void)
public func fetchTransactions(accountID: String, completionHandler: @escaping (_ transactions: [Transaction]?, _ errors: [Error]?) -> Void)
public func fetchCurrentUser(_ completionHandler: @escaping (_ user: User?, _ errors: [Error]?) -> Void)
public func updateCurrentUser(edits: UserEdits, completionHandler: @escaping (_ user: User?, _ errors: [Error]?) -> Void)
public func createAddress(accountID: String, completionHandler: @escaping (_ address: Address?, _ errors: [Error]?) -> Void)
public func fetchExchangeRates(baseCurrencyCode: String, completionHandler: @escaping (_ exchangeRate: ExchangeRate?, _ errors: [Error]?) -> Void)

Xcode 9.3 Simulator Freezes

For a few days I had an issue where every so often the iOS simulator would freeze when booting up. It would just display an activity indicator. Then I saw this Tweet and now all is fixed!

Weird Xcode install issues with unbootable simulators?

xcrun simctl erase all

resolved it for me

— Jason Kneen (@jasonkneen) April 30, 2018

Cool Things - April Edition

The Subtle Art of Not Giving a F*ck

Brave Web Browser - A project to keep an eye on. It was reasonably stable on macOS but has recently crashed a lot, so I have moved back to Safari for now.

Revolut Business - Revolut added Freeagent sync. Once it supports unique IBAN numbers, rather than relying on a reference number, I'm all in.

Showerthoughts - a macOS screensaver I built.

Balance Manager (beta) - A web-based tool to help you manage your tokens.