API Documentation
Identity
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase |
Return Value
This function returns an address derived from the given passphrase as a string.
from crypto.identity.address import Address
address = Address.from_passphrase("super secret passphrase")
>> '0x2289577F3784788784226Eb41E9B0ca9705C7C37'
Type | Name | Required | Description |
---|---|---|---|
str | public_key | Yes | Public key |
Return Value
This function returns an address derived from the given public key as a string.
from crypto.identity.address import Address
address = Address.from_public_key("023efc1da7f315f3c533a4080e491f32cd4219731cef008976c3876539e1f192d3")
>> '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22'
Type | Name | Required | Description |
---|---|---|---|
str | private_key | Yes | Address in hexadecimal format |
Return Value
This function returns an address derived from the given private key as a string.
from crypto.identity.address import Address
address = Address.from_private_key("50829dd3b7ffbe2df401d730b5e60cea7520ba3f3a18e5b1490707667fb43fae")
>> '0x1E6747BEAa5B4076a6A98D735DF8c35a70D18Bdd'
Type | Name | Required | Description |
---|---|---|---|
str | address | Yes | Address to validate |
Return Value
This method returns a boolean indicating the validity of the address.
from crypto.identity.address import Address
is_valid = Address.validate('0x2289577F3784788784226Eb41E9B0ca9705C7C37');
>> True
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase |
Return Value
This method returns a PrivateKey object derived from the given passphrase.
from crypto.identity.private_key import PrivateKey
private_key = PrivateKey.from_passphrase("super secret passphrase")
>> PrivateKey
Type | Name | Required | Description |
---|---|---|---|
str | private_key | Yes | Hexadecimal representation of the private key |
Return Value
This method returns a PrivateKey object derived from a hex string.
from crypto.identity.private_key import PrivateKey
private_key = PrivateKey.from_hex("d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712")
>> PrivateKey
Type | Name | Required | Description |
---|---|---|---|
str | wif | Yes | Hexadecimal representation of the WIF string |
Return Value
This method returns a PrivateKey object derived from a WIF string.
from crypto.identity.private_key import PrivateKey
private_key = PrivateKey.from_wif("UZYnRZ8qpeQWTLeCNzw93guWSdKLmr2vHEWGG4sNv7TJofL7TZvy")
>> PrivateKey
Type | Name | Required | Description |
---|---|---|---|
bytes | message | Yes | Message to sign in bytes |
Return Value
This method signs a message using the private key.
from crypto.identity.private_key import PrivateKey
private_key = PrivateKey.from_passphrase('this is a top secret passphrase');
signature = private_key.sign(bytes.fromhex("Hello World"));
>> bytes
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase to derive the public key |
Return Value
This function returns a public key instance object derived from the given passphrase.
from crypto.identity.public_key import PublicKey
public_key = PublicKey.from_passphrase("super secret passphrase")
>> PublicKey
Create a public key instance from a hex string
def from_hex(public_key: str) -> PublicKey:
Parameters
Type | Name | Required | Description |
---|---|---|---|
str | public_key | Yes | Hexadecimal representation of the public key |
Return Value
This function returns a PublicKey object created from the given hex string.
from crypto.identity.public_key import PublicKey
public_key = PublicKey.from_hex("validPublicKeyHex")
>> PublicKey
Recover a public key from a signature
def recover(cls, message: bytes, signature: bytes) -> PublicKey:
Parameters
Type | Name | Required | Description |
---|---|---|---|
bytes | message | Yes | Message to use to recover |
bytes | signature | Yes | Signature to use to recover |
Return Value
This function returns a PublicKey object recovered from the given signature and message.
from crypto.identity.public_key import PublicKey
publicKey = PublicKey.recover(message, signature);
>> PublicKey
Derive a WIF from a passphrase
def wif_from_passphrase(passphrase: str, network_wif: Optional[int] = None) -> str:
Parameters
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase to derive the WIF |
int | network_wif | No | Optional network WIF |
Return Value
This function returns a Wallet Import Format (WIF) string derived from the passphrase.
from crypto.identity.wif import WIF
wif = WIF.from_passphrase("super secret passphrase", 170)
>> 'L2KVU2v4a1kgwem1S7LhWhTkzwMBvd1aR9Kh8itwu1c7cnRXAUxE'
Network
Set the network you want to use in the crypto library
def set_network(network: AbstractNetwork) -> None:
Parameters
Type | Name | Required | Description |
---|---|---|---|
AbstractNetwork | network | Yes | Network object (Testnet, Mainnet, etc) |
Return Value
This method returns void.
from crypto.configuration.network import Network
from crypto.networks.mainnet import Mainnet
Network.set_network(Mainnet())
>> None
# Custom network example
from crypto.configuration.network import Network
from crypto.networks.abstract_network import AbstractNetwork
class CustomNetwork(AbstractNetwork) {
...
}
Network.set_network(CustomNetwork())
>> None
This function returns the current network.
from crypto.configuration.network import Network
current_network = Network.get_network()
>> AbstractNetwork
This method returns the chain ID as an integer.
from crypto.networks.testnet import Testnet
Testnet().chainId()
>> 10000
This method returns the epoch time in string format.
from crypto.networks.testnet import Testnet
Testnet().epoch()
>> '2017-03-21T13:00:00.000Z'
This method returns the WIF in hexadecimal string format.
from crypto.networks.testnet import Testnet
Testnet().wif()
>> 'ba'
Transactions
For full examples of using the Builders to create transactions, please have a look at the Examples page.
Type | Name | Required | Description |
---|---|---|---|
dict | data | No | Optional transaction data |
Return Value
This method creates and returns a new transaction builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new()
>> TransferBuilder
This method returns the string representation of the transaction.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = str(TransferBuilder.new())
>> '{"gasPrice":"5000000000","network":30,"gas":1000000,"nonce":"1","senderPublicKey":"","value":"0","data":""}'
Type | Name | Required | Description |
---|---|---|---|
int | gasLimit | Yes | Gas limit for the transaction |
Return Value
This method sets the gas limit for the transaction and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().gasLimit(21000)
>> TransferBuilder
Type | Name | Required | Description |
---|---|---|---|
str | to | Yes | Recipient address |
Return Value
This method sets the recipient address for the transaction and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().to('0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A')
>> TransferBuilder
Type | Name | Required | Description |
---|---|---|---|
int | gas_price | Yes | Gas price for the transaction |
Return Value
This method sets the gas price for the transaction and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().gas_price(5000000000)
>> TransferBuilder
Type | Name | Required | Description |
---|---|---|---|
str | nonce | Yes | Nonce for the transaction |
Return Value
This method sets the nonce for the transaction and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().nonce('12')
>> TransferBuilder
Type | Name | Required | Description |
---|---|---|---|
int | network | Yes | Network identifier for the transaction |
Return Value
This method sets the network for the transaction and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().network(30)
>> TransferBuilder
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase for signing the transaction |
Return Value
This method signs the transaction using the provided passphrase and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().sign('this is a top secret passphrase')
>> TransferBuilder
Sign the transaction with a second signature (only for legacy addresses)
def legacySecondSign(self, passphrase: str, secondPassphrase: str):
Parameters
Type | Name | Required | Description |
---|---|---|---|
str | passphrase | Yes | Passphrase for signing the transaction |
str | secondPassphrase | Yes | Second Passphrase for signing the transaction |
Return Value
This method signs the transaction using the provided passphrases and returns the builder instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
transaction = TransferBuilder.new().legacySecondSign('this is a top secret passphrase', 'this is a top secret second passphrase')
>> TransferBuilder
This method returns the validity of the transaction as a bool.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
is_valid = (TransferBuilder.new()
.to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
.sign('this is a top secret passphrase')).verify()
>> true
This method converts the transaction to a dictionary and returns it.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
data = TransferBuilder.new().to_dict()
>> {
'gasPrice': '5000000000',
'network': 30,
'gasLimit': 1000000,
'nonce': 1,
'senderPublicKey': '',
'value': '0',
'data': '',
}
This method converts the transaction to a JSON string and returns it.
from crypto.transactions.builder.transfer_builder import TransferBuilder
# Using TransferBuilder as an example of AbstractTransactionBuilder
json = TransferBuilder.new().to_json()
>> '{"value": "0", ..., "data": ""}'
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
str | payload | Yes | EVM code in hexadecimal format (without `0x` prefix) |
Return Value
This method sets the payload for the EVM call and returns the builder instance.
from crypto.transactions.builder.evm_call_builder import EvmCallBuilder
transaction = EvmCallBuilder.new().payload('yourHexPayload')
>> EvmCallBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
str | address | Yes | Address to recipient |
str | value | Yes | Amount to send to recipient |
Return Value
This method adds a recipient to the transaction and returns the builder instance.
from crypto.transactions.builder.multipayment_builder import MultipaymentBuilder
transaction = MultipaymentBuilder.new().pay('0x512F366D524157BcF734546eB29a6d687B762255', '100000')
>> MultipaymentBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
int | value | Yes | Amount to transfer in the smallest unit |
Return Value
This method returns the transaction instance with the value set.
from crypto.transactions.builder.transfer_builder import TransferBuilder
transaction = TransferBuilder.new().value('1000000000000000000')
>> TransferBuilder
Inherits from:AbstractTransactionBuilder
Set the validator BLS public key
def validator_public_key(self, validator_public_key: str):
Parameters
Type | Name | Required | Description |
---|---|---|---|
str | validator_public_key | Yes | Validator BLS public key to register |
Return Value
This method returns the transaction instance with the validator public key set.
from crypto.transactions.builder.validator_registration_builder import ValidatorRegistrationBuilder
transaction = ValidatorRegistrationBuilder.new().validator_public_key('954f46d6097a1d314e900e66e11e0dad0a57cd03e04ec99f0dedd1c765dcb11e6d7fa02e22cf40f9ee23d9cc1c0624bd')
>> ValidatorRegistrationBuilder
Type | Name | Required | Description |
---|---|---|---|
int | value | Yes | Amount to lock in wei |
Return Value
This method returns the transaction instance with the value set.
from crypto.transactions.builder.validator_registration_builder import ValidatorRegistrationBuilder
transaction = ValidatorRegistrationBuilder.new().value('250000000000000000000')
>> ValidatorRegistrationBuilder
Inherits from:AbstractTransactionBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
str | username | Yes | Username to register |
Return Value
This method returns the transaction instance with the Username set.
from crypto.transactions.builder.username_registration_builder import UsernameRegistrationBuilder
transaction = UsernameRegistrationBuilder.new().username('myusername')
>> UsernameRegistrationBuilder
Inherits from:AbstractTransactionBuilder
Inherits from:AbstractTransactionBuilder
Type | Name | Required | Description |
---|---|---|---|
str | vote | Yes | Address to vote for |
Return Value
This method returns the transaction instance with the vote set.
from crypto.transactions.builder.vote_builder import VoteBuilder
transaction = VoteBuilder.new().vote('0x512F366D524157BcF734546eB29a6d687B762255')
>> VoteBuilder
Inherits from:AbstractTransactionBuilder
It is recommended to use the Transaction Builders instead of the AbstractTransaction class directly. You can also access the underlying transaction
property of the Builder to work with the AbstractTransaction instance, e.g. TransferBuilder.new().transaction
.
Type | Name | Required | Description |
---|---|---|---|
dict | data | Yes | The transaction data. |
Return Value
This method initializes a new transaction instance.
from crypto.transactions.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
transaction = Transfer({...})
>> Transfer
This method returns the payload of the transaction as a string.
from crypto.transactions.transfer import Transfer
from crypto.transactions.Vote import vote
from crypto.utils.abi_encoder import AbiEncoder
payload = Transfer().getPayload()
>> ''
encoded = AbiEncoder().encode_function_call('vote', ['0xC3bBE9B1CeE1ff85Ad72b87414B0E9B7F2366763'])
payload = Vote({'data': encoded}).getPayload()
>> '0x6dd7d8ea000000000000000000000000c3bbe9b1cee1ff85ad72b87414b0e9b7f2366763'
Type | Name | Required | Description |
---|---|---|---|
PrivateKey | private_key | Yes | Private key for signing |
Return Value
This method signs the transaction and returns the transaction instance.
from crypto.transactions.types.transfer import Transfer
from crypto.identity.private_key import PrivateKey
# Using Transfer as an example of AbstractTransaction
Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase'))
>> Transfer
This method recovers the sender's public key and address from the transaction signature.
from crypto.transactions.types.transfer import Transfer
from crypto.identity.private_key import PrivateKey
# Using Transfer as an example of AbstractTransaction
transfer = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase'))
transfer.recoverSender()
transaction_json = transfer.toJson()
>> '{..., "to":"0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22"}'
This method verifies the transaction signature and returns a boolean indicating validity.
from crypto.transactions.types.transfer import Transfer
from crypto.identity.private_key import PrivateKey
# Using Transfer as an example of AbstractTransaction
transfer = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).sign(PrivateKey.fromPassphrase('this is a top secret passphrase'))
transfer.verify()
>> True
Type | Name | Required | Description |
---|---|---|---|
bool | skip_signature | No | Serialization options |
Return Value
This method serializes the transaction and returns it as a Buffer.
from crypto.transactions.types.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
serialized = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).serialize()
>> Buffer
Type | Name | Required | Description |
---|---|---|---|
bool | skip_signature | Yes | Serialization options |
Return Value
This method computes and returns the transaction hash.
from crypto.transactions.types.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
transaction_hash = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).hash()
>> string
This method refreshes the payload data of the transaction and returns the transaction instance.
from crypto.transactions.types.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
transaction = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).refresh_payload_data();
>> Transfer
This method returns the transaction data as a dict.
from crypto.transactions.types.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
transaction_dict = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).to_dict();
>> {'gasPrice': '5000000000', 'gasLimit': '21000', ...}
This method returns the JSON representation of the transaction as a string.
from crypto.transactions.types.transfer import Transfer
# Using Transfer as an example of AbstractTransaction
transaction_dict = Transfer({
'value': '1',
'nonce': 1,
'to': '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
'gasPrice': 5000000000,
'gasLimit': 21000,
}).to_json();
>> '{"gasPrice":"5000000000", ...}'
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Inherits from:AbstractTransaction
Serializer
Type | Name | Required | Description |
---|---|---|---|
AbstractTransaction | transaction | Yes | The transaction object to serialize. |
Return Value
This method creates and returns a new serializer instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
from crypto.transactions.serializer import Serializer
transaction = (TransferBuilder.new()
.value('0')
.gas_price('5000000000')
.gas_limit('21000')
.recipient_address('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
.sign('this is a top secret passphrase')).transaction
serializer = Serializer.new(transaction)
>> Serializer
Type | Name | Required | Description |
---|---|---|---|
bool | skip_signature | No | Whether to skip the signature in serialization. |
Return Value
This method serializes the transaction into a byte array.
from crypto.transactions.builder.transfer_builder import TransferBuilder
from crypto.transactions.serializer import Serializer
transaction = (TransferBuilder.new()
.value('0')
.gas_price('5000000000')
.gas_limit('21000')
.recipient_address('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
.sign('this is a top secret passphrase')).transaction
serialized = Serializer.new(transaction).serialize()
>> bytes
Deserializer
Type | Name | Required | Description |
---|---|---|---|
str | serialized | Yes | The serialized transaction string. |
Return Value
This static method creates and returns a new deserializer instance.
from crypto.transactions.builder.transfer_builder import TransferBuilder
from crypto.transactions.deserializer import Deserializer
transaction = (TransferBuilder.new()
.value('0')
.gas_price('5000000000')
.gas_limit('21000')
.recipient_address('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
.sign('this is a top secret passphrase')).transaction
deserializer = Deserializer.new(transaction.serialize().getHex())
>> Deserializer
This method deserializes the transaction from the provided serialized data and returns an `AbstractTransaction` object.
from crypto.transactions.builder.transfer_builder import TransferBuilder
from crypto.transactions.deserializer import Deserializer
transaction = (TransferBuilder.new()
.value('0')
.gas_price('5000000000')
.gas_limit('21000')
.recipient_address('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
.sign('this is a top secret passphrase')).transaction
transaction = Deserializer.new(transaction.serialize().getHex()).deserialize()
>> Transfer
Decode the payload of a transaction
def decode_payload(data: dict, abi_type: ContractAbiType = ContractAbiType.CONSENSUS) -> Optional[dict]:
Parameters
Type | Name | Required | Description |
---|---|---|---|
dict | data | Yes | The dictionary containing the payload data to decode. |
ContractAbiType | abi_type | No | Contract ABI Type to decode against |
Return Value
This method decodes the payload data of a transaction and returns it as a dictionary. If decoding fails, it returns `None`.
from crypto.transactions.deserializer import Deserializer
from crypto.enums.contract_abi_type import ContractAbiType
payload_data = Deserializer.decode_payload(transaction_data)
>> {'args': [...], ...}
payload_data = Deserializer.decode_payload(transaction_data, ContractAbiType.USERNAMES)
>> {'args': [...], ...}
Message
Create a new message instance
def new(public_key: Union[bytes, str], message: Union[bytes, str], signature: Union[bytes, str]):
Parameters
Type | Name | Required | Description |
---|---|---|---|
Union[bytes, str] | public_key | Yes | Public key of the sender |
Union[bytes, str] | message | Yes | Message object |
Union[bytes, str] | signature | Yes | Signature of the message |
Return Value
This method returns a new message instance.
from crypto.utils.message import Message
message = Message.new(
public_key='034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
message='0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
signature='Hello World',
)
>> Message
Sign a message using the given passphrase
def sign(cls, message: Union[bytes, str], passphrase: Union[bytes, str]):
Parameters
Type | Name | Required | Description |
---|---|---|---|
Union[bytes, str] | message | Yes | Message to sign |
Union[bytes, str] | passphrase | Yes | Passphrase for signing |
Return Value
This method signs the message and returns a new Message instance.
from crypto.utils.message import Message
signed_message = Message.sign("Hello, blockchain!", b"super secret passphrase")
>> Message
This method verifies the message and returns a boolean value indicating validity.
from crypto.utils.message import Message
signed_message = Message.sign("Hello, blockchain!", b"super secret passphrase")
is_valid = signed_message.verify()
>> True
This method converts the message to a dict and returns it.
from crypto.utils.message import Message
signed_message = Message.sign("Hello, blockchain!", b"super secret passphrase")
message_dict = signed_message.to_dict()
>> {
'public_key': '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
'signature': '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
'message': 'Hello World',
}
This method returns the string representation of the message in JSON format.
from crypto.utils.message import Message
signed_message = Message.sign("Hello, blockchain!", b"super secret passphrase")
json_message = str(signed_message)
>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'
This method converts the message to a JSON string and returns it.
from crypto.utils.message import Message
signed_message = Message.sign("Hello, blockchain!", b"super secret passphrase")
json_message = signed_message.to_json()
>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'
Slot
This method returns the time difference between now and the network start in int format.
from crypto.utils.slot import Slot
network_time = Slot.time()
>> 200366014
This method returns the network start epoch as a datetime object.
from crypto.utils.slot import Slot
epoch_time = Slot.epoch()
>> 1490101200
ArgumentDecoder
Type | Name | Required | Description |
---|---|---|---|
string | bytes | Yes | The encoded bytes to be decoded |
Return Value
This method returns a new instance of the ArgumentDecoder class.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
>> ArgumentDecoder
This method returns the decoded string.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
string = decoder.decode_string()
>> str
This method returns the decoded address.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
address = decoder.decode_address()
>> str
This method returns an unsigned integer as a string.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
value = decoder.decode_unsigned_int()
>> int
This method returns an signed integer as a string.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
value = decoder.decode_signed_int()
>> int
This method returns a boolean.
from crypto.utils.abi.argument_decoder import ArgumentDecoder
decoder = ArgumentDecoder(argument)
value = decoder.decode_bool()
>> bool
RlpEncoder
Type | Name | Required | Description |
---|---|---|---|
Union[str, bytes, int, list] | data | Yes | The data to encode. |
Return Value
This method encodes data using the Recursive Length Prefix (RLP) algorithm and returns the encoded data as a hexadecimal string.
from crypto.utils.rlp_encoder import RlpEncoder
rlp_encoding = RlpEncoder.encode([
bytes(network),
bytes(nonce),
b'',
bytes(gasPrice),
bytes(gas),
bytes(to),
bytes(value),
bytes(transaction_data),
[],
bytes(v),
bytes(r),
bytes(s),
])
>> str
RlpDecoder
Type | Name | Required | Description |
---|---|---|---|
Union[str, list] | data | Yes | The data to decode. |
Return Value
This method decodes encoded Recursive Length Prefix (RLP) data and returns the decoded data as a string or list.
from crypto.utils.rlp_decoder import RlpDecoder
decoded_rlp = RlpDecoder.decode('0x' + serialized_transaction[2:])
>> Union[str, list]
UnitConverter
Parse a value into the appropriate units
def parse_units(value: Union[float, int, str, Decimal], unit='ark') -> int:
Parameters
Type | Name | Required | Description |
---|---|---|---|
Union[float, int, str, Decimal] | value | Yes | The value to be converted |
str | unit | No | The unit to convert to (default is 'ark') |
Return Value
This method returns the value converted to the smallest unit as a string.
from crypto.utils.unit_converter import UnitConverter
wei_value = UnitConverter.parse_units(1, 'wei')
>> 1
gwei_value = UnitConverter.parse_units(1, 'gwei')
>> 1000000000
ark_value = UnitConverter.parse_units(1, 'ark')
>> 1000000000000000000
ark_decimal_value = UnitConverter.parse_units(0.1, 'ark')
>> 100000000000000000
Format a value from smaller units to a larger unit
def format_units(value: Union[float, int, str, Decimal], unit='ark') -> Decimal:
Parameters
Type | Name | Required | Description |
---|---|---|---|
str | value | Yes | The value in the smallest unit (as a string) |
str | unit | No | The unit to format to (default is 'ark') |
Return Value
This method returns the value converted from the smallest unit to the specified unit as a float.
from crypto.utils.unit_converter import UnitConverter
formatted_wei = UnitConverter.format_units('1', 'wei')
>> 1.0
formatted_gwei = UnitConverter.format_units('1000000000', 'gwei')
>> 1.0
formatted_ark = UnitConverter.format_units('1000000000000000000', 'ark')
>> 1.0
formatted_ark_decimal = UnitConverter.format_units('100000000000000000', 'ark')
>> 0.1
Format a wei value to an ark value
def wei_to_ark(value: Union[float, int, str, Decimal], suffix=None):
Parameters
Type | Name | Required | Description |
---|---|---|---|
Union[float, int, str, Decimal] | value | Yes | The wei value (as a string, int or float) |
str | suffix | No | The suffix to show after the formatted value (default is null) |
Return Value
This method returns the wei value formatted as an ARK value.
from crypto.utils.unit_converter import UnitConverter
formatted_value = UnitConverter.wei_to_ark(1, 'DARK')
>> 0.000000000000000001 DARK
formatted_value = UnitConverter.wei_to_ark(1)
>> 0.000000000000000001
formatted_value = UnitConverter.wei_to_ark('1000000000000000000', 'DARK')
>> 1 DARK
formatted_value = UnitConverter.wei_to_ark('1000000000000000000')
>> 1
Format a gwei value to an ark value
def gwei_to_ark(value: Union[float, int, str, Decimal], suffix=None):
Parameters
Type | Name | Required | Description |
---|---|---|---|
Union[float, int, str, Decimal] | value | Yes | The gwei value (as a string, int or float) |
str | suffix | No | The suffix to show after the formatted value (default is null) |
Return Value
This method returns the gwei value formatted as an ARK value.
from crypto.utils.unit_converter import UnitConverter
formatted_value = UnitConverter.gwei_to_ark(1, 'DARK')
>> 0.000000001 DARK
formatted_value = UnitConverter.gwei_to_ark(1)
>> 0.000000001
formatted_value = UnitConverter.gwei_to_ark('1000000000', 'DARK')
>> 1 DARK
formatted_value = UnitConverter.gwei_to_ark('1000000000')
>> 1