Complementary Examples

Prerequisites

Before we get started we need to make sure that all of the required dependencies are installed. These dependencies are the Crypto SDK and Client SDK. You can head on over to their documentations to read more about them but for now we are only concerned with installing them to get up and running.

Open your project and execute the following commands to install both SDKs. Make sure that those complete without any errors. If you encounter any errors, please open an issue with as much information as you can provide so that our developers can have a look and get to the bottom of the issue.

1pip install arkecosystem-crypto
2pip install arkecosystem-client

Now that we’re setup and ready to go we’ll look into some examples for the most common tasks you’ll encounter when wanting to interact with the ARK Blockchain.

Creating and Broadcasting a Transfer

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.transfer import Transfer
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = Transfer(
20 recipientId='YOUR_RECIPIENT_ADDRESS',
21 amount=200000000,
22 vendorField="Hello World"
23)
24transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
25transaction.set_nonce(nonce)
26transaction.schnorr_sign('this is a top secret passphrase')
27 
28# Step 3: Broadcast the transaction
29try:
30 broadcastResponse = client.transactions.create([transaction.to_dict()])
31except ArkHTTPException as exception:
32 print(exception.response.json())
33 
34# Step 4: Log the response
35print(broadcastResponse)

Information

The vendorField is optional and limited to a length of 255 characters. It can be a good idea to add a vendor field to your transactions if you want to be able to easily track them in the future.

Creating and Broadcasting a Second Signature

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.second_signature_registration import SecondSignatureRegistration
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = SecondSignatureRegistration('this is a top secret second passphrase')
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Creating and Broadcasting a Delegate Registration

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.delegate_registration import DelegateRegistration
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = DelegateRegistration('johndoe')
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Creating and Broadcasting a Vote

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.vote import Vote
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = Vote('+0296893488d335ff818391da7c450cfeb7821a4eb535b15b95808ea733915fbfb1')
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Information

Note the plus prefix for the public key that is passed to the Vote constructor. This prefix denotes that this is a transaction to remove a vote from the given delegate.

Creating and Broadcasting an Unvote

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.vote import Vote
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = Vote('-0296893488d335ff818391da7c450cfeb7821a4eb535b15b95808ea733915fbfb1')
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Information

Note the minus prefix for the public key that is passed to the Vote constructor. This prefix denotes that this is a transaction to remove a vote from the given delegate.

Creating and Broadcasting a Multi Signature

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.multi_signature_registration import MultiSignatureRegistration
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = MultiSignatureRegistration()
20transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
21transaction.set_nonce(nonce)
22 
23transaction.set_sender_public_key('YOUR_SENDER_WALLET_PUBLIC_KEY')
24transaction.set_min(2)
25transaction.set_public_keys([
26 'participant_1_pk',
27 'participant_2_pk'
28])
29transaction.multi_sign('participant_1_passphrase', 0)
30transaction.multi_sign('participant_2_passphrase', 1)
31 
32transaction.schnorr_sign('this is a top secret passphrase')
33 
34# Step 3: Broadcast the transaction
35try:
36 broadcastResponse = client.transactions.create([transaction.to_dict()])
37except ArkHTTPException as exception:
38 print(exception.response.json())
39 
40# Step 4: Log the response
41print(broadcastResponse)

Creating and Broadcasting a IPFS

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.ipfs import IPFS
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = IPFS('QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB')
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Creating and Broadcasting a Multi Payment

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.multi_payment import MultiPayment
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = MultiPayment()
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.add_payment(1, 'D6Z26L69gdk9qYmTv5uzk3uGepigtHY4ax')
24transaction.add_payment(2, 'DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9')
25transaction.schnorr_sign('this is a top secret passphrase')
26 
27# Step 3: Broadcast the transaction
28try:
29 broadcastResponse = client.transactions.create([transaction.to_dict()])
30except ArkHTTPException as exception:
31 print(exception.response.json())
32 
33# Step 4: Log the response
34print(broadcastResponse)

Creating and Broadcasting a Delegate Resignation

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.delegate_resignation import DelegateResignation
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = DelegateResignation()
20 
21transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
22transaction.set_nonce(nonce)
23transaction.schnorr_sign('this is a top secret passphrase')
24 
25# Step 3: Broadcast the transaction
26try:
27 broadcastResponse = client.transactions.create([transaction.to_dict()])
28except ArkHTTPException as exception:
29 print(exception.response.json())
30 
31# Step 4: Log the response
32print(broadcastResponse)

