3

I keep seeing the words offline forging and forged mentioned in various Tezos blogs and tutorials.

What is the purpose of forging?

2
  • 1
    can you provide examples where you see occurrences of these in the documentations?
    – Frank
    Apr 20, 2019 at 21:23
  • I have updated the question as forging is not mentioned in the official Tezos documentation but is mentioned by various Tezos blogs and tutorials. Apr 21, 2019 at 15:12

2 Answers 2

7

From a high level perspective the purpose of forging is to convert a set of inputs along with the kind of operation you wish to perform (eg: send money, to whom, from whom, how much etc) into a single sequence of bytes which will uniquely represent said operation.

Then the next step for this operation to enter the chain is to sign those bytes with your public key in order to prove publicly that you are the author of such bytes. The output of this will be another sequence of bytes.

Eventually those bytes will be actually pushed to the chain by an injection step.

All of those are performed by various clients/wallets behind the scene when you perform operations.

More details are available here

How to create a transaction

0

I will try to make you understand forging programmatically. I use nodejs and taquito package. Import necessary packages and provide signer to setProvider method.

const input = {
    "destination"       :   "Address of receiver",
    "amount"            :   "Amount in Tez",
    "source"            :   "Address of sender",
    "senderPrivateKey"  :   "Private key of sender"
}

Take the above object as input and estimate gas_limit, storage_limit and fee using the below function.

const estimate = async function(input){
    tezos.setProvider({ signer: new InMemorySigner(input.senderPrivateKey) });
    const estimated = await tezos.estimate
                .transfer({ to: input.destination, amount: input.amount })
                .then((est) => {
                    const estimated = {
                        "gasLimit"     :   est.gasLimit.toString(),
                        "storageLimit" :   est.storageLimit.toString(),
                        "suggestedFee" :   est.suggestedFeeMutez.toString()
                    }
                    return estimated;
                })
                .catch((error) => {
                    console.log("ERROR: Estimation failed!!",error);
                });
    return estimated;
}

Using the getCounter method from RpcClient class of '@taquito/rpc' package, get the counter and increment by 1 to build an operation object like below:

const forgeOp = async function(input){
    try{
        const counter = await getCounter(input.source); // getCounter is taquito rpc method
        const estimated = await estimate(input);
        const block = await getBlock(); // getBlock is taquito rpc method
        const branch = block.hash;
        const source = input.source;
        const destination = input.destination;
        const amount = input.amount;
        const gas_limit = estimated.gasLimit;
        const storage_limit = estimated.storageLimit;
        const fee = estimated.suggestedFee;

        const opObj = {
            "branch": branch,
            "contents": [
                { 
                    "kind": "transaction",
                    "fee": fee,
                    "gas_limit": gas_limit,
                    "storage_limit": storage_limit,
                    "amount": amount,
                    "destination": destination,
                    "parameters" : undefined,
                    "source": source,
                    "counter": counter  
                }
            ]
        };
        const forgedBytes = await forgeOperations(opObj); // forgeOperations is taquito rpc Method
        return forgedBytes;
    }catch(error){
        console.log({
            "Error"     :   "FORGE ERROR!",
            "message"   :   error
        });
    }  
}

By the below function, you will get forged bytes of the operation object.

const run = async function(input){
    const forgedBytes = await forgeOp(input);
    console.log("FORGED BYTES:\t", forgedBytes);
}
run(input);

Once you get forgedBytes, you should sign it using the taquito sign method and inject the resultant sBytes in order to broadcast the operation. Hence bakers will include your operation into a block successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.