Bringing the Dependencies of a BTCR Wallet to the Swift Ecosystem
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.
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:
- Libsecp256k1— Optimized C library for EC operations on curve secp256k1.
- Used by: Bitcoin Core and Libbitcoin
- Nettle— A low-level cryptographic library that is designed to fit easily in more or less any context.
- Used by: Bread Wallet
- Breadwallet Core— An implementation of SPV.
- Used by: Bread Wallet
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:
- Libbitcoin— Libbitcoin is a multipurpose bitcoin library targeted towards high end use. An ideal backend to build fast implementations on top: mobile apps, desktop clients and server API's. The library places a heavy focus around asychronicity, speed and availability.
- Blockchain Commons have already released a framework for bridging from Libbitcoin to Swift written by the author.
- Bitcoin Core— A free and open-source application that serves as a bitcoin node and provides a bitcoin wallet which fully verifies payments. It is considered to be bitcoin's reference implementation.
- Boost— A set of libraries that provide support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains over eighty individual libraries.
- Used by Libbitcoin.
- jsonld-signatures— An implementation of the Linked Data Signatures specification for JSON-LD.
- Webcoin— A Bitcoin SPV client that works in Node.js and the browser.
Go (AKA Golang) is a statically typed, compiled programming language designed at Google.
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.
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:
Codableprotocol, which works in concert with the
JSONDecoderclasses to serialize statically-typed data structures.
JSONSerializationclass, which can be used to structure and navigate dynamic JSON objects.
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:
- GitHub— A web-based hosting service for version control using Git. It is mostly used for computer code, but can host data of any type. It offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features.
- DropBox— A file hosting service that offers cloud storage, file synchronization, personal cloud, and client software.
- Amazon S3— A "simple storage service" offered by Amazon Web Services (AWS) that provides object storage through a web service interface.
- IPFS— A protocol and network designed to create a content-addressable, peer-to-peer method of storing and sharing hypermedia in a distributed file system. Users typically run an IPFS node that stores their own files, which are replicated upon demand to other IPFS nodes. Data placed into IPFS is public by default, and must be encrypted by the user if it is to remain controlled. Notably, once data is placed into IPFS, it needs to be considered indelible.
- Storj— A decentralized, distributed, encrypted file system. Storj data is end-to-end encrypted and can only be accessed by its owners by default, but Storj can also be used to host public buckets as a CDN.
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 is acronym for REpresentational State Transfer. RESTful web services (RWS), provide interoperability between computer systems on the Internet.
REST APIs of note include:
- GitHub API— Used to manage GitHub repositories and commit data such as DID Documents.
- Dropbox API— The Dropbox API allows developers to work with files in Dropbox, including advanced functionality like sharing.
- Amazon S3 API— You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web.
- IPFS API— When an IPFS node is running as a daemon, it exposes an HTTP API that allows you to control the node and run the same commands you can from the command line.
- Storj API— Access the Storj network using a simple REST API.
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:
- Webcoin (see above)
- BreadWallet Core (see above)
- spvwallet (see above)
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.