[EOS]开源钱包iOS版本

SwiftyEOS

SwiftyEOS is an open-source framework for interacting with EOS, written in Swift.

Features

EOS key pairs generation

Private key import

Signature hash

Basic RPC API(chain/history) query client

Transaction(EOS token transfer)

Helper class for handling offline wallet on iOS

Encrypt/decrypt importing private key on iOS

How to use it

Copy the Libraries and Sources folders into your project, without main.swift.

Delete Sources/Utils/iOS if not targeted to iOS platform.

Add Libraries/include into Header Search Path.

Set Libraries/include/Bridging-Header.h as Objective-C Bridging Header. If you've got your own bridging header , copy all the import content in that file and paste into your own one.

Compile and pray.

Key Pairs Generation

SwiftyEOS now support secp256k1 key pairs.

There are bugs around secp256r1 key pairs generation but I cannot figure out why. Unit tests created from cleos create key --r1 wouldn't pass. You may not consider secp256r1 as an option since the cleos wallet command cannot import those keys, either.

Generate a random key pair:

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

Easy, right?

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

The PVT_K1_ and the PUB_K1_ prefixes are the parts of standard key presentations. But old styles are also supported in EOS system and SwiftyEOS:

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

Import existing key:

letimportedPk=tryPrivateKey(keyString:"5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL")letimportedPub=PublicKey(privateKey: importedPk!)

With delimiter and prefix:

letimportedPk=tryPrivateKey(keyString:"PVT_K1_5HxrYTdZX89zodtJhTzCk87MfNZAkiBRfFvSX8kacYjtwaDpTkL")letimportedPub=PublicKey(privateKey: importedPk!)

RPC API

EOSRPC.sharedInstance.chainInfo{ (chainInfo, error)iniferror==nil{print("Success:\(chainInfo!)")    }else{print("Error:\(error!.localizedDescription)")    }}

Currently we have some basic rpc endpoints, you can find it at Sources/SwiftyEOS/Network

iOS Key Store

We have helpers for iOS offline wallet management at SEWallet.swift.

With SEWallet.swift you can easily store AES encrypted key info into file system. The default location is app's sandbox Library.

Multiple wallets management is not supported yet.

Create new wallet on iOS

In Objective-C:

[SEKeystoreService.sharedInstancenewAccountWithPasscode:passcodesucceed:^(SELocalAccount *account) {}failed:^(NSError*error) {        }];

Retrieve saved wallet

[SELocalAccountcurrentAccount];

It will return nil if there's no saved wallet.

Transactions

Transaction behaviors are not fully supported yet, but you still can have a try with sample code at main.swift.

The related documents will be provided once the whole function is done.

Currency transfer (2018.08.15)

Push general transactions (2018.08.16)

On-device(offline) wallet lock & unlock on iOS (2018.08.17)

Stake/unstake cpu/net (2018.08.28)

Buy/sell ram (2018.08.28)

Create account (2018.10)

Push transaction with params list (2018.11.05)

Create/import key pairs with mnemonic

Transfer Currency

vartransfer=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)iniferror!=nil{iferrorisRPCErrorResponse {print("\((erroras!RPCErrorResponse).errorDescription())")        }else{print("other error:\(String(describing: error?.localizedDescription))")        }    }else{print("done.")    }})

Push General Transactions

In Swift:

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

And also in Objective-C:

AbiJson *your_abi;[TransactionUtilpushTransactionWithAbi:your_abiaccount:@"your_account"pkString:@"your_private_key"completion:^(TransactionResult *result,NSError*error) {        }];

On-device(offline) Wallet Lock & Unlock on iOS

We added lock and timedUnlock functions to SELocalAccount.

Cpu/net/ram operations

There's a ResourceUtil.swift file with ResourceUtil class including, within which are several methods:

stakeResource

unstakeResource

buyRam

sellRam

Stake resource:

ResourceUtil.stakeResource(account:"raoji",net:1.0,cpu:1.0,pkString:"5HsaHvRCPrjU3yhapB5rLRyuKHuFTsziidA13Uw6WnQTeJAG3t4",completion: { (result, error)in})

Mnemonic

Create a new key pair:

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

Import existing mnemonic:

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

And we do have iOS helper API with mnemonic for key manangement in SEWallet.swift. Creating and importing mnemonic is possible now, with SEKeystoreService class(or SEKeystore deep level API if you're store it on yourself):

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

We're using NSObject inhereiting class for all top level API, so calling in Objective-C is the same way without providing additional bridging files.

And in SEWallet.swift file there are helper methods for iOS, too.

Thanks

Inspired by:

https://github.com/EOSIO/eos

https://github.com/EOSIO/fc

https://github.com/bitcoin/bitcoin

https://github.com/bitcoin/libbase58

https://github.com/OracleChain/chainkit

https://github.com/OracleChain/PocketEOS-IOS

Built with:

https://github.com/kmackay/micro-ecc

https://github.com/Flight-School/AnyCodable

Contributers:

https://github.com/croath

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,529评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,015评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,409评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,385评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,387评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,466评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,880评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,528评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,727评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,528评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,602评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,302评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,873评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,890评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,132评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,777评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,310评论 2 342

推荐阅读更多精彩内容