Bringing the Dependencies of a BTCR Wallet to the Swift Ecosystem

Wolf McNally for Blockchain Commons

The Swift Ecosystem

Swift is a programming language introduced by Apple in 2014 as the new official programming language for writing iOS and macOS apps. In 2015 Apple released Swift under the Apache open source license and also seeded a build of Swift for Linux. A growing community of developers headquartered at Swift.org are extending the language in various ways, including Google's high-performance numerical computation library Swift for TensorFlow and server-side application frameworks such as Vapor. IBM has launched an initiative to promote server-side Swift, including Kitura, its own Swift server framework. Swift has a number of traits that make it attractive for use on the desktop, on mobile devices and cloud services. Swift is a modern, multi-paradigm language designed to be safe, fast, and expressive. In this document, the phrase "Apple platforms" refers collectively to iOS, macOS, and tvOS. The use of Swift across Apple platforms and Linux is what this document refers to as the Swift ecosystem.

Bridging BTCR Dependencies to Swift

While all the code to implement something as complex as a DID resolver or registrar could be written entirely in Swift, it makes sense to leverage code already written in other languages, and use Swift as a top-level language used to tie heterogenous modules into a unified application, whether it be a mobile application or server. This document surveys programing languages and technologies of interest, discusses issues of interoperating with Swift, and lists software packages of note.

The author invites additions and clarifications from the reader.


Programming Languages

C

C is the grandfather of most modern programming languages, and is still valued for its simplicity, speed, and interoperability with other languages.

The whole Swift ecosystem can call C functions directly..

Packages of interest written in C include:

C++

C++ is an object-oriented dialect of C, and is one of the main workhorse languages in use today.

Swift cannot call C++ directly, but we can get around this by writing a C-based shim— Swift calls the C API and the C API calls C++. This technique works under the entire Swift ecosystem.

Packages of interest written in C++ include:

Javascript

JavaScript is a high-level, interpreted programming language. It is a language that is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm. Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web.

Apple platforms include JavaScriptCore, a built-in framework used by the WebKit engine in the Safari Browser, but also made available directly to Apple platforms developers for evaluating JavaScript programs. On Linux, Swift applications can integrate Javascript by using an execution engine like V8.

Packages of interest written in JavaScript include:

Go

Go (AKA Golang) is a statically typed, compiled programming language designed at Google.

Swift running under iOS can bridge to Go using Go Mobile. Under Linux, Go modules can be compiled to export C functions that can be called by Swift.

Packages of interest written in Go include:

btcd— A full node bitcoin implementation. One key difference between btcd and Bitcoin Core is that btcd does NOT include wallet functionality and this was a very intentional design decision. That functionality is provided by the btcwallet project. btcwallet— A daemon handling bitcoin wallet functionality for a single user. spvwallet— Lightweight p2p SPV wallet and library in Go. It connects directly to the bitcoin p2p network to fetch headers, merkle blocks, and transactions.


Other Technologies

JSON

JSON, as specified in RFC7159, is a simple data description language for representing objects on the Web.

The Swift Foundation framework, available on Apple platforms and Linux, includes APIs for working with JSON:

JSON-LD

JSON-LD is an extension of JSON designed as a light-weight syntax that can be used to express Linked Data, and is specifically used in the Decentralized Identifiers (DID) specification for constructing DID Documents. A JSON-LD implementation will usually provide the functionality described in JSON-LD 1.1 Processing Algorithms and API.

Swift can integrate JSON-LD by bridging to the jsonld.js package listed above.

DID Document Hosting

The process of resolving, creating, updating, or revoking a DID implies that the target DID Document itself is stored somewhere under the control of its owner. A DID wallet will need to be able to store, retrieve, and update DID documents via a hosting service or protocol.

Services and protocols of note include:

Linked Data Signatures

Linked Data Signatures describes a mechanism for ensuring the authenticity and integrity of Linked Data documents using digital signatures. It can be applied to Linked Data formats such as JSON-LD or RDF. This is how DID documents are signed.

Swift can integrate Linked Data Signatures by bridging to the jsonld-signatures package listed above.

REST

REST is acronym for REpresentational State Transfer. RESTful web services (RWS), provide interoperability between computer systems on the Internet.

The Swift ecosystem uses the Foundation URLSession API to perform HTTP REST requests. Packages like Alamofire build more elegant conveniences on top of this.

REST APIs of note include:

SPV

Simple Payment Verification (SPV) is a technique described in Satoshi Nakamoto’s paper. SPV allows a lightweight client (like a mobile app) to verify that a transaction is included in the Bitcoin blockchain, without downloading the entire blockchain. The SPV client only needs download the block headers directly from the Bitcoin network, which are much smaller than the full blocks. To verify that a transaction is in a block, a SPV client requests a proof of inclusion, in the form of a Merkle branch. SPV clients offer more security than web wallets, because they do not need to trust the servers with the information they send.

Packages that provide SPV services that can be called from Swift include:

BreadWallet

Bread Wallet (AKA BreadWallet or BRD) is an open source SPV wallet for iOS and Android that supports a variety of cryptocurrencies, including Bitcoin-based altcoins and Ethereum tokens. Bread Wallet is under active development, hosted on GitHub and released under the MIT open source license. The iOS version has been forked 228 times as of this writing. The iOS and Android versions of Bread Wallet share the same C-based BreadWallet Core (see above) to provide SPV functionality. The current iOS version is written in Swift.

One potential path for bringing a BTCR wallet to iOS would be to fork Bread Wallet and then extend it to include BTCR DID capabilities. Benefits of this approach include leveraging a mature and well-tested SPV codebase and shipping app. Possible drawbacks include dealing with possible (TBD) mismatches between the assumptions and requirements of Bread Wallet and those of a BTCR wallet. In any case, incorporating any of the necessary technologies listed above (e.g., JSON-LD) should be possible via the bridging techniques described.