Giter Site home page Giter Site logo

How to sign offline? about swiftyeos HOT 17 CLOSED

KiyoCao avatar KiyoCao commented on May 27, 2024
How to sign offline?

from swiftyeos.

Comments (17)

croath avatar croath commented on May 27, 2024

Hi @MaoxinCao , did you mean signing transaction? All transactions are signed offline with SwiftyEOS. Try to make a token transfer like the README file described: Using Currency.transferCurrency.

But some params are needed to be fetched from an eos endpoint (block prefix and/or block number).

from swiftyeos.

croath avatar croath commented on May 27, 2024

Hi @MaoxinCao , did you solve the problem?

from swiftyeos.

zhenghongchuan avatar zhenghongchuan commented on May 27, 2024

I have the same question..

from swiftyeos.

zhenghongchuan avatar zhenghongchuan commented on May 27, 2024

how to sign to like this:

{
  "signatures": [
    "SIG_K1_KdRNkKSDNFUb7gU45UZcySykeGRGp4ESzEZj4pyJwWsuDVkRcHjREViyF1qQmci8981zib3zGebD64YAXgsNQkzAa7jftK"
  ],
  "compression": "none",
  "packed_context_free_data": "",
  "packed_trx": "a4f38c5b390282247889000000000100a6823403ea3055000000572d3ccdcd01104260d33d364dc300000000a8ed323241104260d33d364dc3204260d33d364dc31027000000000000044a554e474c45002054657374206279207368616e676a696e67202e2020323031383039303330303200"
}

from swiftyeos.

croath avatar croath commented on May 27, 2024

@zhenghongchuan it's a signed transaction, right?

I'll make it clear by posting an example. I have a transaction to make, it's a token-transfer transaction:

  1. Account agoodaccount wants to transfer 1 EOS to account gq3dinztgage. The first step is to have a transaction action like this:
{  
         "account":"eosio.token",
         "name":"transfer",
         "authorization":[  
            {  
               "actor":"agoodaccount",
               "permission":"active"
            }
         ],
         "data":{  
            "from":"agoodaccount",
            "to":"gq3dinztgage",
            "quantity":"1.0000 EOS"
         }
      }
  1. With the raw transaction action, the next step is to pack it, by converting this key-value based structure(json) to hex representation with the contract's ABI. After packing, the whole trasaction will be something like you've posted:

a4f38c5b390282247889000000000100a6823403ea3055000000572d3ccdcd01104260d33d364dc300000000a8ed323241104260d33d364dc3204260d33d364dc31027000000000000044a554e474c45002054657374206279207368616e676a696e67202e2020323031383039303330303200

(It's the string I just copied from your code.

  1. After that, the final thing is signing. Using EOS's standard signing method, and your private key, to sign this hex string to a signature:

SIG_K1_KdRNkKSDNFUb7gU45UZcySykeGRGp4ESzEZj4pyJwWsuDVkRcHjREViyF1qQmci8981zib3zGebD64YAXgsNQkzAa7jftK

  1. Then we got the result:
{
  "signatures": [
    "SIG_K1_KdRNkKSDNFUb7gU45UZcySykeGRGp4ESzEZj4pyJwWsuDVkRcHjREViyF1qQmci8981zib3zGebD64YAXgsNQkzAa7jftK"
  ],
  "compression": "none",
  "packed_context_free_data": "",
  "packed_trx": "a4f38c5b390282247889000000000100a6823403ea3055000000572d3ccdcd01104260d33d364dc300000000a8ed323241104260d33d364dc3204260d33d364dc31027000000000000044a554e474c45002054657374206279207368616e676a696e67202e2020323031383039303330303200"
}

The packed transaction, signature, no compression by default, and no context free data. It's ready. Then we can ship this signed transaction http body to one of the EOS RPC endpoint using RPC push_transaction API:

POST http://yourdomainandport/v1/chain/push_transaction

That's all.

With SwiftyEOS, it's much easier to make it happen because you don't need to care about the step 2 and step 3:

var transfer = Transfer()
transfer.from = "agoodaccount"
transfer.to = "gq3dinztgage"
transfer.quantity = "1.0000 EOS"
transfer.memo = "eureka"

Currency.transferCurrency(transfer: transfer, 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)")
    }
})

You can also find the code snippet in the project readme file. It's written in Swift but could be used with Objective-C, too.

from swiftyeos.

croath avatar croath commented on May 27, 2024

The crypto signing progress itself is offline. But you can't make the whole process totally offline, because we need to fetch some chain block data and put it together with our transaction, to make a signature.

BTW it's safe to get the abi_json data from an RPC endpoint. Though we could make this step "offline" for the contract we know, it's dangerous because the contract may change anytime.

from swiftyeos.

KiyoCao avatar KiyoCao commented on May 27, 2024

@croath Thanks Reply.

I am currently doing is multi-signature, so I need to sign offline on multiple terminals. But the signature in SwiftyEOS seems to be put into the transaction.

from swiftyeos.

zhenghongchuan avatar zhenghongchuan commented on May 27, 2024

@croath How can I get the privateKey in my currentAccount?

from swiftyeos.

croath avatar croath commented on May 27, 2024

@MaoxinCao Sorry that multi-sig is not supported yet.

But I'm sure I could provide a low-level method just for calculating signatures for a given context. Will be ready in next versions.

from swiftyeos.

croath avatar croath commented on May 27, 2024

@zhenghongchuan With currentAccount there's no need to take the privateKey, just use SELocalAccount's pushTransaction. Unlock with a password is a safer way in iOS.

from swiftyeos.

zhenghongchuan avatar zhenghongchuan commented on May 27, 2024

@croath The point is that can I get the sign json by my currentAccount, and how to do it. And can I create wallet by a account name? Thanks.

from swiftyeos.

KiyoCao avatar KiyoCao commented on May 27, 2024
let abi = try AbiJson(code: "currency", action: "transfer", json: jsonString) 

EOSRPC.sharedInstance.chainInfo {  (chainInfo, error) in
   EOSRPC.sharedInstance.getBlock(blockNumOrId: "\(chainInfo!.lastIrreversibleBlockNum)" as AnyObject, completion: { (blockInfo, error) in
         EOSRPC.sharedInstance.abiJsonToBin(abi: abi, completion: { (bin, error) in
                                                
                    })
                })
            } 
How to create AbiJson object?  I wrote this, bin is always nil. 

from swiftyeos.

croath avatar croath commented on May 27, 2024

@zhenghongchuan

  1. Yes and no. There's way you can get the final signed json yourself, read the source file and find the way I do in Transaction.swift.

  2. No. A wallet can not be created by just a name. A private key associated with that account is needed.

from swiftyeos.

croath avatar croath commented on May 27, 2024

@MaoxinCao Then what the error is?

And are you sure the code you wanna use is currency?

from swiftyeos.

zhenghongchuan avatar zhenghongchuan commented on May 27, 2024

@croath Thank you!

from swiftyeos.

KiyoCao avatar KiyoCao commented on May 27, 2024

@croath It's work...

let abi = try AbiJson(code: "eosio.token", action: "transfer", json: jsonString)

from swiftyeos.

croath avatar croath commented on May 27, 2024

🎉

from swiftyeos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.