Information

A delegate resignation has to be sent from the delegate wallet itself to verify its identity.

Creating and Broadcasting a HTLC Lock

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.htlc_lock import HtlcLock
7import hashlib
8 
9# Set your network
10set_network(Devnet)
11 
12# Configure our API client
13client = ArkClient('https://dexplorer.ark.io/api')
14 
15# Step 1: Retrieve the incremental nonce of the sender wallet
16senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
17nonce = int(senderWallet['data']['nonce']) + 1
18 
19# Secret hash is sha256 of the sha256 hash of the original message
20secret_hash = hashlib.sha256(hashlib.sha256('hello'.encode('utf-8')).hexdigest().encode('utf-8')).hexdigest()
21 
22# Step 2: Create the transaction
23transaction = HtlcLock(
24 recipient_id='YOUR_RECIPIENT_ADDRESS',
25 secret_hash=secret_hash,
26 expiration_type=1,
27 expiration_value=1504193605
28)
29 
30transaction.set_amount(5)
31transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
32transaction.set_nonce(nonce)
33transaction.schnorr_sign('this is a top secret passphrase')
34 
35# Step 3: Broadcast the transaction
36try:
37 broadcastResponse = client.transactions.create([transaction.to_dict()])
38except ArkHTTPException as exception:
39 print(exception.response.json())
40 
41# Step 4: Log the response
42print(broadcastResponse)

Creating and Broadcasting a HTLC Claim

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.htlc_claim import HtlcClaim
7import hashlib
8 
9# Set your network
10set_network(Devnet)
11 
12# Configure our API client
13client = ArkClient('https://dexplorer.ark.io/api')
14 
15# Step 1: Retrieve the incremental nonce of the sender wallet
16senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
17nonce = int(senderWallet['data']['nonce']) + 1
18 
19# Unlock secret is the sha256 hash of the original message
20unlock_secret = hashlib.sha256('hello'.encode('utf-8')).hexdigest()
21 
22# Step 2: Create the transaction
23transaction = HtlcClaim('LOCK_TRANSACTION_ID', unlock_secret)
24 
25transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
26transaction.set_nonce(nonce)
27transaction.schnorr_sign('this is a top secret passphrase')
28 
29# Step 3: Broadcast the transaction
30try:
31 broadcastResponse = client.transactions.create([transaction.to_dict()])
32except ArkHTTPException as exception:
33 print(exception.response.json())
34 
35# Step 4: Log the response
36print(broadcastResponse)

Information

The unlockSecret has to be a SHA256 hash of the plain text secret that you shared with the person that is allowed to claim the transaction.

Creating and Broadcasting a HTLC Refund

1from client import ArkClient
2from client.exceptions import ArkHTTPException
3from crypto.constants import TRANSACTION_TYPE_GROUP
4from crypto.configuration.network import set_network
5from crypto.networks.devnet import Devnet
6from crypto.transactions.builder.htlc_refund import HtlcRefund
7 
8# Set your network
9set_network(Devnet)
10 
11# Configure our API client
12client = ArkClient('https://dexplorer.ark.io/api')
13 
14# Step 1: Retrieve the incremental nonce of the sender wallet
15senderWallet = client.wallets.get('YOUR_SENDER_WALLET_ADDRESS')
16nonce = int(senderWallet['data']['nonce']) + 1
17 
18# Step 2: Create the transaction
19transaction = HtlcRefund(
20 lock_transaction_id='LOCK_TRANSACTION_ID'
21)
22 
23transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
24transaction.set_nonce(nonce)
25transaction.schnorr_sign('this is a top secret passphrase')
26 
27# Step 3: Broadcast the transaction
28try:
29 broadcastResponse = client.transactions.create([transaction.to_dict()])
30except ArkHTTPException as exception:
31 print(exception.response.json())
32 
33# Step 4: Log the response
34print(broadcastResponse)
Last updated 10 months ago
Edit Page
Share:
2022 © ARK.io | All Rights Reserved
An ARK.io Product