Developers
  • SWT PROTOCOL
    • Introduction
    • Accounts
    • Transactions
    • Fees
    • Smart Contracts
  • GETTING STARTED
    • Network Node
      • Introduction
      • STEP 1:RENTING A LINUX VPS
      • STEP 2:INSTALLATION OF PUTTY
      • STEP 3:INSTALLATION OF FILEZILLA
      • STEP 4:VPS CONNECTION AND LINUX CONFIGURATION WITH PUTTY (SSH)
      • STEP 5:HOW TO SECURE YOUR VPS
      • STEP 6:USING TMUX THROUGH PUTTY
      • STEP 7:UPLOAD OF THE SWT SOFTWARE WITH FILEZILLA
      • STEP 8:EXTRACT THE SWT SOFTWARE FOLDER
      • STEP 9:RUNNING THE SWT NODE THROUGH TMUX
      • STEP 10:RUNNING THE MONITORING TOOLS
  • API REFERENCE
    • Apache Thrift API
      • Transactions
      • Blocks
      • Smart contract
      • Tokens
      • Wallets
      • Sync info
      • Data structures
    • REST API
      • GetBlocks()
      • GetNodeInfo()
      • GetBalance()
      • GetWalletInfo()
      • GetTokenBalance()
      • GetTransactionsByWallet()
      • GetTransactionInfo()
      • GetEstimatedFee()
      • GetContractByAddress()
      • GetContractFromTransaction()
      • GetContractMethods()
      • ContractValidation()
      • ContractCall()
      • TransactionPack()
      • TransactionExec()
  • SMART CONTRACTS
    • Creating "Hello-world" Smart Contract
    • Smart Contract Methods
    • Smart Contract Standarts
      • Token Smart Contract
      • Escrow Smart Contract
      • Stable coin Token
  • HOW TO REST API
    • Introduction
    • Retrieve a balance from the blockchain
    • Request a specific transaction from the blockchain
    • Sending Transactions to the SWT Blockchain
    • Validating and deploying a Smart Contract with REST API
Powered by GitBook
On this page
  • Introduction
  • Dependencies Python
  • Signing a Transaction
  1. HOW TO REST API

Sending Transactions to the SWT Blockchain

Introduction

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:

  1. Use the API to generate a correct set of transaction bytes from transaction data (transaction/pack)

  2. 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

  1. pip install base58check

  2. pip install ed25519

Key generation

You should execute the following code to generate the keys:

import base58check
import ed25519

priv_key, pub_key = ed25519.create_keypair()
priv_key_b58 = base58check.b58encode(priv_key.to_bytes())
pub_key_b58 = base58check.b58encode(pub_key.to_bytes())
print(f"Public Key: {pub_key_b58}")
print(f"Private Key: {priv_key_b58}")

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.

PreviousRequest a specific transaction from the blockchainNextValidating and deploying a Smart Contract with REST API

Last updated 1 year ago

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.

Execute()
Execute()