# Manipulation d'adresses SS58

**URL:** https://forum.duniter.org/t/manipulation-dadresses-ss58/11622
**Category:** Tools
**Tags:** ss58
**Created:** [10 October 2023 15:22 UTC](https://forum.duniter.org/t/manipulation-dadresses-ss58/11622 "2023-10-10T15:22:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://forum.duniter.org/user_avatar/forum.duniter.org/hugotrentesaux/32/6396_2.png) [@HugoTrentesaux](https://forum.duniter.org/u/HugoTrentesaux)
#### Post date: [10 October 2023 15:22 UTC](https://forum.duniter.org/t/manipulation-dadresses-ss58/11622/1 "2023-10-10T15:22:33Z")

</div>

Je partage certains outils utiles pour manipuler des adresse SS58 :

- le site [https://ss58.org/](https://ss58.org/) pour des petites conversions à la main
- l’outil en ligne de commande [subkey](https://docs.rs/subkey/latest/subkey/) pour génération, inspection, signature
- l’appli polkadotjs app section developer/utilities pour le décodage

Ou en Julia pour extraire le préfixe et la clé publique :

```julia
using Base58
# clé publique en base58
pubkey(x) = String(base58encode(base58decode(Vector{UInt8}(x))[2:end-2]))
# préfixe sous forme d'entier
prefix(x) = Int(base58decode(Vector{UInt8}(x))[1])

julia> prefix("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
42
julia> pubkey("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
"FHNpKmJrUtusuvKPGomAygQqeiks98bdV6yD61Stb6vg"

```

---

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://forum.duniter.org/user_avatar/forum.duniter.org/hugotrentesaux/32/6396_2.png) [@HugoTrentesaux](https://forum.duniter.org/u/HugoTrentesaux)
#### Post date: [20 September 2024 11:55 UTC](https://forum.duniter.org/t/manipulation-dadresses-ss58/11622/2 "2024-09-20T11:55:53Z")

</div>

Pour l’opération inverse en Julia :

```julia
using Blake2, Base58

# subkey inspect 5Dq8xjvkmbz7q4g2LbZgyExD26VSCutfEc6n4W4AfQeVHZqz
# Public Key URI `5Dq8xjvkmbz7q4g2LbZgyExD26VSCutfEc6n4W4AfQeVHZqz` is account:
# Network ID/Version: substrate
# Public key (hex): 0x4e1f8a8a639370952db38fef62a763e1b8eea5600b6ccafc60d6849f028ff70f
# Account ID: 0x4e1f8a8a639370952db38fef62a763e1b8eea5600b6ccafc60d6849f028ff70f
# Public key (SS58): 5Dq8xjvkmbz7q4g2LbZgyExD26VSCutfEc6n4W4AfQeVHZqz
# SS58 Address: 5Dq8xjvkmbz7q4g2LbZgyExD26VSCutfEc6n4W4AfQeVHZqz

ss58prefix = codeunits("SS58PRE") # SS58 prefix
chainprefix = hex2bytes("2a") # 42 prefix
pubkey = hex2bytes("4e1f8a8a639370952db38fef62a763e1b8eea5600b6ccafc60d6849f028ff70f")

md = zeros(UInt8, 64)
key = zeros(UInt8, 0)
input::Vector{UInt8} = vcat(ss58prefix, chainprefix, pubkey)

output = Blake2.Blake2b!(md, length(md), key, 0, input, length(input))
checksum = output[1:2]

addrs = vcat(chainprefix, pubkey, checksum)
ss58 = String(base58encode(addrs))

# 5Dq8xjvkmbz7q4g2LbZgyExD26VSCutfEc6n4W4AfQeVHZqz

```

---

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://forum.duniter.org/user_avatar/forum.duniter.org/hugotrentesaux/32/6396_2.png) [@HugoTrentesaux](https://forum.duniter.org/u/HugoTrentesaux)
#### Post date: [16 October 2024 09:45 UTC](https://forum.duniter.org/t/manipulation-dadresses-ss58/11622/3 "2024-10-16T09:45:25Z")

</div>

À noter que ma définition du préfixe n’est valable que pour un préfixe \<64, cf le code de py-substrate-interface :

```auto
if ss58_format < 64:
  ss58_format_bytes = bytes([ss58_format])
else:
  ss58_format_bytes = bytes([
    ((ss58_format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,
    (ss58_format >> 8) | ((ss58_format & 0b0000_0000_0000_0011) << 6)
  ])

```

En julia, ça donne :

```auto
# x est le préfixe ss58
[ UInt8(((x & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000),
  UInt8((x >> 8) | ((x & 0b0000_0000_0000_0011) <<6)) ]

```
