Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) #1506 - additional CreateTransaction documentation #1600

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 68 additions & 34 deletions docs/api/zilliqa/createtransaction.doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,82 @@ Create a new Transaction object and send it to the network to be processed.

| Parameter | Type | Required | Description |
| ----------- | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `version` | number | Required | The decimal conversion of the bitwise concatenation of `CHAIN_ID` and `MSG_VERSION` parameters. <br/><br/> **-** For mainnet, it is `65537`. <br/> **-** For Developer testnet, it is `21823489`. |
| `nonce` | number | Required | A transaction counter in each account. This prevents replay attacks where a transaction sending eg. 20 coins from A to B can be replayed by B over and over to continually drain A's balance. <br/><br/> It's value should be `Current account nonce + 1`. |
| `toAddr` | string | Required | Recipient's account address. This is represented as a `String`. <br/><br/> **NOTE:** This address has to be checksummed for every 6th bit, but the "0x" prefix is optional. <br/><br/> For deploying new contracts, set this to `"0000000000000000000000000000000000000000"`. |
| `version` | number | Required | `(MSG_VERSION & 0xffff) | (chain_id << 16)`. `MSG_VERSION` is 1. <br/><br/> **-** For mainnet, it is `65537`. <br/> **-** For Developer testnet, it is `21823489`. |
| `nonce` | number | Required | Transaction nonce. The value should be `current account nonce + 1`. Zilliqa-native nonces start at 1. |
| `toAddr` | string | Required | Recipient's account address. For Zilliqa 2, this need not be checksummed and can be either a hex address (`4BAF5faDA8e5Db92C3d3242618c5B47133AE003C`) with or without a leading `0x` and with or without a checksum, or a bech32 address (`zil1fwh4ltdguhde9s7nysnp33d5wye6uqpugufkz7`). For deploying new contracts, set this to `"0000000000000000000000000000000000000000"`. |
| `amount` | string | Required | Transaction amount to be sent to the recipent's address. This is measured in the smallest price unit **Qa** (or 10^-12 **Zil**) in Zilliqa. |
| `pubKey` | string | Required | Sender's public key of 33 bytes. |
| `pubKey` | string | Required | Sender's public key of 33 bytes, in hex, without leading `0x`. |
| `gasPrice` | string | Required | An amount that a sender is willing to pay per unit of gas for processing this transaction. This is measured in the smallest price unit **Qa** (or 10^-12 **Zil**) in Zilliqa. |
| `gasLimit` | string | Required | The amount of gas units that is needed to be process this transaction. |
| `code` | string | Optional | The smart contract source code. This is present only when deploying a new contract. |
| `gasLimit` | string | Required | Gas limit, in gas units. |
| `code` | string | Optional | When deploying a new smart contract, the code for the contract. Otherwise, empty or absent. |
| `data` | string | Optional | `String`-ified JSON object specifying the transition parameters to be passed to a specified smart contract. <br/><br/> - When creating a contract, this JSON object contains the **init** parameters. <br/> - When calling a contract, this JSON object contains the **msg** parameters. <br/><br/> _For more information on the Scilla interpreter, please visit the [documentation](https://scilla.readthedocs.io/en/latest/interface.html)._ |
| `signature` | string | Required | An **EC-Schnorr** signature of 64 bytes of the entire Transaction object as stipulated above. |
| `priority` | boolean | Optional | A flag for this transaction to be processed by the DS committee. Now ignored. |
| `signature` | string | Required | A hex-encoded, no prefixing `0x`, **EC-Schnorr** signature of 64 bytes of the entire Transaction object; see below. |

Additional fields may be specified, but will be ignored.

## Signature computation

To compute the signature of a transaction, we first encode the transaction as a protobuf using the declaration:

```
message ProtoTransactionCoreInfo
{
uint32 version = 1;
oneof oneof2 { uint64 nonce = 2; }
bytes toaddr = 3;
ByteArray senderpubkey = 4;
ByteArray amount = 5;
ByteArray gasprice = 6;
uint64 gaslimit = 7;
oneof oneof8 { bytes code = 8; }
oneof oneof9 { bytes data = 9; }
}
```

Byte arrays are stored big endian. Take the encoding of this structure
and call it `msgBytes`. Examples of protobuf encoding can be obtained
from the Zilliqa rust (`zilliqa-rs`), golang (`gozilliqa-sdk`) and Javascript (`zilliqa-js`) SDKs.

Now take the public key as bytes, `publicKeyBytes`.

Invent a random `k`, and in `secp256k1`, with `G` the generator of the group and `N` its modulus and using '.' for concatenation and '*' for multiplication, compute:

```
Q = compress( k * G )
r = SHA256( Q . publicKeyBytes . msgBytes ) mod N
s = k - (r * privateKey)
```

Now represent `r` and `s` as big-endian 0-padded 32-byte byte arrays, and concatenate them - `r . s` - to form an EC-Schnorr signature for the transaction, encode it in hex with no leading `0x`, and put it in the `signature` field eg. `9fe2d73db6cc4635c54dfdeb6c6965ed14a172ac5ba4dc77f9bdfe230394d62b47c8c5702cb757b460b2fc407090ed2c1d6732855ac891fea46ca3e86ab6ec4a`.

# Curl

With private key `0x2`:

```shell
curl -d '{
"id": "1",
"jsonrpc": "2.0",
"method": "CreateTransaction",
"params": [{
"version": 65537,
"nonce": 1,
"toAddr": "0x4BAF5faDA8e5Db92C3d3242618c5B47133AE003C",
"amount": "1000000000000",
"pubKey": "0205273e54f262f8717a687250591dcfb5755b8ce4e3bd340c7abefd0de1276574",
"gasPrice": "2000000000",
"gasLimit": "50",
"code": "",
"data": "",
"signature": "29ad673848dcd7f5168f205f7a9fcd1e8109408e6c4d7d03e4e869317b9067e636b216a32314dd37176c35d51f9d4c24e0e519ba80e66206457c83c9029a490d",
"priority": false
}]
}' -H "Content-Type: application/json" -X POST "{{_api_url}}"
curl -d '{
"id": "1",
"jsonrpc": "2.0",
"method": "CreateTransaction",
"params": [
{
"amount": "10000000",
"code": "",
"data": "",
"gasLimit": "50000",
"gasPrice": "2000000016",
"nonce": 4,
"priority": false,
"pubKey": "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5",
"signature": "9fe2d73db6cc4635c54dfdeb6c6965ed14a172ac5ba4dc77f9bdfe230394d62b47c8c5702cb757b460b2fc407090ed2c1d6732855ac891fea46ca3e86ab6ec4a",
"toAddr": "4BAF5faDA8e5Db92C3d3242618c5B47133AE003C",
"version": 45875201
}
]
}' -H "Content-Type: application/json" -X POST "{{ _api_url }}"
```


# NodeJs

```js
Expand Down Expand Up @@ -163,13 +203,7 @@ func SendTransaction() {
"id": "1",
"jsonrpc": "2.0",
"result": {
"Info": "Non-contract txn, sent to shard",
/*
Other possible Info:
Contract Creation txn, sent to shard
Contract Txn, Shards Match of the sender and reciever
Contract Txn, Sent To Ds
*/
"Info": "Txn processed",
"TranID": "2d1eea871d8845472e98dbe9b7a7d788fbcce226f52e4216612592167b89042c"
}
}
Expand All @@ -182,5 +216,5 @@ func SendTransaction() {
| `id` | string | Required | `"1"` |
| `jsonrpc` | string | Required | `"2.0"` |
| `method` | string | Required | `"CreateTransaction"` |
| `params` | N/A | Required | See table below for the Transaction parameters required: |
| `params` | N/A | Required | See table above for the Transaction parameters required: |