Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/greymass/swift-eosio
Library for swiftly working with EOSIO blockchains on Linux, macOS and iOS with ESR support
https://github.com/greymass/swift-eosio
eos-resources eos-swift eosio
Last synced: about 1 month ago
JSON representation
Library for swiftly working with EOSIO blockchains on Linux, macOS and iOS with ESR support
- Host: GitHub
- URL: https://github.com/greymass/swift-eosio
- Owner: greymass
- License: other
- Created: 2019-10-17T20:35:34.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T17:45:52.000Z (9 months ago)
- Last Synced: 2024-10-02T10:16:32.362Z (2 months ago)
- Topics: eos-resources, eos-swift, eosio
- Language: Swift
- Homepage:
- Size: 1010 KB
- Stars: 14
- Watchers: 5
- Forks: 17
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-eosio - greymass/swift-eosio - Swift SDK with ESR support. (Developers / Libraries and Frameworks)
- awesome-wax - swift-eosio - Alternative Antelope API library for Swift. (Developer Resources / Languages)
README
EOSIO for Swift
===============Library for swiftly working with EOSIO blockchains on MacOS, Linux and iOS.
Features:
- Strongly typed EOSIO ABI encoding and decoding using Swift's `Codable` protocol
- Untyped EOSIO ABI coding using EOSIO ABI definitions (JSON & binary)
- Extendable HTTP API Client
- All EOSIO primitive types implemented in Swift with ergonomic interfaces (Asset, Name, PublicKey etc.)
- Fast and battle-hardened ECDSA via libsecp256k1
- Signing requests (ESR/EEP-7)Installation
------------In your `Package.swift`'s dependencies:
```swift
.package(url: "https://github.com/greymass/swift-eosio.git", .branch("master")),
```Usage example
-------------```swift
import EOSIO
import Foundationstruct MyAction: ABICodable, Equatable {
let message: String // most native types conform to ABICodable
let from: Name // all eosio builtins have their own type
let tip: Asset? // optionals just work
let extra: [MyAction] // so does complex types
}let action = MyAction(
message: "hi mom",
from: "2goodgenes", // most eosio types can be expressed by literals
tip: "3.00 BUCKZ",
extra: [
MyAction(message: "yup", from: "me", tip: nil, extra: [])
]
)// types have same memory layout as their c++ eosio counterparts
print(action.from.rawValue) // 1380710289163812864
print(action.tip!.symbol) // 2,BUCKZ
print(action.tip!.units) // 300// types conform to standrad protocols where applicable
print(action.from == "2goodgenes") // true
print(action.tip! * 10) // "30.00 BUCKZ"
print(action.tip! + "0.99 BUCKZ") // "3.99 BUCKZ"
print(Name(String(action.from).replacingOccurrences(of: "good", with: "BÅÅD"))) // 2....genes// encode action to json
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try! jsonEncoder.encode(action)
print(String(bytes: jsonData, encoding: .utf8)!)
/*
{
"extra" : [
{
"message" : "yup",
"extra" : [],
"from" : "me"
}
],
"message" : "hi mom",
"tip" : "3.00 BUCKZ",
"from" : "2goodgenes"
}
*/// encode action to binary
let abiEncoder = ABIEncoder()
let binData: Data = try! abiEncoder.encode(action)
print(binData.hexEncodedString())
// 066869206d6f6d00005653b1442913012c01000000000000024255434b5a0000010379757000000000000080920000// decoding actions
let abiDecoder = ABIDecoder() // same for JSONDecoder
let decodedAction = try! abiDecoder.decode(MyAction.self, from: binData)
print(decodedAction == action) // true// untypepd coding using ABI definitions
let myAbiJson = """
{
"version": "eosio::abi/1.1",
"structs": [
{
"name": "my_action",
"base": "",
"fields": [
{"name": "message", "type": "string"},
{"name": "from", "type": "name"},
{"name": "tip", "type": "asset?"},
{"name": "extra", "type": "my_action[]"}
]
}
]
}
"""
let jsonDecoder = JSONDecoder()
// ABI defs are also ABICodable
let abi = try! jsonDecoder.decode(ABI.self, from: myAbiJson.data(using: .utf8)!)
print(abi.resolveStruct("my_action")!.map({ $0.name })) // ["message", "from", "tip", "extra"]// untyped decoding
let anyFromBin = (try! abiDecoder.decode("my_action", from: binData, using: abi))
let anyFromJson = (try! jsonDecoder.decode("my_action", from: jsonData, using: abi))let objFromBin = anyFromBin as! [String: Any]
let objFromJson = anyFromJson as! [String: Any]print(objFromJson["from"] as! Name) // 2goodgenes
print(objFromJson["from"] as? Name == objFromBin["from"] as? Name) // true
print(objFromJson["from"] as? Name == action.from) // true
```