|
| 1 | +--- |
| 2 | +namespace-identifier: avax-caip2 |
| 3 | +title: Avalanche Namespace - Chains |
| 4 | +author: Gergely Lovas (@gergelylovas) |
| 5 | +status: Draft |
| 6 | +type: Standard |
| 7 | +created: 2024-07-16 |
| 8 | +requires: CAIP-2 |
| 9 | +--- |
| 10 | + |
| 11 | +# CAIP-2 |
| 12 | + |
| 13 | +*For context, see the [CAIP-2][] specification.* |
| 14 | + |
| 15 | +## Rationale |
| 16 | + |
| 17 | +In CAIP-2 a general blockchain identification scheme is defined. This is the |
| 18 | +implementation of CAIP-2 for the Avalanche Ecosystem. |
| 19 | +Blockchains in the "avax" namespace are validated by their `blockchainID`. |
| 20 | +The `blockchainID` in Avalanche refers to the [txID][] that created the |
| 21 | +blockchain on the Avalanche [P-Chain][Multichain Architecture]. |
| 22 | +These blockchain IDs require transformations to be used as conformant CAIP-2 |
| 23 | +references. |
| 24 | + |
| 25 | +## Syntax |
| 26 | + |
| 27 | +The namespace "avax" refers to the Avalanche open-source blockchain platform. |
| 28 | + |
| 29 | +### Reference Definition |
| 30 | + |
| 31 | +The definition for this namespace will use the `blockchainID` as an identifier |
| 32 | +for different Avalanche chains. Since the P-Chain's `blockchainID` is the same |
| 33 | +on Testnet and Mainnet, to ensure uniqueness, a testnet prefix is introduced |
| 34 | +for chains on the [Fuji Testnet][]. |
| 35 | + |
| 36 | +The method for calculating the chain ID is as follows with pseudo-code: |
| 37 | + |
| 38 | +``` |
| 39 | +first_32_chars(base64url(sha256(concat(testnet_prefix, blockchain_id)))) |
| 40 | +``` |
| 41 | + |
| 42 | +- `blockchain_id` = `txID` that created the blockchain on the Avalanche P-Chain |
| 43 | +- `testnet_prefix`= `fuji` when the chain was created on the Fuji Testnet; |
| 44 | + empty string otherwise |
| 45 | +- `concat`= a string concatenation function |
| 46 | +- `sha256`= a SHA256 hash function |
| 47 | +- `base64url`= a Base64URL encoder |
| 48 | +- `first_32_chars`= a function to extract the first 32 characters of the |
| 49 | + resulting string and dropping the rest |
| 50 | + |
| 51 | +### Resolution Method |
| 52 | + |
| 53 | +To resolve a blockchain reference for the Avalanche namespace, make a JSON-RPC |
| 54 | +request to the [Info API][] with method `info.getBlockchainID`, for example: |
| 55 | + |
| 56 | +```jsonc |
| 57 | +// Request |
| 58 | +{ |
| 59 | + "jsonrpc":"2.0", |
| 60 | + "id" :1, |
| 61 | + "method" :"info.getBlockchainID", |
| 62 | + "params": { |
| 63 | + "alias":"C" |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Response |
| 68 | +{ |
| 69 | + "jsonrpc": "2.0", |
| 70 | + "id": 1, |
| 71 | + "result": { |
| 72 | + "blockchainID": "2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5" |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +For example, this Node.js code transforms the above response into a CAIP-2 identifier: |
| 78 | + |
| 79 | +```javascript |
| 80 | +const isTestnet = false; |
| 81 | +const blockchainID = "2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5"; |
| 82 | +const testnetPrefix = isTestnet ? 'fuji' : ''; |
| 83 | +const hash = createHash('sha256') |
| 84 | + .update(testnetPrefix + blockchainID) |
| 85 | + .digest('base64url') |
| 86 | + .substring(0, 32); |
| 87 | +const identifier = "avax:" + hash; |
| 88 | + |
| 89 | +console.log(identifier); // prints "avax:8aDU0Kqh-5d23op-B-r-4YbQFRbsgF9a" |
| 90 | +``` |
| 91 | + |
| 92 | +### Backwards Compatibility |
| 93 | + |
| 94 | +Not applicable |
| 95 | + |
| 96 | +## Test Cases |
| 97 | + |
| 98 | +This is a list of manually composed examples |
| 99 | + |
| 100 | +``` |
| 101 | +# Avalanche P Chain Mainnet |
| 102 | +# blockchainID: 11111111111111111111111111111111LpoYY |
| 103 | +avax:Rr9hnPVPxuUvrdCul-vjEsU1zmqKqRDo |
| 104 | +
|
| 105 | +# Avalanche C Chain Mainnet |
| 106 | +# blockchainID: 2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5 |
| 107 | +avax:8aDU0Kqh-5d23op-B-r-4YbQFRbsgF9a |
| 108 | +
|
| 109 | +# Avalanche X Chain Mainnet |
| 110 | +# blockchainID: 2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM |
| 111 | +avax:imji8papUf2EhV3le337w1vgFauqkJg- |
| 112 | +
|
| 113 | +# Avalanche P Chain Testnet |
| 114 | +# blockchainID: 11111111111111111111111111111111LpoYY |
| 115 | +avax:Sj7NVE3jXTbJvwFAiu7OEUo_8g8ctXMG |
| 116 | +
|
| 117 | +# Avalanche C Chain Testnet |
| 118 | +# blockchainID: yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp |
| 119 | +avax:YRLfeDBJpfEqUWe2FYR1OpXsnDDZeKWd |
| 120 | +
|
| 121 | +# Avalanche X Chain Testnet |
| 122 | +# blockchainID: 2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm |
| 123 | +avax:8AJTpRj3SAqv1e80Mtl9em08LhvKEbkl |
| 124 | +
|
| 125 | +``` |
| 126 | + |
| 127 | +## References |
| 128 | + |
| 129 | +- [Fuji Testnet][] - Fuji Testnet in Avalanche official documentation |
| 130 | +- [Info API][] - Info API reference in Avalanche official documentation |
| 131 | +- [txID][] - Create Chain TX reference in Avalanche official documentation |
| 132 | +- [Multichain Architecture][] - Overview of P-Chain, X-Chain, and C-Chain system |
| 133 | + |
| 134 | +[Multichain Architecture]: https://docs.avax.network/learn/avalanche/avalanche-platform |
| 135 | + |
| 136 | +[CAIP-2]: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md |
| 137 | +[Fuji Testnet]: https://docs.avax.network/learn/avalanche/fuji |
| 138 | +[Info API]: https://docs.avax.network/reference/avalanchego/info-api |
| 139 | +[txID]: https://docs.avax.network/reference/avalanchego/p-chain/txn-format#unsigned-create-chain-tx |
| 140 | + |
| 141 | +## Copyright |
| 142 | + |
| 143 | +Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/). |
0 commit comments