Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update encode decode so that the output is nicely formatted #1000

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 90 additions & 41 deletions dev/scripts/src/scripts/encodeDecode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 Prosopo (UK) Ltd.
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,52 +14,101 @@
import { at } from '@prosopo/util'
import { blake2AsHex, isAddress } from '@polkadot/util-crypto'
import { decodeAddress, encodeAddress } from '@polkadot/keyring'
import { hexToNumber, hexToString, hexToU8a, isHex, stringToHex, u8aToHex } from '@polkadot/util'
import { hexToNumber, hexToString, hexToU8a, isHex, stringToHex, u8aToHex, u8aToString } from '@polkadot/util'

const ss58Format = 42
// https://stackoverflow.com/a/75872362/1178971
function wrapItemToMultipleRows(item: { [key: string]: string }, maxCellWidth: number): { [key: string]: string }[] {
const isRemainingData = Object.values(item).find((value) => {
return value && value.length > 0
})

const arg = at(process.argv.slice(2), 0).trim()
const argIsHex = isHex(arg)
const argIsAddress = isAddress(arg, false, ss58Format)
console.log(`arg : ${arg}`)
console.log(`arg length : ${arg.length}`)
console.log(`argIsAddress : ${argIsAddress}`)
console.log(`argIsHex : ${argIsHex}`)
if (!isRemainingData) {
return []
}

if (argIsAddress) {
try {
const encodedAddress = encodeAddress(decodeAddress(arg, false, ss58Format), ss58Format)
const decodedAddress = decodeAddress(encodedAddress, false, ss58Format)
if (encodedAddress === arg) {
const hexAddress = u8aToHex(decodedAddress)
console.log(`Hex address ${hexAddress}`)
console.log(`Address bytes ${decodedAddress}`)
} else {
console.log(`Encoded address ${encodedAddress}`)
const itemRow: { [key: string]: string } = {}
const remaining: { [key: string]: string } = {}
Object.entries(item).forEach(([key, value]) => {
itemRow[key] = value?.slice ? value.slice(0, maxCellWidth) : value
remaining[key] = value?.slice ? value.slice(maxCellWidth) : value
})

console.log(`Address as hex ${u8aToHex(decodeAddress(encodedAddress))}`)
}
return [itemRow, ...wrapItemToMultipleRows(remaining, maxCellWidth)]
}

function consoleTableWithWrapping(data: { [key: string]: string }[], maxColWidth = 90) {
const tableItems = data.reduce<{ [key: string]: string }[]>((prev, item) => {
return [...prev, ...wrapItemToMultipleRows(item, maxColWidth)]
}, [])

console.table(tableItems)
}

function isJSON(arg: string): boolean {
try {
JSON.parse(arg)
return true
} catch (e) {
console.log(`Address encoding/decoding failed: ${e}`)
return false
}
}
if (argIsHex) {
console.log(`Decoding hex ${arg} to string`)
console.log(hexToString(arg))
console.log(`Decoding hex ${arg} to number`)
console.log(hexToNumber(arg))
console.log(`Uint8Array ${hexToU8a(stringToHex(arg))}`)
} else {
console.log(`Hashing string ${arg} using blake2AsHex`)
console.log(blake2AsHex(arg))
}

try {
const u8a = new Uint8Array(JSON.parse(arg))
console.log(`Found uint8array ${u8a}`)
const hex = u8aToHex(u8a)
console.log(`Hex version ${hex}`)
console.log(`Encoded address ${encodeAddress(hex, ss58Format)}`)
} catch (e) {
console.log(`Not a uint8array ${e}`)
function main() {
const ss58Format = process.env.PROSOPO_SS58_FORMAT ? parseInt(process.env.PROSOPO_SS58_FORMAT) : 42
const arg = at(process.argv.slice(2), 0).trim()
const argIsHex = isHex(arg)
const argIsAddress = isAddress(arg, false, ss58Format)
const output: { name: string; value: string }[] = []
output.push({ name: 'arg', value: arg })
output.push({ name: 'length', value: arg.length.toString() })
output.push({ name: 'argIsAddress', value: argIsAddress.toString() })
output.push({ name: 'argIsHex', value: argIsHex.toString() })
if (argIsAddress) {
try {
const encodedAddress = encodeAddress(decodeAddress(arg, false, ss58Format), ss58Format)
const decodedAddress = decodeAddress(encodedAddress, false, ss58Format)
if (encodedAddress === arg) {
const hexAddress = u8aToHex(decodedAddress)
output.push({ name: `Hex address`, value: hexAddress })
output.push({ name: `Address bytes`, value: decodedAddress.toString() })
} else {
output.push({ name: `Encoded address`, value: encodedAddress })
output.push({ name: `Address as hex`, value: u8aToHex(decodeAddress(encodedAddress)) })
}
} catch (e) {
output.push({ name: `Failure encoding/decoding address`, value: `FAIL - ${e}` })
}
}
if (argIsHex) {
output.push({ name: `Decoding hex to string`, value: hexToString(arg) })
try {
output.push({ name: `Decoding hex to number`, value: hexToNumber(arg).toString() })
} catch (e) {
output.push({ name: `Decoding hex to number`, value: `FAIL - ${e}` })
}
output.push({ name: `Decoding string to hex to u8a`, value: hexToU8a(stringToHex(arg)).toString() })
} else {
output.push({ name: `Hashing string using blake2AsHex`, value: blake2AsHex(arg) })
}

if (isJSON(arg)) {
const u8aMaybe = JSON.parse(arg)
output.push({ name: `Found JSON`, value: u8aMaybe })
// pad the array
const padded = new Uint8Array(new Uint8ClampedArray(u8aMaybe))
padded.set(u8aMaybe)
output.push({ name: `uint8array`, value: padded.toString() })

const hex = u8aToHex(padded)
output.push({ name: `u8aToHex`, value: u8aToHex(padded) })
output.push({ name: `encodeAddress(_, ss58Format)`, value: encodeAddress(hex, ss58Format) })
output.push({ name: `u8aToString`, value: u8aToString(padded) })
}

console.log('\nTABLE OUTPUT\n')
consoleTableWithWrapping(output)
console.log('\nJSON OUTPUT\n')
console.log(JSON.stringify(output, null, 4))
}

main()
Loading