SwiftyEOS is an open source framework for interacting with EOS, written in Swift. Available on iOS and macOS.

Features: – EOS key pair generation – Private key import – Signature hashing – Basic RPC API (chain/History) queryable client – Transactions (EOS Token transfer) – Help classes to handle offline wallets on iOS – Encrypt/decrypt import private keys on iOS

How to use it

  • 1. TheLibrariesandSourcesThe folder is copied to the project, not requiredmain.swift.
  • 2. If it is not for iOS platforms, delete itSources/Utils/iOS.
  • 3.Libraries/includeAdd to the Header search path.
  • 4. WillLibraries/include/Bridging-Header.hSet it to Objective-C Bridging Header. If you have your own Bridging headers, copy all the imports from that file and paste them into your own file.
  • 5. Compile and wait

Key pair generation

SwiftyEOS now supports secP256K1 key pair.

Secp256r1 key pair generation bug but I can’t figure out why. Create the key from the unit test created by CLEOS — R1 will not pass. You might not consider secp256R1 as an option, because the cleos wallet command doesn’t import these keys either.

Generate a random key pair:

let (pk, pub) = generateRandomKeyPair(enclave: .Secp256k1)
Copy the code

Easy, right?

print("private key: \(pk! .wif())") print("public key : \(pub! .wif())") // private key: PVT_K1_5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL // public key : PUB_K1_4yDYdmcVcXxAxeNsUWRG7x9FKQE4HbJZdzgZFv1AYxk6oSVcLdCopy the code

The PVT_K1_ and PUB_K1_ prefixes are part of the standard key representation. But EOS and SwiftyEOS also support the old way:

print("private key: \(pk! .rawPrivateKey())") print("public key : \(pub! .rawPublicKey())") // private key: 5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL // public key : EOS4yDYdmcVcXxAxeNsUWRG7x9FKQE4HbJZdzgZFv1AYxk6oSVcLdCopy the code

Import existing keys:

let importedPk = try PrivateKey(keyString: "5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL")
let importedPub = PublicKey(privateKey: importedPk!)
Copy the code

Delimiters and prefixes:

let importedPk = try PrivateKey(keyString: "PVT_K1_5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL")
let importedPub = PublicKey(privateKey: importedPk!)
Copy the code

RPC API

EOSRPC.sharedInstance.chainInfo { (chainInfo, error) in if error == nil { print("Success: \(chainInfo!) ") } else { print("Error: \(error! .localizedDescription)") } }Copy the code

At present, we have some basic RPC endpoint, you can find it in the Sources/SwiftyEOS/Network.

IOS key store

We have SEWallet. Swift offline Wallet Manager for iOS.

SEWallet. Swift makes it easy to save AES encrypted key information to the file system. The default location is the application sandbox.

Multiple wallet management is not currently supported.

Create a new wallet on iOS

In the Objective – C:

[SEKeystoreService.sharedInstance newAccountWithPasscode:passcode succeed:^(SELocalAccount *account) {
} failed:^(NSError *error) {
        
}];
Copy the code

Retrieve the saved wallet

[SELocalAccount currentAccount];
Copy the code

If the wallet is not saved, it returns zero.

trading

Transaction behavior is not yet fully supported, but you can still try using the sample code in Main.swift.

When the full functionality is complete, relevant files will be provided.

  • Currency Transfer (2018.08.15)
  • Submission of General Transaction (2018.08.16)
  • Wallet Locking and Unlocking on devices (Offline) on iOS (2018.08.17)
  • Bet/Cancel Bet/Net value (2018.08.28)
  • Buy/Sell RAM (2018.08.28)
  • Creating an Account (2018.10)
  • Push transactions with Params List (2018.11.05)
  • Create/import key pairs using mnemonics

Money transfer

Var transfer = transfer () transfer.from = "agoodaccount" transfer.to = "gq3dinztgage" transfer.quantity = "1.0000 EOS" transfer.memo = "eureka" Currency.transferCurrency(transfer: transfer, code: "eosio.token", privateKey: importedPk! , completion: { (result, error) in if error ! = nil { if error is RPCErrorResponse { print("\((error as! RPCErrorResponse).errorDescription())") } else { print("other error: \(String(describing: error? .localizedDescription))") } } else { print("done.") } })Copy the code

Submission of general transaction

swift:

