-
Notifications
You must be signed in to change notification settings - Fork 1
Grin Wallet Cocoa API Guide
RECENT CHANGES:
- (17 Aug 2019) Update for v1.0.4
- (26 Jul 2019) Creation for cocoa_grinwallet v1.0.x
These APIs support IOS ObjectC / Swift, and React-Native.
- Wallet Initialization
- Wallet Status Query
- Wallet Send and Receive
- File Transaction
- Wallet Management
You can find the APIs reference code GrinBridge.swift
For a pure Swift caller, it need a little bit adaption in function signature to remove all the @objc
and resolve/reject
parts, and change the return type from Void
to String
.
For example
this listen
API need to changed from:
@objc func listen(_ state: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt8 = 0
let cResult = grin_listen(state, &error)
handleCResult(error:error, cResult:cResult!, resolve: resolve, reject: reject)
}
to:
func listen(state: String) -> String {
var error: UInt8 = 0
let cResult = grin_listen(state, &error)
return handleCResult(error:error, cResult:cResult!)
}
A simple global replacement should work.
btw, a reference of the `handleResult` function here.
func handleCResult(error: UInt8, cResult: UnsafePointer<Int8>) -> Result<String, GrinWalletError> {
let result = String(cString: cResult)
cstr_free(cResult)
switch error {
case 0:
return .success(result)
case 2:
print("not validated!")
return .success(result)
default:
return .failure(GrinWalletError(code: Int(error), message: result))
}
}
If need Objective-C APIs, you can refer to GrinBridge.m
.
For a pure Objective-C caller, it need a little bit adaption in function signature to remove all the RCT_EXTERN_METHOD
and resolve/reject
parts, and add the return type as NSString
.
For example
this listen
API need to changed from:
RCT_EXTERN_METHOD(listen: (NSString*)state resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
to:
(NSString*)listen: (NSString*)state
A simple global replacement should work.
And to provide the React-Native APIs, both [GrinBridge.m
] and GrinCode-Bridging-Header.h
are needed, to open it for React-Native as NativeModules.
- Swift
func walletInit(state: String, password: String, is_12_phrases: Bool) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)walletInit:(NSString*)state password:(NSString*)password is_12_phrases:(BOOL)is_12_phrases
- React-Native call example
NativeModules.GrinBridge.walletInit(JSON.stringify(walletState), walletState.password, false)
- Swift
func walletPhrase(state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)walletPhrase:(NSString*)state
- React-Native call example
const wallet1Mnemonic = await NativeModules.GrinBridge.walletPhrase(JSON.stringify(walletState))
- Swift
func walletRecovery(state: String, phrase: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)walletRecovery:(NSString*)state phrase:(NSString*)phrase
- React-Native call example
NativeModules.GrinBridge.walletRecovery(JSON.stringify(walletState), mnemonic)
- Swift
func walletRestore(state: String, startIndex: UInt64, batchSize: UInt64) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)walletRestore:(NSString*)state startIndex:(uint64_t)startIndex batchSize:(uint64_t)batchSize
- React-Native call example
NativeModules.GrinBridge.walletRestore(JSON.stringify(walletState), startIndex, batchSize)
- Swift
func balance(state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)balance:(NSString*)state
- React-Native call example
NativeModules.GrinBridge.balance(JSON.stringify(walletState))
- Swift
func txsGet(state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txsGet:(NSString*)state
- React-Native call example
const data = await NativeModules.GrinBridge.txsGet(JSON.stringify(walletState))
- Swift
func txGet(_ state: String, txSlateId: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txGet:(NSString*)state txSlateId:(NSString*)txSlateId
- React-Native call example
const data = await NativeModules.GrinBridge.txGet(JSON.stringify(walletState), slateId)
- Swift
func outputsGet(state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)outputsGet:(NSString*)state
- React-Native call example
const data = await NativeModules.GrinBridge.outputsGet(JSON.stringify(walletState))
- Swift
func txSend(_ state: String, amount: UInt64, selectionStrategy: String, message: String, targetSlateVersion: Int64, dest:String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txSend:(NSString*)state amount:(uint64_t)amount selectionStrategy:(NSString*)selectionStrategy message:(NSString*)message targetSlateVersion:(int64_t)targetSlateVersion dest:(NSString*)dest
- React-Native call example
const slate = await NativeModules.GrinBridge.txSend(JSON.stringify(walletState),
10000000, 'smallest', 'a user message', -1, 'http://sga.grin.icu:13415')
2. Send by Grin Relay
Same API as above except the dest
parameter is a grin relay address string.
- React-Native call example
const slate2 = await NativeModules.GrinBridge.txSend(JSON.stringify(walletState),
10000000, 'smallest', 'a user message', -1, 'tn1-qgfaqdqy-vm8ryd2k6zfp6cm-359cs4gnudxhljm-d0v38yut4u9r7rg-93d4jp')
- Swift
func txCancel(state: String, txSlateId: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txCancel:(NSString*)state txSlateId:(NSString*)txSlateId
- React-Native call example
await NativeModules.GrinBridge.txCancel(JSON.stringify(walletState), slateId)
Because it's too difficult or even impractical for a common mobile phone to listen on a socket for receiving the Grin transactions, in this grin wallet cocoa APIs, we get rid of the socket listening and use the open source Grin Relay service for Grin transaction receiving.
So, we only listen on the wallet's Grin Relay address. To query the Grin Relay address of current wallet, please refer the address
API.
- Swift
func listen(_ state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)listen:(NSString*)state
- React-Native call example
NativeModules.GrinBridge.listen(JSON.stringify(walletState))
Note: a very complex and insecure procedure for file transaction, strongly not proposed to use! unless you can encapsulate it with a secure transmission between transaction parties.
Use in the payer side, the payer use this API to create a file transaction, and the returned slate file need to be sent back to the payee.
- Swift
func txCreate(state: String, amount: UInt64, selectionStrategy: String, message: String, targetSlateVersion: Int64) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txCreate:(NSString*)state amount:(uint64_t)amount selectionStrategy:(NSString*)selectionStrategy message:(NSString*)message targetSlateVersion:(int64_t)targetSlateVersion
- React-Native call example
await NativeModules.GrinBridge.txCreate(JSON.stringify(walletState), 10000000, 'smallest', 'a user message', -1)
Note: a very complex and insecure procedure for file transaction, strongly not proposed to use! unless you can encapsulate it with a secure transmission between transaction parties.
Use in the payee side, when the payee receive the payer's created file transaction, this txReceive
need to be called and the returned slate file need to be sent back to the payer.
- Swift
func txReceive(state: String, slateFilePath: String, message: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txReceive:(NSString*)state slateFilePath:(NSString*)slateFilePath message:(NSString*)message
- React-Native call example
await NativeModules.GrinBridge.txReceive(JSON.stringify(wallet3State), fileName, 'a receive message')
Note: a very complex and insecure procedure for file transaction, strongly not proposed to use! unless you can encapsulate it with a secure transmission between transaction parties.
Use in the payer side, when the payer receive the payee's signed file transaction, this txFinalize
need to be called and the returned slate file need to be post to the chain.
- Swift
func txFinalize(state: String, slateFilePath: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txFinalize:(NSString*)state slateFilePath:(NSString*)slateFilePath
- React-Native call example
await NativeModules.GrinBridge.txFinalize(JSON.stringify(walletState), fileName)
Note: a very complex and insecure procedure for file transaction, strongly not proposed to use! unless you can encapsulate it with a secure transmission between transaction parties.
As the last step of a file transaction, this txPost
is used to post the finalized slate to the chain, which will be mined by the miner (i.e. packaged into a block).
- Swift
func txPost(state: String, txSlateId: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)txPost:(NSString*)state txSlateId:(NSString*)txSlateId
- React-Native call example
await NativeModules.GrinBridge.txPost(JSON.stringify(walletState), slateId)
- Swift
func checkPassword(state:String, password: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)checkPassword:(NSString*)state password:(NSString*)password
- React-Native call example
NativeModules.GrinBridge.checkPassword(JSON.stringify(walletState), 'my-wallet-password')
2. Query Wallet Address (Grin Relay Bech32 Address)
- Swift
func myRelayAddress(_ state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)myRelayAddress:(NSString*)state
- React-Native call example
const address_string = await NativeModules.GrinBridge.myRelayAddress(JSON.stringify(walletState))
Listed above in the wallet initialization.
- Swift
func walletRepair(state: String, startIndex: UInt64, batchSize: UInt64, updateOutputs: Bool) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)walletRepair:(NSString*)state startIndex:(uint64_t)startIndex batchSize:(uint64_t)batchSize updateOutputs:(BOOL)updateOutputs
- React-Native call example
NativeModules.GrinBridge.walletRepair(JSON.stringify(result), startIndex, batchSize, updateOutputs)
- Swift
func height(state: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)height:(NSString*)state
- React-Native call example
NativeModules.GrinBridge.height(JSON.stringify(walletState))
- Swift
func changePassword(state:String, oldPassword: String, newPassword: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)changePassword:(NSString*)state oldPassword:(NSString*)oldPassword newPassword:(NSString*)newPassword
- React-Native call example:
NativeModules.GrinBridge.changePassword(JSON.stringify(walletState), oldPassword, newPassword)
- Swift
func relayAddressQuery(state: String, sixCode: String) -> Result<String, GrinWalletError>
- ObjectC
(NSString*)relayAddressQuery:(NSString*)state sixCode:(NSString*)sixCode
- React-Native call example
NativeModules.GrinBridge.relayAddressQuery(JSON.stringify(walletState), '5rq8yz')