/slp-wallet-sdk-ios-demo

This is the SLP SDK demo for iOS built with Swift by the Bitcoin.com Wallet Team. Enjoy!

Primary LanguageSwiftMIT LicenseMIT

Logo

SLPWallet Demo 🐍 (Built with VIPER)

Version Platform Compatibility Compatibility License

This app demonstrates how to use the SLPWallet iOS SDK in a wallet. With it, you can:

  • Receive SLP tokens.
  • Send SLP tokens.
  • Receive BCH to fund your SLP transfers.
  • View your token balances.
  • View your BCH balance.
  • View your mnemonic to backup your wallet.
  • Restore your wallet from a mnemonic.

Get Started

Playing with the SDK

You can create an SLP wallet in a few lines only. This code comes from WalletManager.swift. All other files are UI related code and don't concern the SDK.

//
// ...
//

import SLPWallet

//
// ...
//

class WalletManager {
    
    static let shared = WalletManager()
    
    var wallet: SLPWallet
    
    var observedToken: BehaviorRelay<SLPToken>?
    var observedTokens: PublishSubject<SLPToken>?
    
    init() {
        do {
            wallet = try SLPWallet(.mainnet)
            setup()
        } catch {
            fatalError("It should be able to construct a wallet")
        }
    }

    //
    // ... 
    //
}

extension WalletManager: SLPWalletDelegate {
    
    // Call with SLPWalletDelegate ❗️💥🚀
    func onUpdatedToken(_ token: SLPToken) {
        
        // Notify
        if let observedToken = self.observedToken
            , observedToken.value.tokenId == token.tokenId {
            observedToken.accept(token)
        }
        
        // Notify
        if let observedTokens = self.observedTokens {
            observedTokens.onNext(token)
        }
        
    }
}

After creating your wallet, you can start sending tokens. This file describes how to send tokens: SendTokenInteractor.swift. Below you can see a preview of the file.

//
// ...
//

class SendTokenInteractor {
    
    fileprivate let bag = DisposeBag()
    
    func sendToken(_ tokenId: String, amount: Double, toAddress: String) -> Single<String> {
        return Single<String>.create { single in
            WalletManager.shared.wallet
                .sendToken(tokenId, amount: amount, toAddress: toAddress) // That's it 💥🚀
                .subscribe(onSuccess: { txid in
                    single(.success(txid))
                }, onError: { error in
                    single(.error(error))
                })
                .disposed(by: self.bag)
            return Disposables.create()
        }
    }
}

Demo

Alt Text

Authors & Maintainers

References

License

SLPWalletDemo is available under the MIT license. See the LICENSE file for more info.