-
Notifications
You must be signed in to change notification settings - Fork 765
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve cli interface and add examples to readme * fix first example to use json string rather than number * organize code, better error output, add string input validation * re-add buffer hex encoding Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
73 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
#!/usr/bin/env node | ||
|
||
const rlp = require('../dist/index.js') | ||
const command = process.argv[2] | ||
var raw = process.argv[3] | ||
const method = process.argv[2] | ||
const strInput = process.argv[3] | ||
|
||
if (command === 'encode') { | ||
if (typeof strInput !== 'string') { | ||
return console.error(`Expected JSON string input, received type: ${typeof strInput}`) | ||
} | ||
|
||
if (method === 'encode') { | ||
// Parse JSON | ||
let json | ||
try { | ||
const json = JSON.parse(raw) | ||
console.log(rlp.encode(json).toString('hex')) | ||
} catch (e) { | ||
console.log('invalid json') | ||
} | ||
} else { | ||
if (!raw) { | ||
raw = command | ||
json = JSON.parse(strInput) | ||
} catch (error) { | ||
return console.error(`Error could not parse JSON: ${JSON.stringify(error)}`) | ||
} | ||
|
||
// Encode RLP | ||
try { | ||
console.log(baToJSON(rlp.decode(raw))) | ||
const encoded = rlp.encode(json) | ||
console.log('0x' + encoded.toString('hex')) | ||
} catch (e) { | ||
console.log('invalid RLP' + e) | ||
console.error(`Error encoding RLP: ${JSON.stringify(error)}`) | ||
} | ||
} else if (method === 'decode') { | ||
// Decode | ||
try { | ||
const decoded = rlp.decode(strInput) | ||
const json = JSON.stringify(baToJSON(decoded)) | ||
console.log(json) | ||
} catch (error) { | ||
console.error(`Error decoding RLP: ${JSON.stringify(error)}`) | ||
} | ||
} else { | ||
console.error('Unsupported method') | ||
} | ||
|
||
function baToJSON (ba) { | ||
/** | ||
* Buffer or Buffer Array to JSON | ||
*/ | ||
function baToJSON(ba) { | ||
if (Buffer.isBuffer(ba)) { | ||
return ba.toString('hex') | ||
} else if (ba instanceof Array) { | ||
var array = [] | ||
for (var i = 0; i < ba.length; i++) { | ||
array.push(baToJSON(ba[i])) | ||
const arr = [] | ||
for (let i = 0; i < ba.length; i++) { | ||
arr.push(baToJSON(ba[i])) | ||
} | ||
return array | ||
return arr | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters