Skip to content

Commit

Permalink
wip: refactor(agent): improve structure
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 8, 2024
1 parent c8389eb commit aff1524
Show file tree
Hide file tree
Showing 38 changed files with 1,726 additions and 1,610 deletions.
2 changes: 1 addition & 1 deletion fadroma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import { Suite } from '@hackbg/ensuite'
export default new Suite([
['agent', () => import('./packages/agent/agent.test')],
['agent', () => import('./packages/agent/index.test')],
['stores', () => import('./stores.test')],
['scrt', () => import('./packages/scrt/scrt.test')],
['cw', () => import('./packages/cw/cw.test')],
Expand Down
20 changes: 10 additions & 10 deletions fadroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export * from './fadroma.browser'

// And more!
import type { ChainId, CodeHash } from '@fadroma/agent'
import { Core, Chain, Program, Deploy, Store } from '@fadroma/agent'
import { Console, bold, timestamp, Chain, Store } from '@fadroma/agent'

Check failure on line 24 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

'"@fadroma/agent"' has no exported member named 'Store'. Did you mean 'str'?
import { getProject, ProjectPrompter } from '@fadroma/create'
import Commands from '@hackbg/cmds'
import { FileFormat } from '@hackbg/file'
Expand All @@ -30,7 +30,7 @@ import { SyncFS } from '@hackbg/file'
import { fileURLToPath } from 'node:url'
import { basename } from 'node:path'

const console = new Core.Console('@hackbg/fadroma')
const console = new Console('@hackbg/fadroma')

export default function main (...args: any) {
console.debug('Running main...')
Expand Down Expand Up @@ -174,7 +174,7 @@ export async function runScript (context?: { project?: Project, script?: string,
if (typeof main === 'function') {
return main(project, ...args||[])
} else {
console.error(`The default export of ${Core.bold(scriptPath.short)} is not a function`)
console.error(`The default export of ${bold(scriptPath.short)} is not a function`)
process.exit(1)
}
}
Expand Down Expand Up @@ -224,14 +224,14 @@ export function exportDeployment (
// If passed a directory, generate file name
const exportPath = new SyncFS.Path(path)
const exportFile = exportPath.isDirectory()
? new SyncFS.File(exportPath, `${deployment.name}_@_${Core.timestamp()}.json`)
? new SyncFS.File(exportPath, `${deployment.name}_@_${timestamp()}.json`)
: new SyncFS.File(exportPath)
// Serialize and write the deployment.
const state = deployment.serialize()
exportFile.setFormat(FileFormat.JSON).makeParent().save(state)
console.log(
'saved', Object.keys(state).length,
'contracts to', Core.bold(exportFile.short)
'contracts to', bold(exportFile.short)
)
}

Expand Down Expand Up @@ -260,12 +260,12 @@ export class JSONFileUploadStore extends Store.UploadStore {
const uploaded = receipt.load() as { codeId: string }
if (uploaded.codeId) {
this.log(
'loading code id', Core.bold(String(uploaded.codeId)),
'from', Core.bold(receipt.shortPath)
'loading code id', bold(String(uploaded.codeId)),
'from', bold(receipt.shortPath)
)
super.set(codeHash, uploaded)
} else {
this.log.warn('no codeId field found in', Core.bold(receipt.shortPath))
this.log.warn('no codeId field found in', bold(receipt.shortPath))
}
}
return super.get(codeHash)
Expand Down Expand Up @@ -310,9 +310,9 @@ export class JSONFileDeployStore extends Store.DeployStore {
const state = receipt.load()
this.log(
'loading code id',
Core.bold(name),
bold(name),
'from',
Core.bold(receipt.shortPath)
bold(receipt.shortPath)
)
super.set(name, state)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/agent.cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CLI = await import("./agent.dist.js").catch(async e=>{
new Console().debug('Compiling TypeScript...')
await import("@ganesha/esbuild")
const t0 = performance.now()
const module = await import("./agent.ts")
const module = await import("./commands.ts")
new Console().debug('Compiled TypeScript in', ((performance.now() - t0)/1000).toFixed(3)+'s')
return module
}).then(module=>module.default)
Expand Down
135 changes: 135 additions & 0 deletions packages/agent/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import CLI from '@hackbg/cmds'
import { bold, bech32, bech32m, randomBech32m, base16 } from './src/Util'

/** Base command line interface for Fadroma Agent. */
export default class AgentCLI extends CLI {

constructor (...args: ConstructorParameters<typeof CLI>) {
super(...args)
this.log.label = ``
}

bech32 = this.command({
name: 'random-bech32',
info: 'create a random bech32 address',
args: 'PREFIX [LENGTH]'
}, (prefix: string, length: string|number = "20") => {
if (!prefix) {
this.log.error(bold('Pass a prefix to generate a bech32 address'))
process.exit(1)
}
if (isNaN(Number(length))) {
this.log.error(bold(`"${length}" is not a number. Pass a valid length to generate a bech32 address.`))
process.exit(1)
}
this.log
.log(`${length} byte random bech32:`, bold(randomBech32m(prefix, Number(length))))
})

bech32m = this.command({
name: 'random-bech32m',
info: 'create a random bech32m address',
args: 'PREFIX [LENGTH]'
}, (prefix: string, length: string|number = "20") => {
if (!prefix) {
this.log.error(bold('Pass a prefix to generate a bech32m address'))
process.exit(1)
}
if (isNaN(Number(length))) {
this.log.error(bold(`"${length}" is not a number. Pass a valid length to generate a bech32m address.`))
process.exit(1)
}
this.log
.log(`${length} byte random bech32m:`, bold(randomBech32m(prefix, Number(length))))
})

bech32ToHex = this.command({
name: 'from-bech32',
info: 'convert a bech32 address to a hex string',
args: 'ADDRESS'
}, (address: string) => {
if (!address) {
this.log.error(bold('Pass an address to convert it to hexadecimal.'))
process.exit(1)
}
let prefix, words
try {
;({ prefix, words } = bech32.decode(address))
} catch (e) {
this.log.error(bold('Failed to decode this address.'))
this.log.error((e as any).message)
process.exit(1)
}
this.log
.info('Prefix: ', bold(prefix))
.info('Words: ', bold(base16.encode(new Uint8Array(words))))
.log('Original:', bold(base16.encode(new Uint8Array(bech32m.fromWords(words)))))
})

bech32mToHex = this.command({
name: 'from-bech32m',
info: 'convert a bech32m address to a hex string',
args: 'ADDRESS'
}, (address: string) => {
if (!address) {
this.log.error(bold('Pass an address to convert it to hexadecimal.'))
process.exit(1)
}
let prefix, words
try {
;({ prefix, words } = bech32m.decode(address))
} catch (e) {
this.log.error(bold('Failed to decode this address.'))
this.log.error((e as any).message)
process.exit(1)
}
this.log
.info('Prefix: ', bold(prefix))
.info('Words: ', bold(base16.encode(new Uint8Array(words))))
.log('Original:', bold(base16.encode(new Uint8Array(bech32m.fromWords(words)))))
})

hexToBech32 = this.command({
name: 'to-bech32',
info: 'convert a hex string to a bech32 address',
args: 'PREFIX DATA'
}, (prefix: string, data: string) => {
if (!prefix) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32'))
process.exit(1)
}
let dataBin
try {
dataBin = base16.decode(data.toUpperCase())
} catch (e) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32'))
process.exit(1)
}
this.log
.info('input: ', bold(data))
.log('bech32:', bold(bech32.encode(prefix, bech32.toWords(dataBin))))
})

hexToBech32m = this.command({
name: 'to-bech32m',
info: 'convert a hex string to a bech32m address',
args: 'PREFIX DATA'
}, (prefix: string, data: string) => {
if (!prefix) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32m'))
process.exit(1)
}
let dataBin
try {
dataBin = base16.decode(data.toUpperCase())
} catch (e) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32m'))
process.exit(1)
}
this.log
.info('input: ', bold(data))
.log('bech32m:', bold(bech32m.encode(prefix, bech32m.toWords(dataBin))))
})

}

151 changes: 3 additions & 148 deletions packages/agent/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,152 +20,7 @@

export * from './index'

import { _$_HACK_$_ } from './src/Agent'
import { LocalCompiledCode } from './src/Compute.node'
// Monkey patch for fetching from local FS. See docstring.
import { _$_HACK_$_ } from './src/compute/Upload'
import { LocalCompiledCode } from './src/compute/Compile.node'
_$_HACK_$_.CompiledCode = LocalCompiledCode

export * as Compute from './src/Compute'

import {
Console,
base16,
bech32,
bech32m,
randomBech32,
randomBech32m,
bold,
} from './src/Util'

import CLI from '@hackbg/cmds'

/** Base command line interface for Fadroma Agent. */
export default class AgentCLI extends CLI {

constructor (...args: ConstructorParameters<typeof CLI>) {
super(...args)
this.log.label = ``
}

bech32 = this.command({
name: 'random-bech32',
info: 'create a random bech32 address',
args: 'PREFIX [LENGTH]'
}, (prefix: string, length: string|number = "20") => {
if (!prefix) {
this.log.error(bold('Pass a prefix to generate a bech32 address'))
process.exit(1)
}
if (isNaN(Number(length))) {
this.log.error(bold(`"${length}" is not a number. Pass a valid length to generate a bech32 address.`))
process.exit(1)
}
this.log
.log(`${length} byte random bech32:`, bold(randomBech32m(prefix, Number(length))))
})

bech32m = this.command({
name: 'random-bech32m',
info: 'create a random bech32m address',
args: 'PREFIX [LENGTH]'
}, (prefix: string, length: string|number = "20") => {
if (!prefix) {
this.log.error(bold('Pass a prefix to generate a bech32m address'))
process.exit(1)
}
if (isNaN(Number(length))) {
this.log.error(bold(`"${length}" is not a number. Pass a valid length to generate a bech32m address.`))
process.exit(1)
}
this.log
.log(`${length} byte random bech32m:`, bold(randomBech32m(prefix, Number(length))))
})

bech32ToHex = this.command({
name: 'from-bech32',
info: 'convert a bech32 address to a hex string',
args: 'ADDRESS'
}, (address: string) => {
if (!address) {
this.log.error(bold('Pass an address to convert it to hexadecimal.'))
process.exit(1)
}
let prefix, words
try {
;({ prefix, words } = bech32.decode(address))
} catch (e) {
this.log.error(bold('Failed to decode this address.'))
this.log.error(e.message)
process.exit(1)
}
this.log
.info('Prefix: ', bold(prefix))
.info('Words: ', bold(base16.encode(new Uint8Array(words))))
.log('Original:', bold(base16.encode(new Uint8Array(bech32m.fromWords(words)))))
})

bech32mToHex = this.command({
name: 'from-bech32m',
info: 'convert a bech32m address to a hex string',
args: 'ADDRESS'
}, (address: string) => {
if (!address) {
this.log.error(bold('Pass an address to convert it to hexadecimal.'))
process.exit(1)
}
let prefix, words
try {
;({ prefix, words } = bech32m.decode(address))
} catch (e) {
this.log.error(bold('Failed to decode this address.'))
this.log.error(e.message)
process.exit(1)
}
this.log
.info('Prefix: ', bold(prefix))
.info('Words: ', bold(base16.encode(new Uint8Array(words))))
.log('Original:', bold(base16.encode(new Uint8Array(bech32m.fromWords(words)))))
})

hexToBech32 = this.command({
name: 'to-bech32',
info: 'convert a hex string to a bech32 address',
args: 'PREFIX DATA'
}, (prefix: string, data: string) => {
if (!prefix) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32'))
process.exit(1)
}
let dataBin
try {
dataBin = base16.decode(data.toUpperCase())
} catch (e) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32'))
process.exit(1)
}
this.log
.info('input: ', bold(data))
.log('bech32:', bold(bech32.encode(prefix, bech32.toWords(dataBin))))
})

hexToBech32m = this.command({
name: 'to-bech32m',
info: 'convert a hex string to a bech32m address',
args: 'PREFIX DATA'
}, (prefix: string, data: string) => {
if (!prefix) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32m'))
process.exit(1)
}
let dataBin
try {
dataBin = base16.decode(data.toUpperCase())
} catch (e) {
this.log.error(bold('Pass a prefix and a valid hex string to generate bech32m'))
process.exit(1)
}
this.log
.info('input: ', bold(data))
.log('bech32m:', bold(bech32m.encode(prefix, bech32m.toWords(dataBin))))
})

}
Loading

0 comments on commit aff1524

Please sign in to comment.