Skip to content

Swift wrapper for LibWally, a collection of useful primitives for cryptocurrency wallets

License

Notifications You must be signed in to change notification settings

Sjors/libwally-swift

Repository files navigation

LibWally Swift Build Status

Opinionated Swift wrapper around LibWally, a collection of useful primitives for cryptocurrency wallets.

Supports a minimal set of features based on v0.8.8. See also original docs.

  • Core Functions
    • base58 encode / decode
  • Crypto Functions
    • sign ECDSA, convert to DER
  • Address Functions
    • Parse to scriptPubKey
    • Generate from scriptPubKey #7 (wishlist, done for SegWit)
    • Derive
    • WIF
    • Detect bech32 typos #4 (wishlist)
    • bech32 and bech32m
  • BIP32 Functions
    • Derive scriptPubKey #6 (wishlist)
  • BIP38 Functions
  • BIP39 Functions
  • Descriptor functions
    • Parse and canonicalize
    • Convert to address
    • Convert to scriptPubKey
  • Script Functions
    • Serialize scriptPubKey
    • Determine scriptPubkey type
  • PSBT functions
    • Parse and serialize (base64 / binary)
    • Check completeness and extract transaction
  • Transaction Functions
    • Compose and sign transaction
    • Calculate fee

Items marked with wishlist are not (yet) available upstream.

Multisig as well as Elements specific functions such as confidential addresses are not implemented.

Works with iOs 11+ on 64-bit devices and the simulator.

Usage

Derive address from a seed:

let mnemonic = BIP39Mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
let masterKey = HDKey(mnemonic.seedHex("bip39 passphrase"))!
masterKey.fingerprint.hexString
let path = "m/44'/0'/0'"
let account = try! masterKey.derive(path)
account.xpub
account.address(.payToWitnessPubKeyHash)

Derive address from an xpub:

let account = HDKey("xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ")
let receivePath = "0/0"
key = account.derive(receivePath)
key.address(.payToPubKeyHash) # => 1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj

Parse an address:

var address = Address("bc1q6zwjfmhdl4pvhvfpv8pchvtanlar8hrhqdyv0t")
address?.scriptPubKey # => 0014d09d24eeedfd42cbb12161c38bb17d9ffa33dc77
address?.scriptPubKey.type # => .payToWitnessPubKeyHash

Create and sign a transaction:

let txId = "400b52dab0a2bb5ce5fdf5405a965394b43a171828cd65d35ffe1eaa0a79a5c4"
let vout: UInt32 = 1
let amount: Satoshi = 10000
let witness = Witness(.payToWitnessPubKeyHash(key.pubKey))
let input = TxInput(Transaction(txId)!, vout, amount, nil, witness, scriptPubKey)!
transaction = Transaction([input], [TxOutput(destinationAddress.scriptPubKey, amount - 110)])
transaction.feeRate // Satoshi per byte
let accountPriv = HDKey("xpriv...")
let privKey = try! accountPriv.derive("0/0")
transaction.sign([privKey])
transaction.description # transaction hex

See also the included Playground and tests.

Install

As part of a project.

### CocoaPods

Add to your Podfile:

pod 'LibWally', :git => 'https://github.com/Sjors/LibWally-Swift.git', :tag => 'v0.0.3', :submodules => true

and then run:

pod install --verbose

Build

For development.

Install dependencies:

brew install gnu-sed automake libtool

Clone the repository, including submodules:

git clone https://github.com/Sjors/libwally-swift.git --recurse-submodules

Xcode will build libwally-core for you, which may take a few minutes.

When making changes to libwally-core, keep in mind that cleaning the build folder in Xcode will not trigger a recompile. You need to call git clean -dfx inside CLibWally/libwally-core.

About

Swift wrapper for LibWally, a collection of useful primitives for cryptocurrency wallets

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages