项目作者: wombat-tech

项目描述 :
Wombat iOS SDK
高级语言: Ruby
项目地址: git://github.com/wombat-tech/wombat-sdk-ios.git
创建时间: 2019-09-23T15:07:13Z
项目社区:https://github.com/wombat-tech/wombat-sdk-ios

开源协议:MIT License

下载



Wombat

Swift 5
Objective-C

WombatAuth

iOS client SDK for DApps.

Supported Actions

EVM

  1. Authorize: Request the public address
  2. Personal Sign: Request a signature of the message with the personal_sign method
  3. Sign Typed Data: Request a signature of the typped data with the eth_signTypedData_v4 method
  4. Transaction: Request a transaction

EOSIO

  1. Authorize: Request the account details
  2. Transfer: Request a transfer of funds
  3. Push: Request a transaction
  4. Sign: Request a signature

Installation

Swift Package Manager

WombatAuth sdk version 3+ supports installation via Swift Package Manager.

In Xcode go to File -> Swift Packages -> Add Package Dependency

Enter the Wombat iOS SDK GitHub repository - https://github.com/wombat-tech/wombat-sdk-ios

Select the WombatAuth library

and you are ready to start using WombatAuth library.

Cocoapods

Add the following line to your Podfile and run pod install.

  1. pod 'WombatAuth'

Manual

  1. Download the latest version of the framework here.
  2. Add WombatAuth.xcframework into your Embedded Binaries.

Setup

Add the following 2 snippets into your Info.plist.

  1. <key>CFBundleURLTypes</key>
  2. <array>
  3. <dict>
  4. <key>CFBundleTypeRole</key>
  5. <string>Editor</string>
  6. <key>CFBundleURLSchemes</key>
  7. <array>
  8. <!-- URL scheme to use when returning from Wombat to your app -->
  9. <string>wombat.$(PRODUCT_BUNDLE_IDENTIFIER)</string>
  10. </array>
  11. </dict>
  12. </array>
  1. <key>LSApplicationQueriesSchemes</key>
  2. <array>
  3. <!-- URL scheme to use when launching Wombat from your app -->
  4. <string>wombat</string>
  5. </array>
If your project is written in Objective-C
  1. In project’s Build Settings set Always Embed Swift Standard Libraries to YES.
  2. Import the library by adding #import <WombatAuth/WombatAuth-Swift.h>.

Usage

Register your app

Make sure your app is registered prior to executing any requests.

  1. import WombatAuth
  2. class AppDelegate: UIResponder, UIApplicationDelegate {
  3. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  4. WMAuth.shared.registerApp(
  5. name: "Wombat SDK Sample",
  6. icon: URL(string: "https://myapp.com/icon")!,
  7. blockchain: .polygon
  8. )
  9. return true
  10. }
  11. }
  1. #import <WombatAuth/WombatAuth-Swift.h>
  2. [WMAuth.shared registerAppWithName: @"Wombat SDK Sample"
  3. icon: [NSURL URLWithString:@"https://assets.website-files.com/5cde8c951beecf3604688a58/5d120b2cba030f78d70c7236_Wombat_logo_transparent-p-500.png"]
  4. blockchain: WMBlockchain.wax];

Currently supported blockchains are

  • Ethereum
  • Polygon
  • BNB
  • HECO
  • Fantom
  • Avalanche
  • EOS
  • TELOS
  • WAX

Response handler

In order to receive results you need to register an url handler in the AppDelegate:application(_:open:options:).

  1. return WMAuth.shared.application(open: url) { result in
  2. switch result {
  3. case let .success(action, blockchain, data):
  4. switch action {
  5. case .authorize:
  6. let message: String
  7. if blockchain.isEvm {
  8. let address = data["address"] as! String
  9. message = "Public address is \(address)"
  10. } else {
  11. let accountName = data["accountName"] as! String
  12. let publicKey = data["publicKey"] as! String
  13. message = "Name: \(accountName)\nKey: \(publicKey)"
  14. }
  15. showAlert(title: "Auth", message: message)
  16. case .personalSignEVM:
  17. let signedMessage = data["signedMessage"] as! String
  18. showAlert(title: "Personal Sign", message: signedMessage)
  19. case .signTypedDataEVM:
  20. let signedMessage = data["signedMessage"] as! String
  21. showAlert(title: "Signed Typed Data", message: signedMessage)
  22. case .transactionEVM:
  23. let transactionId = data["transactionID"] as! String
  24. showAlert(title: "Transaction", message: transactionId)
  25. case .signEOSIO:
  26. let signedMessage = data["signature"] as! String
  27. showAlert(title: "Sign", message: signedMessage)
  28. case .transferEOSIO:
  29. let transactionId = data["transactionID"] as! String
  30. showAlert(title: "Transaction", message: transactionId)
  31. case .transactionEOSIO:
  32. let transactionId = data["transactionID"] as! String
  33. showAlert(title: "Transaction", message: transactionId)
  34. default:
  35. break
  36. }
  37. case let .error(action, blockchain, error):
  38. showAlert(title: "Error", message: error.localizedDescription)
  39. case let .userCancelled(action, blockchain):
  40. showAlert(title: "User cancelled")
  41. default:
  42. break
  43. }
  44. }
Objective-C
  1. return [WMAuth.shared openURL:url completionHandler:^(WMResultObj *result) {
  2. switch (result.type) {
  3. case WMResultTypeSuccess:
  4. switch (result.action) {
  5. case WMActionTypeAuthorize: {
  6. if (result.blockchain.isEvm) {
  7. NSString *address = [result.data valueForKey:@"address"];
  8. [self showAlertWithTitle:@"Auth" message:address];
  9. } else {
  10. NSString *accountName = [result.data valueForKey:@"accountName"];
  11. NSString *publicKey = [result.data valueForKey:@"publicKey"];
  12. NSString *alertMessage = [NSString stringWithFormat:@"Name: %@\nKey: %@", accountName, publicKey];
  13. [self showAlertWithTitle:@"Auth" message:alertMessage];
  14. }
  15. break;
  16. }
  17. case WMActionTypePushTransactionEVM: {
  18. NSString *transactionID = [result.data valueForKey:@"transactionID"];
  19. [self showAlertWithTitle:@"Push" message:transactionID];
  20. break;
  21. }
  22. case WMActionTypePersonalSignEVM: {
  23. NSString *transactionID = [result.data valueForKey:@"signedMessage"];
  24. [self showAlertWithTitle:@"Personal Sign" message:transactionID];
  25. break;
  26. }
  27. case WMActionTypeSignTypedDataEVM: {
  28. NSString *transactionID = [result.data valueForKey:@"signedMessage"];
  29. [self showAlertWithTitle:@"Sign Typed Data" message:transactionID];
  30. break;
  31. }
  32. case WMActionTypePushTransactionEOSIO: {
  33. NSString *transactionID = [result.data valueForKey:@"transactionID"];
  34. [self showAlertWithTitle:@"Push" message:transactionID];
  35. break;
  36. }
  37. case WMActionTypeSignEOSIO: {
  38. NSString *signature = [result.data valueForKey:@"signature"];
  39. [self showAlertWithTitle:@"Sign" message:signature];
  40. break;
  41. }
  42. case WMActionTypeTransferEOSIO: {
  43. NSString *transactionID = [result.data valueForKey:@"transactionID"];
  44. [self showAlertWithTitle:@"Transfer" message:transactionID];
  45. break;
  46. };
  47. case WMActionTypeUnknown: {
  48. [self showAlertWithTitle:@"Unknown" message:result.message];
  49. break;
  50. };
  51. }
  52. break;
  53. case WMResultTypeError:
  54. [self showAlertWithTitle:@"WMResultTypeError" message: result.message];
  55. break;
  56. case WMResultTypeUserCancelled:
  57. [self showAlertWithTitle:@"WMResultTypeUserCancelled" message:@"Cancelled by the user"];
  58. break;
  59. }
  60. }];
  61. }

Actions

Authorize

  1. try? WombatAuth.shared.requestAuthorization()
  1. NSError *err;
  2. [WMAuth.shared requestAuthorizationAndReturnError:&err];

EVM Personal Sign

  1. try? WMAuth.shared.personalSign(message: "Hello World!")
  1. NSError *err;
  2. [WMAuth.shared personalSignWithMessage:@"Hello World!" error:&err];

EVM Sign Typed Data

  1. let data = """
  2. {
  3. "types": {
  4. "EIP712Domain": [
  5. {"name": "name", "type": "string"},
  6. {"name": "version", "type": "string"},
  7. {"name": "chainId", "type": "uint256"},
  8. {"name": "verifyingContract", "type": "address"}
  9. ],
  10. "Person": [
  11. {"name": "name", "type": "string"},
  12. {"name": "wallet", "type": "bytes32"},
  13. {"name": "age", "type": "int256"},
  14. {"name": "paid", "type": "bool"}
  15. ]
  16. },
  17. "primaryType": "Person",
  18. "domain": {
  19. "name": "Person",
  20. "version": "1",
  21. "chainId": 1,
  22. "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
  23. },
  24. "message": {
  25. "name": "alice",
  26. "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
  27. "age": 40,
  28. "paid": true
  29. }
  30. }
  31. """.data(using: .utf8)!
  32. try? WMAuth.shared.signTypedData(data: data)

EVM Transaction

Transfer of the blockchains native currency e.g. MATIC on polygon
  1. let value = String(format:"%llx", 100_000_000_000_000_000)
  2. let sender = "0xc60Fa6D34C8A926E22791D7178F883Bd4cf2B312"
  3. let receiver = "0x7F4Ff6c65fB4a1211931b80d05062daDfB5480bD"
  4. try? WMAuth.shared.pushTransaction(
  5. WMEVMTransaction(
  6. from: sender,
  7. to: receiver,
  8. value: value,
  9. data: nil
  10. )
  11. )
  1. NSError *err;
  2. NSString *value = [NSString stringWithFormat: @"%lx", 100000000000000000];
  3. NSString *sender = @"0xc60Fa6D34C8A926E22791D7178F883Bd4cf2B312";
  4. NSString *receiver = @"0x7F4Ff6c65fB4a1211931b80d05062daDfB5480bD";
  5. [WMAuth.shared pushEVMTransaction:[[WMEVMTransaction alloc] initFrom:sender
  6. to:receiver
  7. value:value
  8. data:nil]
  9. :&err];
Invocation of smart contract on the registered blockchain.
  1. let sender = "0xc60Fa6D34C8A926E22791D7178F883Bd4cf2B312"
  2. let contract = "0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e"
  3. let data = "a9059cbb0000000000000000000000007f4ff6c65fb4a1211931b80d05062dadfb5480bd000000000000000000000000000000000000000000000000016345785d8a0000".hexadecimal
  4. try? WMAuth.shared.pushTransaction(
  5. WMEVMTransaction(
  6. from: sender,
  7. to: contract,
  8. value: nil,
  9. data: data
  10. )
  11. )
  1. NSString *sender = @"0xc60Fa6D34C8A926E22791D7178F883Bd4cf2B312";
  2. NSString *contract = @"0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e";
  3. NSData *data = [@"a9059cbb0000000000000000000000007f4ff6c65fb4a1211931b80d05062dadfb5480bd000000000000000000000000000000000000000000000000016345785d8a0000" stringToHexData];
  4. NSError *err;
  5. [WMAuth.shared pushEVMTransaction:[[WMEVMTransaction alloc] initFrom:sender
  6. to:contract
  7. value:nil
  8. data:data]
  9. :&err];

EOSIO Transfer

  1. let from = "{FROM_ACCOUNT_NAME}"
  2. let to = "{TO_ACCOUNT_NAME}"
  3. let transfer = WMTransfer(
  4. from: from,
  5. to: to,
  6. amount: 1,
  7. precision: 4,
  8. contract: "eosio.token",
  9. symbol: "EOS",
  10. memo: "Some memo"
  11. )
  12. WombatAuth.shared.requestTransfer(transfer)
  1. NSString *sender = @"{SENDER_ACCOUNT_NAME}";
  2. NSString *receiver = @"{RECEIVER_ACCOUNT_NAME}";
  3. WMEOSIOTransfer *transfer = [[WMEOSIOTransfer alloc] initFrom: sender
  4. to: receiver
  5. amount: 1
  6. precision: 4
  7. contract: @"eosio.token"
  8. symbol: @"EOS"
  9. memo: @"Some memo"];
  10. NSError *err;
  11. [WMAuth.shared requestEOSIOTransfer:transfer :&err];

EOSIO Push

  1. let transaction = WMTransaction(
  2. from: "account_name",
  3. actions: [
  4. .init(
  5. account: "eosio.token",
  6. name: "transfer",
  7. auth: [.init(actor: "account_name", permission: "owner")],
  8. data: [
  9. "from": "account_name",
  10. "to": "account_name_2",
  11. "quantity": "1.0000 EOS",
  12. "memo": "Here you go"
  13. ]
  14. )
  15. ]
  16. )
  17. WombatAuth.shared.pushTransaction(transaction)
  1. NSString *sender = @"{SENDER_ACCOUNT_NAME}";
  2. NSString *receiver = @"{RECEIVER_ACCOUNT_NAME}";
  3. WMEOSIOAuth *auth = [[WMEOSIOAuth alloc] initWithActor: sender permission: @"active"];
  4. NSArray<WMEOSIOAction *> *actions = @[[[WMEOSIOAction alloc] initWithAccount: @"eosio.token" name: @"transfer" auth: @[auth] data: @{
  5. @"from": sender,
  6. @"to": receiver,
  7. @"quantity": @"1 EOS",
  8. @"memo": @"Here you go"
  9. }]];
  10. WMEOSIOTransaction *transaction = [[WMEOSIOTransaction alloc] initFrom: @"aaaabbbbcccc" actions: actions];
  11. NSError *err;
  12. [WMAuth.shared pushEOSIOTransaction:transaction :&err];

EOSIO Sign

  1. WombatAuth.shared.requestSignature(account: "account_name", data: "Some data")
  1. NSError *err;
  2. [WMAuth.shared requestSignatureWithAccount: @"account" data:@"Hello World!" error: &err];