For illustration purposes, we’ll send 0.1 SWT on the MainNet network from wallet A to wallet B.
An algorithm ED25519 is used to generate a transaction signature. For security reasons, the signing process occurs on the client-side to avoid transmitting the private key.
There are various libraries for different programming languages that allow generating a key pair from the private and public keys, validating the digital signature, etc.
We will illustrate how to send a transaction to the blockchain with Python 3.
We will use libraries “ed25519” and “base58check” to work with keys. The latter is used to display the keys in a human-readable base58 format. You can install the libraries with pip (or pip3):
For JS : tweetnacl and bs58 can be used.
You should do the following actions to send a transaction (transfer SWT or call a smart contract method) in the network:
Use the API to generate a correct set of transaction bytes from transaction data (transaction/pack)
From transactions bytes received from the Rest API on step 1, generate a digital signature with the private key
Dependencies Python
These packages need to be installed
pip install base58check
pip install ed25519
Key generation
You should execute the following code to generate the keys:
You can execute this code by copying it line by line to the Python IDLE interpreter. As a result, you will get public and private keys in the form they are transferred to the web-services, like explorer.swttoken.com.
Creating/Pack a transaction
Warning
The wallet must contain some SWT before any transactions can be performed on it. It is best practice to check the wallet balance prior to initiating a transaction using the GetTransactionInfoREST request described above.
For illustration purposes, we’ll send 0.1 SWT on the MainNet network from wallet A to wallet B. We will use the “requests” library to send the REST API requests and the “JSON” library to work with the data.
import requests
import json
# URL REST, “pack” function to generate a set of transaction bytes
url = 'http://176.113.80.7:62000/api/transaction/pack'
# Specify necessary headers
headers = {
'Content-type': 'application/json'
, 'Accept': 'application/json'
, 'Content-Encoding': 'utf-8'
}
# Generate data for the request
data = {
"authKey": "",
, "MethodApi" : "TransferCS"
, "PublicKey":"76sWqAktXoEVMiDXGgSYMMyAn23nGqCB5TYYTesWsQvV"
, "ReceiverPublicKey":"G2GeLfwjg6XuvoWnZ7ssx9EPkEBqbYL3mw3fusgpzoBk"
, "NetworkAlias":"Mainnet"
, "Amount":0.1
, "Fee":0
, "UserData":""
}
answer = requests.post(url, data=json.dumps(data), headers=headers)
resp_data = json.loads(answer.content.decode())
transactionpack_b58 = resp_data["dataResponse"]["transactionPackagedStr"]
print(transactionpack_b58)
Signing a Transaction
import requests
import json
import base58check
import ed25519
url = 'http://176.113.80.7:62000/api/transaction/pack'
headers = {
'Content-type': 'application/json'
, 'Accept': 'application/json'
, 'Content-Encoding': 'utf-8'
}
data = {
"authKey": ""
, "MethodApi" : "TransferCS"
, "PublicKey":"AU8iyCcpwRehfsMv2myrykxWjAsxyrpQbPTaymAxjDYF"
, "ReceiverPublicKey":"G2GeLfwjg6XuvoWnZ7ssx9EPkEBqbYL3mw3fusgpzoBk"
, "NetworkAlias":"Mainnet"
, "Amount":0.1
, "Fee":0.1
, "UserData":""
}
answer = requests.post(url, data=json.dumps(data), headers=headers)
resp_data = json.loads(answer.content.decode())
transactionpack_b58 = resp_data["dataResponse"]["transactionPackagedStr"]
print(resp_data)
# Private key for signature
PrivKey = '2SeqBtsQpoSD7DJQpmY6ZnfDosg9VGndoaPSMSh8hrTALySWPEXxQKMrnAVy79U2GxWNAXm6dnmzYdNUToJpLzFm'
# Decode keys and transaction bytes from base58
keydata = base58check.b58decode(PrivKey)
transactionpack = base58check.b58decode(transactionpack_b58)
# Create object ed25519
signing_key = ed25519.SigningKey(keydata)
# Get digital signature for “transactionpack”
sign = signing_key.sign(transactionpack)
# Encode the resulting signature in base58
sign_b58 = base58check.b58encode(sign).decode()
# New URL according to the documentation
url = 'http://176.113.80.7:62000/api/transaction/execute'
data = {
"authKey": ""
, "MethodApi" : "TransferCS"
, "PublicKey":"AU8iyCcpwRehfsMv2myrykxWjAsxyrpQbPTaymAxjDYF"
, "ReceiverPublicKey":"G2GeLfwjg6XuvoWnZ7ssx9EPkEBqbYL3mw3fusgpzoBk"
, "NetworkAlias":"Mainnet"
, "Amount":0.1
, "Fee":0.1
, "UserData":""
, "TransactionSignature" : sign_b58
}
answer = requests.post(url, data=json.dumps(data), headers=headers)
resp_data = json.loads(answer.content.decode())
# Print to the console server response
print(resp_data)
Info
The goal of this example is to illustrate REST capabilities to connect with the SWT blockchain. By using the documentation from Developer’s Portal it’s possible to perform various functions ranging from retrieving the information about transactions to the deploying or executing smart contracts. For these purposes, the same mechanisms are used as described above.
The code above allows you to retrieve from REST a properly generated set of transaction bytes and send it again using “” function
For illustration purposes, we’ll sign a transaction with the private key by uploading it from the base58 format line, and add the resulting signature to the “”. Below is the full code with comments.