Skip to content

ajamaica/solana-swift-1

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SolanaSwift

Solana-blockchain client, written in pure swift.

Version License Platform

Features

  • Key pairs generation
  • Networking with POST methods for comunicating with solana-based networking system
  • Create, sign transactions
  • Socket communication // TODO

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 11 or later
  • RxSwift

Dependencies

  • RxAlamofire
  • TweetNacl
  • CryptoSwift
  • Socket-io client // TODO

Installation

SolanaSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SolanaSwift', :git => 'https://github.com/p2p-org/solana-swift.git'

How to use

  • Every class or struct is defined within namespace SolanaSDK, for example: SolanaSDK.Account, SolanaSDK.Error.

  • Import

import SolanaSwift
  • Create an AccountStorage for saving account's keyPairs (public and private key), for example: KeychainAccountStorage for saving into Keychain in production, or InMemoryAccountStorage for temporarily saving into memory for testing. The AccountStorage must conform to protocol SolanaSDKAccountStorage, which has 2 requirements: function for saving save(_ account:) throws and computed property account: SolanaSDK.Account? for retrieving user's account.

Example:

import KeychainSwift
struct KeychainAccountStorage: SolanaSDKAccountStorage {
    let tokenKey = <YOUR_KEY_TO_STORE_IN_KEYCHAIN>
    func save(_ account: SolanaSDK.Account) throws {
        let data = try JSONEncoder().encode(account)
        keychain.set(data, forKey: tokenKey)
    }
    
    var account: SolanaSDK.Account? {
        guard let data = keychain.getData(tokenKey) else {return nil}
        return try? JSONDecoder().decode(SolanaSDK.Account.self, from: data)
    }
}

struct InMemoryAccountStorage: SolanaSDKAccountStorage {
    private var _account: SolanaSDK.Account?
    func save(_ account: SolanaSDK.Account) throws {
        _account = account
    }
    var account: SolanaSDK.Account? {
        _account
    }
}
  • Creating an instance of SolanaSDK:
let solanaSDK = SolanaSDK(endpoint: <YOUR_API_ENDPOINT>, accountStorage: KeychainAccountStorage.shared) // endpoint example: https://api.mainnet-beta.solana.com
  • Creating an account:
let mnemonic = Mnemonic()
let account = try SolanaSDK.Account(phrase: mnemonic.phrase)
try solanaSDK.accountStorage.save(account)

Example:

solanaSDK.getBalance(account: account, commitment: "recent")
    .subscribe(onNext: {balance in
        print(balance)
    })
    .disposed(by: disposeBag)
  • Send token with send(to:, amount:) method:
solanaSDK.send(to: <ACCOUNT_PUBLIC_KEY>, amount: <LAMPORTS>)
    .subscribe(onNext: {result in
        print(result)
    })
    .disposed(by: disposeBag)
  • Send custom method, which was not defined by using method request<T: Decodable>(method:, path:, bcMethod:, parameters:) -> Single<T>

Example:

(solanaSDK.request(method: .post, bcMethod: "aNewMethodThatReturnsAString", parameters: []) as Single<String>)
  • Observe socket events:

// TODO: The socket implementation has not been ready yet.

Contribution

  • For supporting new methods, data types, edit SolanaSDK+Methods or SolanaSDK+Models
  • For testing, run Example project and creating test using RxBlocking
  • Welcome to contribute, feel free to change and open a PR.

Author

Chung Tran, [email protected]

License

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 99.4%
  • Ruby 0.6%