项目作者: memtrip

项目描述 :
EOS libraries for Swift, designed primarily for iOS development.
高级语言: Swift
项目地址: git://github.com/memtrip/eos-swift.git
创建时间: 2018-12-27T11:55:04Z
项目社区:https://github.com/memtrip/eos-swift

开源协议:Apache License 2.0

下载


eos-swift

EOS libraries for swift, designed primarily for iOS development.

Install with CocoaPods

Tested with pod —version: 1.5.3

  1. use_frameworks!
  2. target 'YOUR_TARGET_NAME' do
  3. pod 'eosswift', '1.5'
  4. end

Replace YOUR_TARGET_NAME and then, in the Podfile directory, type:

  1. pod install

Getting started

The eosswiftTests directory contains a full suite of integration tests, explore these
tests to quickly gauge what functionality is provided by the SDK.

  • eos-chain-actions tests the highest level of abstraction; pushing transactions composed of actions.
  • eos-http-rpc tests the interaction with nodeos HTTP RPC api.
  • eos-abi-writer tests the building of local abi bytes

eos-core

The core module contains the core building blocks required to interact with the EOS network.

Key pairs

EOS keypairs are generated using the micro-ecc lib.

Create a new keypair:

  1. // new key pair
  2. let eosPrivateKey = try! EOSPrivateKey()
  3. let eosPublicKey = eosPrivateKey.publicKey
  4. // private key from encoded string
  5. let privKey1 = try! EOSPrivateKey(base58: "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3")
  6. // private key from bytes
  7. let privKey2 = try! EOSPrivateKey(ecKeyPrivateKey: ECPrivateKey(privKeyData: eosPrivateKey.bytes()))

Sign bytes with a private key

When a transaction is pushed to the EOS network, the packed transaction bytes must be
signed by an EOS private key.

  1. let bytesToSign = imaginaryAbi.toData()
  2. let privateKey = try! EOSPrivateKey()
  3. let signature = PrivateKeySigning().sign(digest: bytesToSign, eosPrivateKey: privateKey)

Block Id details

Extract the block number and prefix from a block id.

  1. let blockIdDetails = BlockIdDetails(blockId: "0000000ac7619ca01df1e0b4964921020e772ceb7343ec51f65537cdbce192d3")
  2. let blockNum = blockIdDetails.blockNum
  3. let blockPrefix = blockIdDetails.blockPrefix

eos-abi-writer

A local replacement of abi_json_to_bin, the Encodable protocol is used with reflection to write struct
values to a byte array.

Model

Byte writer models are structs that adopt the Encodable protocol, the type of the member variable
is used to determine how the data will be written to the byte array. For more complex types a derivative
of AbiTypeWriter can be used, such as; AssetWriterValue, AccountNameWriter or PublicKeyWriter.

  • List of complex AbiTypeWriter types
    1. struct TransactionAbi : Encodable {
    2. let expiration: TimestampWriterValue
    3. let ref_block_num: BlockNumWriterValue
    4. let ref_block_prefix: BlockPrefixWriterValue
    5. let max_net_usage_words: UInt64
    6. let max_cpu_usage_ms: UInt64
    7. let delay_sec: UInt64
    8. let context_free_actions: [ActionAbi]
    9. let actions: [ActionAbi]
    10. let transaction_extensions: StringCollectionWriterValue
    11. }

Write bytes

The Encodable protocol has two extension funcs called toHex() and toData().
The toData() function will write the struct values to a byte array. The toHex()
function will write the struct values to a byte array and encode the bytes as a hexadecimal string,
in other words, an abi_bin.

  1. public struct TransferArgs : Encodable {
  2. let from: AccountNameWriterValue
  3. let to: AccountNameWriterValue
  4. let quantity: AssetWriterValue
  5. let memo: String
  6. }
  7. let args = TransferArgs(
  8. from: AccountNameWriterValue(name: "memtripblock"),
  9. to: AccountNameWriterValue(name: "memtripproxy"),
  10. quantity: AssetWriterValue(asset: "0.0001 EOS"),
  11. memo: "memo"
  12. )
  13. let hexValue = args.toHex(AbiEncoder(capacity: 512))

Examples

See the eos-swiftTests/eos-chain-actions/abihex test package for abi byte writing examples.

eos-http-rpc

A http client used to makes requests to the nodeos RPC HTTP API.

Factory

The http client interfaces can be created using ChainApiFactory and HistoryApiFactory.

  1. let chainApi = ChainApiFactory.create(rootUrl: Config.CHAIN_API_BASE_URL)

ChainApi

The ChainApi interface contains all the network requests for the chain/ resource.
e.g; chain/get_info

  1. chainApi.getInfo().subscribe(onSuccess: { response in
  2. let info: Info = response.body!
  3. }, onError: { error in
  4. let httpErrorResponse = error as! HttpErrorResponse<ChainError>
  5. print(httpErrorResponse.bodyString)
  6. })

The HistoryApi interface contains all the network requests for the history/ resource.
e.g; history/get_transaction

  1. historyApi.getTransaction(body: GetTransaction(id: action.action_trace.trx_id)).subscribe(onSuccess: { response in
  2. let historicTransaction: HistoricTransaction = response.body!
  3. }, onError: { error in
  4. let httpErrorResponse = error as! HttpErrorResponse<ChainError>
  5. print(httpErrorResponse.bodyString)
  6. })

Credits

Vote for memtripblock

If you find this SDK useful, please vote for memtripblock
as a block producer. We are committed to open sourcing all the software we develop, let’s build the future of EOS on mobile together!