3 releases (breaking)
0.4.0 | Nov 8, 2024 |
---|---|
0.3.0 | Nov 7, 2024 |
0.2.0 | Nov 6, 2024 |
0.1.0 |
|
#798 in Encoding
346 downloads per month
39KB
654 lines
TRAIT Keyless Address Encoding and Decoding Library
This library provides functions for encoding and decoding keyless addresses, used by the TRAIT blockchain: application agent addresses, transactional addresses, and named addresses.
A keyless address is a type of address that doesn't rely on cryptographic key pairs for identification. Instead, it is derived from a combination of identifiers and checksums, making it suitable and convenient for off-chain applications to use.
Example
use trait_keyless::*;
// Encode an AppAgent keyless address
let app_agent_id = 123;
let app_agent_address = encode_app_agent_account(&app_agent_id);
// Decode the keyless address
let decoded_app_agent_id = decode_app_agent_account(&app_agent_address).unwrap();
assert_eq!(decoded_app_agent_id, app_agent_id);
lib.rs
:
This module provides functionality for encoding and decoding keyless addresses.
A keyless address is a type of address that doesn't rely on cryptographic key pairs for identification. Instead, it is derived from a combination of identifiers and checksums, making it suitable and convenient for off-chain applications to use.
Examples
use trait_keyless::*;
// Encode an AppAgent keyless address
let app_agent_id = 123;
let app_agent_address = encode_app_agent_account(&app_agent_id);
// Decode the keyless address
let decoded_app_agent_id = decode_app_agent_account(&app_agent_address).unwrap();
assert_eq!(decoded_app_agent_id, app_agent_id);
Keyless Address Structure
A keyless address consists of two parts:
-
Open Part: Contains specific identifiers or data relevant to the address type.
-
Checksum: A hash-based checksum to ensure address integrity.
Each keyless address belongs to one of the address types. It can be one of the following:
APP_AGENT_ADDRESS_IDENTIFIER
: Identifies an AppAgent keyless address.TRANSACTIONAL_ADDRESS_IDENTIFIER
: Identifies a Transactional keyless address.NAMED_ADDRESS_IDENTIFIER
: Identifies a Named keyless address.
Each address type has a different structure for the open part, as described below:
-
AppAgent Address:
- [0..=3] -> AppAgentId
- [4..4] -> Address type identifier
APP_AGENT_ADDRESS_IDENTIFIER
- [5..=31] -> Checksum
-
Transactional Address:
- [0..=3] -> AppAgentId
- [4..4] -> Address type identifier
TRANSACTIONAL_ADDRESS_IDENTIFIER
- [5..=8] -> TransactionalId
- [9..=31] -> Checksum
-
Named Address:
- [0..=3] -> AppAgentId
- [4..4] -> Address type identifier
NAMED_ADDRESS_IDENTIFIER
- [5..=14] -> Address name
- [15..=31] -> Checksum
Public Interface
This module exposes functions to encode, decode, and manipulate keyless addresses:
encode_app_agent_account
: Builds a keyless address for the specifiedAppAgentId
.decode_app_agent_account
: Decodes the provided keyless address and retrieves the correspondingAppAgentId
.encode_transactional_account
: Builds a keyless address for the specifiedAppAgentId
andTransactionalId
.decode_transactional_account
: Decodes the provided keyless address and retrieves theAppAgentId
andTransactionalId
.encode_named_account
: Builds a keyless address for the specifiedAppAgentId
andName
.decode_named_account
: Decodes the provided keyless address and retrieves theAppAgentId
andAddressName
.decode_account
: Decodes any type of account, including regular accounts, and provides full information about the account.
Errors
Errors can occur during the encoding or decoding process, such as invalid input or checksum failure.
In such cases, functions return a Result
containing an error message.
Safety
This module assumes that the provided data types are used correctly and safely. It's essential to ensure that the input values are valid and within the expected ranges.
Dependencies
~3.5MB
~79K SLoC