Skip to content

Transaction on Contentbox

gtygo edited this page Jun 4, 2019 · 2 revisions

Here is an introduction to sending a transaction in ContentBox and verifying whether the transaction is successful.

Prepare Accounts

In ContentBox, each address represents a unique account.

Prepare two accounts: an address to send tokens (the sending address, called "from") and an address to receive the tokens (the receiving address, called "to").

The Sender

In this guide,we will take b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz as a sender. We can use this address to generate transactions with other accounts. Of course, you can use your own wallet address when you created it.

Create account

./box wallet newaccount

//response
Please Input Your Passphrase
Created new account. Address:b1mi6ZCs3zpHfb72m141Be1jRQVf5LbMWuC

Get some coin for test

echo '{"addr":"b1mi6ZCs3zpHfb72m141Be1jRQVf5LbMWuC","amount":200}' | http POST  http://localhost:19190/v1/faucet/claim

Recipient

Create a new account to receive the tokens.

SHELL

$ ./box wallet newaccount

and you will input a Passphrase, Do not forget this passphrase.
 
RESPONSE
Created new account. Address:b1gFAdBjy8gu3vFRbg1qJEXtiV9xZTVTKx1

The Keystore file of the new wallet will be located in ../src/github.com/BOXFoundation/boxd/keyfile/

Start the nodes

./startnode 1

or

./box start --config=./.devconfig/.box-1.yaml

Interacting with Nodes

ContentBox provides developers with HTTP API,gRPC API with the running nodes.here,we will share how to send a transaction using HTTP API.

Check address

Validate address checks if the given address is valid

SHELL
./box ctl validateaddress b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz

RESPONSE
validateing address b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz
b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz  is a valid address

Check Account Balance

Fetch the balance of sender's accoutb1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz

echo '{"addrs":["b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz"]}'| http post localhost:19190/v1/tx/getbalance

RESPONSE

{
    "balances": [
        "949895"
    ],
    "code": 0,
    "message": "ok"
}

Check the recipient's account balance.

echo '{"addrs":["b1gFAdBjy8gu3vFRbg1qJEXtiV9xZTVTKx1"]}'| http post localhost:19190/v1/tx/getbalance

RESPONSE

{
    "balances": [
        "0"
    ],
    "code": 0,
    "message": "ok"
}

Send a transaction

sign & send

We sign the transaction by offline, and then send the signed transaction string out.

This method is very secure because you don't need to transfer your private key over the Internet.

First,Create an unsigned transaction

./box tx maketx b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz b1eS3j8bVa4epiBjvfvw8jnzsN167fjhP8a 5000 0

you will get some information like this.Please save the string corresponding to raw transaction and rawMsgs avatar

Then,sign the transaction to get the raw data.You need to enter the sequence string of the transaction and the originating address of the transaction.The process of signing is done locally.

./box tx signrawtx 12260a240a2037f7c2ab1b566c82c6d440e9c0019abc763f74c169672f6381684bb85ba1fbdf10011a1e08d00f121976a914748bf96a02f1ef2f7d74c2874f8a8345a1d66de388ac1a2108c8f7eadc03121976a91431ad4d5651e280d00ba4b2ec3d9588e9e782759888ac b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz

you will get some information like this.Please save the raw transaction string. avator

Finally, send the transaction out!The entered data is a locally signed transaction sequence.

./box tx sendrawtx 1291010a240a2037f7c2ab1b566c82c6d440e9c0019abc763f74c169672f6381684bb85ba1fbdf10011269463044022072491da68350af806bff6bd3a3449d9b4eceabee7dd6a6e98b48134dd1bf0128022009ec6eb184cab540edd124e9adc07f75e5fdd138e54cc57b6ae32302bfadb7852102f32aa1ffea2f31794949747b8655794a6fac1f2517c1e1d94bcd7de8a5eb034f1a1e08d00f121976a914748bf96a02f1ef2f7d74c2874f8a8345a1d66de388ac1a2108c8f7eadc03121976a91431ad4d5651e280d00ba4b2ec3d9588e9e782759888ac
//response
sendrawtx called
4d7f179d32fa80f64f9a7f893c7cdfa26e35dc8dcd19371120f863e605362da2

Congratulations when returning a bunch of transaction hashes, the transaction was sent successfully!

unlock & send

Sendfrom function sends an amount of box from a managed account to another account(This way is not safe): There are 3 internal steps during excution.

1 .Get valid utxos of the managed account

2 .Generate a transaction using part of the utxos and input params

3 .Unlock the managed account and sign the transaction

