Skip to content

Commit

Permalink
chore: setup package.json for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakia committed Jun 18, 2024
1 parent 67ebd38 commit d2f3190
Show file tree
Hide file tree
Showing 4 changed files with 1,665 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ yarn-error.log
!.yarn/releases
!.yarn/sdks
!.yarn/versions
cli/.pnp.*
cli/.yarn/*
!cli/.yarn/patches
!cli/.yarn/plugins
!cli/.yarn/releases
!cli/.yarn/sdks
!cli/.yarn/versions

# App specific
#
Expand Down
156 changes: 146 additions & 10 deletions cli/index.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,159 @@
#!/usr/bin/env node

import yargs from 'yargs'
import crypto from 'crypto'
import { hideBin } from 'yargs/helpers'
import inquirer from 'inquirer'
import process from 'process'

import sign_nano_community_link_key from '#common/sign-nano-community-link-key.mjs'
import sign_nano_community_revoke_key from '#common/sign-nano-community-revoke-key.mjs'
import sign_nano_community_message from '#common/sign-nano-community-message.mjs'
import encode_nano_address from '#common/encode-nano-address.mjs'
import request from '#common/request.mjs'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import inquirer from 'inquirer'
import fetch, { Request } from 'node-fetch'
import ed25519 from '@trashman/ed25519-blake2b'

const is_test = process.env.NODE_ENV === 'test'

const base_url = is_test ? 'http://localhost:8080' : 'https://nano.community'

const load_private_key = async () => {
function sign_nano_community_link_key({
linked_public_key,
nano_account,
nano_account_private_key,
nano_account_public_key
}) {
if (!linked_public_key) {
throw new Error('linked_public_key is required')
}

if (!nano_account) {
throw new Error('nano_account is required')
}

if (!nano_account_private_key) {
throw new Error('nano_account_private_key is required')
}

if (!nano_account_public_key) {
throw new Error('nano_account_public_key is required')
}

const data = Buffer.from(['LINK', nano_account, linked_public_key])

const message_hash = ed25519.hash(data)

return ed25519.sign(
message_hash,
nano_account_private_key,
nano_account_public_key
)
}

function sign_nano_community_revoke_key({
linked_public_key,
either_private_key,
either_public_key
}) {
if (!linked_public_key) {
throw new Error('linked_public_key is required')
}

if (!either_private_key) {
throw new Error('either_private_key is required')
}

if (!either_public_key) {
throw new Error('either_public_key is required')
}

const data = Buffer.from(['REVOKE', linked_public_key])

const message_hash = ed25519.hash(data)

return ed25519.sign(message_hash, either_private_key, either_public_key)
}

function sign_nano_community_message(message, private_key) {
const {
entry_id,
chain_id,
entry_clock,
chain_clock,
public_key,
operation,
content,
tags,
references,
created_at
} = message

const data = Buffer.from([
entry_id,
chain_id,
entry_clock,
chain_clock,
public_key,
operation,
content,
tags,
references,
created_at
])

const message_hash = ed25519.hash(data)

return ed25519.sign(message_hash, private_key, public_key)
}

function encode_nano_base32(view) {
const length = view.length
const leftover = (length * 8) % 5
const offset = leftover === 0 ? 0 : 5 - leftover
const alphabet = '13456789abcdefghijkmnopqrstuwxyz'

let value = 0
let output = ''
let bits = 0

for (let i = 0; i < length; i++) {
value = (value << 8) | view[i]
bits += 8

while (bits >= 5) {
output += alphabet[(value >>> (bits + offset - 5)) & 31]
bits -= 5
}
}

if (bits > 0) {
output += alphabet[(value << (5 - (bits + offset))) & 31]
}

return output
}

function encode_nano_address({ public_key_buf, prefix = 'nano_' }) {
const encoded_public_key = encode_nano_base32(public_key_buf)
const checksum = ed25519.hash(public_key_buf, 5).reverse()
const encoded_checksum = encode_nano_base32(checksum)
return prefix + encoded_public_key + encoded_checksum
}

async function request(options) {
const request = new Request(options.url, {
timeout: 20000,
...options
})
const response = await fetch(request)

if (response.status >= 200 && response.status < 300) {
return response.json()
} else {
const res = await response.json()
const error = new Error(res.error || response.statusText)
error.response = response
throw error
}
}

async function load_private_key() {
let private_key = process.env.NC_CLI_NANO_PRIVATE_KEY
if (private_key) {
console.log('Private key found in environment variable.')
Expand Down Expand Up @@ -166,7 +302,7 @@ const update_block_meta = {
await send_message_handler('update-block-meta', block_hash)
}

const send_message_handler = async (type, block_hash = null) => {
async function send_message_handler(type, block_hash = null) {
const { private_key, public_key } = await load_private_key()

let message_content_prompts = []
Expand Down
28 changes: 28 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "nano-community-cli",
"version": "0.0.1",
"description": "CLI for Nano.Community Messages",
"main": "index.mjs",
"bin": {
"nano-community": "./index.mjs"
},
"keywords": [
"nano.community",
"nano",
"crypto",
"blockchain",
"cli"
],
"author": "Nano Community",
"license": "MIT",
"dependencies": {
"@trashman/ed25519-blake2b": "0.0.6",
"inquirer": "9.2.23",
"node-fetch": "3.3.2",
"yargs": "17.7.2"
},
"engines": {
"node": ">=16.0.0"
},
"packageManager": "[email protected]"
}
Loading

0 comments on commit d2f3190

Please sign in to comment.