Skip to content

Commit

Permalink
chore: fully automatic table generation
Browse files Browse the repository at this point in the history
The codec table is now created fully automatically. You just need
to run a single script. The script is written in Python in hope
that it can be adapted to be used for other multicodec implementations
as well. Python is most likely installed on all systems and no
big toolchain is needed in order to run/modify it.

This commit also updates the table to the most current version.

BREAKING CHANGE: multibase is not part of this package anymore

As multibase works differently from multicodec, those codecs
were [removed from the multicodec table], hence those are also
removed from this implementation as we have automatic
conversion from the upstream table.

[removed from the multicodec table]:
multiformats/multicodec@1ec0e97
  • Loading branch information
vmx authored and hacdias committed Jan 7, 2019
1 parent 9e251ce commit f3d8c0d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 46 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ https://multiformats.github.io/js-multicodec/

## Updating the lookup table

Updating the lookup table is a manual process. The source of truth is the
[multicodec default table](https://github.com/multiformats/multicodec/blob/master/table.csv). To make the process easier, there’s an [AWK script in the tools directory](tools/update-table.awk) that does a basic conversion of the default table. The result can’t be used as-is, but serves as a template for manual diffing. The workflow is:
Updating the lookup table is done with a script. The source of truth is the
[multicodec default table](https://github.com/multiformats/multicodec/blob/master/table.csv).
Update the table with running:

- Create a basic draft version

`curl -X GET https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv|awk -f tools/update-table.awk > /tmp/draft.js`

- Diff it with your tool of choice (e.g. [Meld](http://meldmerge.org/)) and apply the changes

`meld /tmp/draft.js base-table.js`
curl -X GET https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv|./tools/update-table.py

## Contribute

Expand Down
37 changes: 17 additions & 20 deletions src/base-table.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
// THIS FILE IS GENERATED, DO NO EDIT MANUALLY
// For more information see the README.md
/* eslint-disable dot-notation */
'use strict'

// spec and table at: https://github.com/multiformats/multicodec

exports = module.exports

// Miscellaneous
// miscellaneous
exports['raw'] = Buffer.from('55', 'hex')

// bases encodings
exports['base1'] = Buffer.from('01', 'hex')
exports['base2'] = Buffer.from('00', 'hex')
exports['base8'] = Buffer.from('07', 'hex')
exports['base10'] = Buffer.from('09', 'hex')

// Serialization formats
// serialization formats
exports['cbor'] = Buffer.from('51', 'hex')
exports['protobuf'] = Buffer.from('50', 'hex')
exports['rlp'] = Buffer.from('60', 'hex')
exports['bencode'] = Buffer.from('63', 'hex')

// Multiformats
// multiformats
exports['multicodec'] = Buffer.from('30', 'hex')
exports['multihash'] = Buffer.from('31', 'hex')
exports['multiaddr'] = Buffer.from('32', 'hex')
exports['multibase'] = Buffer.from('33', 'hex')
exports['md4'] = Buffer.from('d4', 'hex')
exports['md5'] = Buffer.from('d5', 'hex')

// multihashes
exports['identity'] = Buffer.from('00', 'hex')
exports['md4'] = Buffer.from('d4', 'hex')
exports['md5'] = Buffer.from('d5', 'hex')
exports['sha1'] = Buffer.from('11', 'hex')
exports['sha2-256'] = Buffer.from('12', 'hex')
exports['sha2-512'] = Buffer.from('13', 'hex')
Expand Down Expand Up @@ -376,32 +370,37 @@ exports['dccp'] = Buffer.from('21', 'hex')
exports['sctp'] = Buffer.from('84', 'hex')
exports['udt'] = Buffer.from('012d', 'hex')
exports['utp'] = Buffer.from('012e', 'hex')
exports['p2p'] = Buffer.from('01a5', 'hex')
exports['ipfs'] = Buffer.from('01a5', 'hex')
exports['http'] = Buffer.from('01e0', 'hex')
exports['https'] = Buffer.from('01bb', 'hex')
exports['quic'] = Buffer.from('01cc', 'hex')
exports['ws'] = Buffer.from('01dd', 'hex')
exports['wss'] = Buffer.from('01de', 'hex')
exports['onion'] = Buffer.from('01bc', 'hex')
exports['onion3'] = Buffer.from('01bd', 'hex')
exports['garlic64'] = Buffer.from('01be', 'hex')
exports['p2p-circuit'] = Buffer.from('0122', 'hex')
exports['dns'] = Buffer.from('35', 'hex')
exports['dns4'] = Buffer.from('36', 'hex')
exports['dns6'] = Buffer.from('37', 'hex')
exports['dnsaddr'] = Buffer.from('38', 'hex')
exports['p2p-websocket-star'] = Buffer.from('01df', 'hex')
exports['p2p-webrtc-star'] = Buffer.from('0113', 'hex')
exports['p2p-webrtc-direct'] = Buffer.from('0114', 'hex')
exports['unix'] = Buffer.from('0190', 'hex')

// archiving formats

// image formats

// video formats

// VCS formats
exports['git-raw'] = Buffer.from('78', 'hex')

// IPLD formats
exports['dag-pb'] = Buffer.from('70', 'hex')
exports['dag-cbor'] = Buffer.from('71', 'hex')
exports['dag-json'] = Buffer.from('0129', 'hex')
exports['git-raw'] = Buffer.from('78', 'hex')
exports['eth-block'] = Buffer.from('90', 'hex')
exports['eth-block-list'] = Buffer.from('91', 'hex')
exports['eth-tx-trie'] = Buffer.from('92', 'hex')
Expand All @@ -411,7 +410,6 @@ exports['eth-tx-receipt'] = Buffer.from('95', 'hex')
exports['eth-state-trie'] = Buffer.from('96', 'hex')
exports['eth-account-snapshot'] = Buffer.from('97', 'hex')
exports['eth-storage-trie'] = Buffer.from('98', 'hex')

exports['bitcoin-block'] = Buffer.from('b0', 'hex')
exports['bitcoin-tx'] = Buffer.from('b1', 'hex')
exports['zcash-block'] = Buffer.from('c0', 'hex')
Expand All @@ -422,7 +420,6 @@ exports['decred-block'] = Buffer.from('e0', 'hex')
exports['decred-tx'] = Buffer.from('e1', 'hex')
exports['dash-block'] = Buffer.from('f0', 'hex')
exports['dash-tx'] = Buffer.from('f1', 'hex')

exports['torrent-info'] = Buffer.from('7b', 'hex')
exports['torrent-file'] = Buffer.from('7c', 'hex')
exports['ed25519-pub'] = Buffer.from('ed', 'hex')
17 changes: 0 additions & 17 deletions tools/update-table.awk

This file was deleted.

62 changes: 62 additions & 0 deletions tools/update-table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3

import csv
import os
import sys

# This is relative to where this script resides. Though you can also define
# an absolute path
DEFAULT_OUTPUT_DIR = '../src'

# The header of the generated files
HEADER = '''\
// THIS FILE IS GENERATED, DO NO EDIT MANUALLY
// For more information see the README.md
/* eslint-disable dot-notation */
'use strict'
'''

def padded_hex(hexstring):
"""Creates a padded (starting with a 0 if odd) hex string"""
number = int(row['code'], 16)
hexbytes = '{:x}'.format(number)
if len(hexbytes) % 2:
prefix = '0x0'
else:
prefix = '0x'
return prefix + hexbytes


parsed = []
multicodec_reader = csv.DictReader(sys.stdin, skipinitialspace=True)
for row in multicodec_reader:
# Skip over headlines and not yet defined codes
if row['code'] and row['code'] != '0x':
code = padded_hex(row['code'])
name_const = row['codec'].upper().replace('-', '_')
name_human = row['codec']
parsed.append({
'const': name_const,
'human': name_human,
'code': code,
})
# add headlines
if row['codec'] and not row['description'] and not row['code']:
parsed.append({
'headline': row['codec']
})

tools_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(tools_dir, DEFAULT_OUTPUT_DIR)

print_file = os.path.join(output_dir, 'base-table.js')
with open(print_file, 'w') as ff:
ff.write(HEADER)
for index, codec in enumerate(parsed):
if 'headline' in codec:
ff.write("\n// {headline}\n".format(**codec))
else:
# print('{} {} {:02x}'.format(codec['human'], codec['code'], codec['code']))
hexstring = codec['code'][2:]
ff.write("exports['{}'] = Buffer.from('{}', 'hex')\n"
.format(codec['human'], hexstring))

0 comments on commit f3d8c0d

Please sign in to comment.