./box tx sendfrom b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz b1gFAdBjy8gu3vFRbg1qJEXtiV9xZTVTKx1 50000

RESPONSE

Tx Hash: 8c7bbc9127b3efa900468a136053726b357bb3c7a8673075185f057ae6f47421
Transaction
Version:	0
Vin:	Array
--0	:TxIn
--PrevOutPoint:	OutPoint
----Hash:	e627d72307ab202a59cc12244b7a958dc7b54f5ec206721535e4429bab5bc0b3
----Index:	0
--ScriptSig:	0x343633303434303232303165333738393338373765666265656431636465633865336137323234383031663034653130346632633166383266363933306234383366386337613765636530323230356230326533353661386538316238363737323565663331633832633261326564356230353338386438623464383462366531356361633861323633643862373231303266333261613166666561326633313739343934393734376238363535373934613666616331663235313763316531643934626364376465386135656230333466
--Sequence:	0
Vout:	Array
--0	:TxOut
--Value:	50000
--ScriptPubKey:	0x3736613931343838366436383732343938393531373439316539326334323061383230613965396435623431643638386163
--1	:TxOut
--Value:	449995
--ScriptPubKey:	0x3736613931343331616434643536353165323830643030626134623265633364393538386539653738323735393838386163
Data:	Nil
Magic:	0
LockTime:	0

Transaction Receipt

We'll get a txhash after sending a transaction successfully. The txhash value can be used to query the transaction status.

 echo '{"hash":"8c7bbc9127b3efa900468a136053726b357bb3c7a8673075185f057ae6f47421"}' | http POST http://127.0.0.1:19190/v1/tx/detail

RESPONSE

 {
    "block_height": 8154,
    "block_time": "1555404780",
    "code": 0,
    "detail": {
        "hash": "8c7bbc9127b3efa900468a136053726b357bb3c7a8673075185f057ae6f47421",
        "vin": [
            {
                "prev_out_detail": {
                    "addr": "b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz",
                    "script_disasm": "OP_DUP OP_HASH160 31ad4d5651e280d00ba4b2ec3d9588e9e7827598 OP_EQUALVERIFY OP_CHECKSIG",
                    "script_pub_key": "76a91431ad4d5651e280d00ba4b2ec3d9588e9e782759888ac",
                    "type": "pay_to_pubkey_hash",
                    "value": "500000"
                },
                "prev_out_point": "8iPmF8iah79B1b6QDZXasukF6ZQ7sEH1WrspeCYUd41XCn3ooFm",
                "script_sig": "46304402201e37893877efbeed1cdec8e3a7224801f04e104f2c1f82f6930b483f8c7a7ece02205b02e356a8e81b867725ef31c82c2a2ed5b05388d8b4d84b6e15cac8a263d8b72102f32aa1ffea2f31794949747b8655794a6fac1f2517c1e1d94bcd7de8a5eb034f",
                "sequence": 0
            }
        ],
        "vout": [
            {
                "addr": "b1gFAdBjy8gu3vFRbg1qJEXtiV9xZTVTKx1",
                "script_disasm": "OP_DUP OP_HASH160 886d68724989517491e92c420a820a9e9d5b41d6 OP_EQUALVERIFY OP_CHECKSIG",
                "script_pub_key": "76a914886d68724989517491e92c420a820a9e9d5b41d688ac",
                "type": "pay_to_pubkey_hash",
                "value": "50000"
            },
            {
                "addr": "b1YLUNwJD124sv9piRvkqcmfcujTZtHhHSz",
                "script_disasm": "OP_DUP OP_HASH160 31ad4d5651e280d00ba4b2ec3d9588e9e7827598 OP_EQUALVERIFY OP_CHECKSIG",
                "script_pub_key": "76a91431ad4d5651e280d00ba4b2ec3d9588e9e782759888ac",
                "type": "pay_to_pubkey_hash",
                "value": "449995"
            }
        ]
    },
    "message": "",
    "status": "onchain",
    "version": 0
}


The Codefields may be 0,-1

0 : Successful. It means It means the transaction has been submitted on chain and its execution succeeded.

-1: Failed. It means the transaction has been submitted on chain but its execution failed.

Double Check

Let's double check the recipient's balance.



echo '{"addrs":["b1gFAdBjy8gu3vFRbg1qJEXtiV9xZTVTKx1"]}'| http post localhost:19190/v1/tx/getbalance

RESPONSE

{
    "balances": [
        "50000"
    ],
    "code": 0,
    "message": "ok"
}

Here you should see a balance that is the total of all the successful transfers that you have executed.

Clone this wiki locally