|
1 | 1 | --- |
2 | 2 | title: Address Conversion |
3 | | -description: Convert between different address formats |
| 3 | +description: Convert between different address formats using Core AddressStructure |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | # Address Conversion |
7 | 7 |
|
8 | | -Content coming soon. |
| 8 | +The Core `AddressStructure` module provides transformations between different address representations: Bech32 strings, hexadecimal, and raw bytes. |
| 9 | + |
| 10 | +## Available Formats |
| 11 | + |
| 12 | +### Bech32 (Human-Readable) |
| 13 | + |
| 14 | +Standard format used in wallets and explorers: |
| 15 | +- Mainnet base: `addr1...` |
| 16 | +- Testnet base: `addr_test1...` |
| 17 | +- Mainnet reward: `stake1...` |
| 18 | +- Testnet reward: `stake_test1...` |
| 19 | + |
| 20 | +### Hexadecimal |
| 21 | + |
| 22 | +Hex-encoded bytes, useful for low-level operations and debugging. |
| 23 | + |
| 24 | +### Raw Bytes |
| 25 | + |
| 26 | +Binary format (Uint8Array), used internally and for serialization. |
| 27 | + |
| 28 | +## Conversion Examples |
| 29 | + |
| 30 | +### Bech32 ↔ AddressStructure |
| 31 | + |
| 32 | +```typescript twoslash |
| 33 | +import { Address, BaseAddress } from "@evolution-sdk/evolution"; |
| 34 | + |
| 35 | +const bech32 = "addr1qx2kd28nq8ac5prwg32hhvudlwggpgfp8utlyqxu6wqgz62f79qsdmm5dsknt9ecr5w468r9ey0fxwkdrwh08ly3tu9sy0f4qd"; |
| 36 | + |
| 37 | +// Parse Bech32 string to address |
| 38 | +const address = Address.fromBech32(bech32) as BaseAddress.BaseAddress; |
| 39 | + |
| 40 | +console.log("Network ID:", address.networkId); |
| 41 | +console.log("Payment credential:", address.paymentCredential); |
| 42 | +console.log("Staking credential:", address.stakeCredential); |
| 43 | + |
| 44 | +// Convert address back to Bech32 |
| 45 | +const encoded = Address.toBech32(address); |
| 46 | +console.log("Bech32:", encoded); |
| 47 | +// Same as original: addr1qx2kd28nq8ac5prwg32hhvudlwggpgfp8utlyqxu6wqgz62f79qsdmm5dsknt9ecr5w468r9ey0fxwkdrwh08ly3tu9sy0f4qd |
| 48 | +``` |
| 49 | + |
| 50 | +### Hex ↔ AddressStructure |
| 51 | + |
| 52 | +```typescript twoslash |
| 53 | +import { Address } from "@evolution-sdk/evolution"; |
| 54 | + |
| 55 | +const hexAddress = "019493315cd92eb5d8c4304e67b7e16ae36d61d34502694657811a2c8e32c728d3861e164cab28cb8f006448139c8f1740ffb8e7aa9e5232dc"; |
| 56 | + |
| 57 | +// Parse hex to address |
| 58 | +const address = Address.fromHex(hexAddress); |
| 59 | + |
| 60 | +console.log("Parsed from hex:", address); |
| 61 | + |
| 62 | +// Convert address to hex |
| 63 | +const hex = Address.toHex(address); |
| 64 | +console.log("Hex:", hex); |
| 65 | +``` |
| 66 | + |
| 67 | +### Bytes ↔ AddressStructure |
| 68 | + |
| 69 | +```typescript twoslash |
| 70 | +import { BaseAddress, KeyHash, Address } from "@evolution-sdk/evolution"; |
| 71 | + |
| 72 | +// Create an address structure |
| 73 | +const address = new BaseAddress.BaseAddress({ |
| 74 | + networkId: 1, |
| 75 | + paymentCredential: new KeyHash.KeyHash({ |
| 76 | + hash: new Uint8Array(28) |
| 77 | + }), |
| 78 | + stakeCredential: new KeyHash.KeyHash({ |
| 79 | + hash: new Uint8Array(28) |
| 80 | + }) |
| 81 | +}); |
| 82 | + |
| 83 | +// Convert to raw bytes |
| 84 | +const bytes = Address.toBytes(address); |
| 85 | +console.log("Bytes length:", bytes.length); // 57 for base address |
| 86 | + |
| 87 | +// Parse from bytes |
| 88 | +const decoded = Address.fromBytes(bytes); |
| 89 | +console.log("Decoded:", decoded); |
| 90 | +``` |
| 91 | + |
| 92 | +## Chain Conversions |
| 93 | + |
| 94 | +Combine transformations for multi-step conversions: |
| 95 | + |
| 96 | +### Bech32 → Hex |
| 97 | + |
| 98 | +```typescript twoslash |
| 99 | +import { Address } from "@evolution-sdk/evolution"; |
| 100 | + |
| 101 | +const bech32 = "addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp"; |
| 102 | + |
| 103 | +// Parse from Bech32 |
| 104 | +const address = Address.fromBech32(bech32); |
| 105 | + |
| 106 | +// Convert to hex |
| 107 | +const hex = Address.toHex(address); |
| 108 | +console.log("Hex:", hex); |
| 109 | +``` |
| 110 | + |
| 111 | +### Hex → Bech32 |
| 112 | + |
| 113 | +```typescript twoslash |
| 114 | +import { Address } from "@evolution-sdk/evolution"; |
| 115 | + |
| 116 | +const hex = "009493315cd92eb5d8c4304e67b7e16ae36d61d34502694657811a2c8e32c728d3861e164cab28cb8f006448139c8f1740ffb8e7aa9e5232dc"; |
| 117 | + |
| 118 | +// Parse from hex |
| 119 | +const address = Address.fromHex(hex); |
| 120 | + |
| 121 | +// Convert to Bech32 |
| 122 | +const bech32 = Address.toBech32(address); |
| 123 | +console.log("Bech32:", bech32); |
| 124 | +``` |
| 125 | + |
| 126 | +## Best Practices |
| 127 | + |
| 128 | +**Use Bech32 for Display**: Always show users Bech32-encoded addresses. They're human-readable and include error detection. |
| 129 | + |
| 130 | +**Use Bytes Internally**: For storage and serialization, raw bytes are most efficient. |
| 131 | + |
| 132 | +**Validate After Conversion**: Always validate addresses after parsing from external sources. |
| 133 | + |
| 134 | +**Network Awareness**: Verify the network ID matches your expected environment (mainnet vs testnet). |
| 135 | + |
| 136 | +## Error Handling |
| 137 | + |
| 138 | +Conversions can fail with invalid input: |
| 139 | + |
| 140 | +```typescript twoslash |
| 141 | +import { Address } from "@evolution-sdk/evolution"; |
| 142 | + |
| 143 | +const invalidBech32 = "invalid_address"; |
| 144 | + |
| 145 | +// Error handling with try-catch |
| 146 | +try { |
| 147 | + const address = Address.fromBech32(invalidBech32); |
| 148 | + console.log("Parsed address:", address); |
| 149 | +} catch (error) { |
| 150 | + console.error("Failed to parse address:", error); |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Next Steps |
| 155 | + |
| 156 | +- [Address Validation](/docs/addresses/validation) - Verify address correctness |
| 157 | +- [Address Construction](/docs/addresses/construction) - Build addresses from scratch |
| 158 | +- [Base Addresses](/docs/addresses/base) - Understanding base address structure |
| 159 | +- [Enterprise Addresses](/docs/addresses/enterprise) - Enterprise address format |
0 commit comments