Let account = "raoji" let asset = "1.0000 EPRA" let data = "{\"hey\": {\"account\":\"" + account + "\", \"quantity\":\"" + asset + "\"}}" let abi = try! AbiJson(code: "prabox1", action: "withdraw", json: data) TransactionUtil.pushTransaction(abi: abi, account: account, privateKey: importedPk! , completion: { (result, error) in if error ! = nil { if (error! as NSError).code == RPCErrorResponse.ErrorCode { print("\(((error! as NSError).userInfo[RPCErrorResponse.ErrorKey] as! RPCErrorResponse).errorDescription())") } else { print("other error: \(String(describing: error? .localizedDescription))") } } else { print("Ok. Txid: \(result! .transactionId)") } })Copy the code

Objective – C:

AbiJson *your_abi;
[TransactionUtil pushTransactionWithAbi:your_abi
                                account:@"your_account"
                               pkString:@"your_private_key"
                             completion:^(TransactionResult *result, NSError *error) {
        
}];
Copy the code

Wallet lock and unlock on (offline) devices on iOS

We add lock and timedUnlock functions to SELocalAccount.

Cpu/net/ram operation

The resourceutil. swift file includes the ResourceUtil class, which includes the following methods:

  • stakeResource
  • unstakeResource
  • buyRam
  • sellRam

Stake resource:

ResourceUtil. StakeResource (account: "raoji", net: 1.0, CPU: 1.0, pkString: "5HsaHvRCPrjU3yhapB5rLRyuKHuFTsziidA13Uw6WnQTeJAG3t4", completion: { (result, error) in })Copy the code

mnemonics

Create a new key pair:

let (pk, pub, mn) = generateRandomKeyPair(enclave: .Secp256k1)
Copy the code

Import existing mnemonics:

let (pk, mn) = PrivateKey(enclave: .Secp256k1, mnemonicString: "your words here")
Copy the code

We have key management iOS Assistant apis and mnemonics in SEWallet. Swift. You can now create and import mnemonics using the SEKeystoreService class (or use the SEKeystore deeper API if you store them yourself) :

SEKeystoreService.sharedInstance.newAccountAndMnemonic(passcode: "your pass here", succeed: { (account, mnemonic) in

}) { (error) in

}
Copy the code

We’re using NSObject native classes for all of our top-level apis, so it’s the same for calling Objective-C without providing additional bridge files.

There is also help for iOS in the SEWallet. Swift file.

thank you

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Share some interactive online programming tutorials related to blockchain such as Ethereum, EOS and Bitcoin:

  • This course will help you get a quick start on the development of EOS blockchain decentralized applications. It covers core knowledge points such as EOS tool chain, account and wallet, issuing tokens, development and deployment of smart contracts, and interaction between codes and smart contracts. Finally, the development of a notepad DApp will be completed using all knowledge points comprehensively.
  • Java Ethereum development tutorial, mainly for Java and Android programmers for blockchain Ethereum development web3J details.
  • Python Ethereum is a detailed explanation of blockchain ethereum development by Python engineers using Web3.py.
  • PHP Ethereum, mainly introduces the use of PHP for smart contract development interaction, account creation, transactions, transfers, token development, filters and transactions and other content.
  • Ethereum introductory course, mainly introduces smart contract and DAPP application development, suitable for entry.
  • Ethereum development advanced tutorial, mainly introduces the use of Node.js, mongodb, blockchain, IPFS to achieve decentralized e-commerce DApp combat, suitable for advanced.
  • C# ethereum, mainly explains how to use C# based development. Net ethereum applications, including account management, status and transactions, smart contract development and interaction, filters and transactions, etc.
  • Java development tutorial COINS, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrate the currency support functions in Java code, such as creating address wallet, tectonic naked trading, management, Is a rare bitcoin development course for Java engineers.
  • PHP currency development tutorial, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrated the currency support functions in PHP code, such as creating address wallet, tectonic naked trading, management, This is a rare bitcoin development course for Php engineers.
  • Tendermint blockchain development in detail, this course is suitable for engineers who want to use Tendermint blockchain development, the course content includes the core concepts of tendermint application development model, such as ABCI interface, Merkle tree, multi-version state library, etc., also includes the rich practical code such as token issuance. It is the best choice for go language engineers to quickly start blockchain development.

Huizhi net original translation, reprint please indicate the source. Here is the original EOS open source framework SwiftyEOS written by Swift