/ scripts / pubkey_to_did_key.py
pubkey_to_did_key.py
 1  #!/usr/bin/env python3
 2  """
 3  Convert an OpenSSH Ed25519 public key to a did:key identifier.
 4  
 5  Usage: python3 pubkey_to_did_key.py "<openssh-pub-key-line>"
 6  Example: python3 pubkey_to_did_key.py "ssh-ed25519 AAAAC3Nz... comment"
 7  """
 8  import base64
 9  import sys
10  
11  ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
12  
13  
14  def base58_encode(data: bytes) -> str:
15      n = int.from_bytes(data, 'big')
16      result = ''
17      while n:
18          n, rem = divmod(n, 58)
19          result = ALPHABET[rem] + result
20      pad = next((i for i, b in enumerate(data) if b != 0), len(data))
21      return ALPHABET[0] * pad + result
22  
23  
24  def openssh_pub_to_did_key(openssh_pub: str) -> str:
25      # OpenSSH wire format: [4-byte len][type][4-byte len][key bytes]
26      key_data = base64.b64decode(openssh_pub.strip().split()[1])
27      offset = 4 + int.from_bytes(key_data[0:4], 'big')  # skip "ssh-ed25519"
28      key_len = int.from_bytes(key_data[offset:offset + 4], 'big')
29      raw_key = key_data[offset + 4:offset + 4 + key_len]
30  
31      # did:key encoding: multicodec Ed25519 prefix (0xED 0x01) + raw pubkey
32      prefixed = bytes([0xED, 0x01]) + raw_key
33      return 'did:key:z' + base58_encode(prefixed)
34  
35  
36  if __name__ == '__main__':
37      if len(sys.argv) < 2:
38          print("Usage: pubkey_to_did_key.py '<openssh-pub-key>'", file=sys.stderr)
39          sys.exit(1)
40      print(openssh_pub_to_did_key(sys.argv[1]))