Skip to content

Commit

Permalink
refactor(seed): rework almost all seed scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBuhler committed Mar 13, 2024
1 parent 150e9e1 commit 8c78bb7
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 43 deletions.
15 changes: 4 additions & 11 deletions seed/create-drive.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#!/usr/bin/env node

import { writeFile } from 'node:fs/promises'
import { join } from 'node:path'

import { arDriveFactory, JWKWallet } from 'ardrive-core-js'

import { instance as arweave } from './utils/arweaveInstance.mjs'
import { loadWallet } from './utils/loadWallet.mjs'

const arDrive = arDriveFactory({
arweave,
wallet: new JWKWallet(await loadWallet()),
})
import { arDrive } from './utils/ardrive.mjs'

const { created } = await arDrive.createPublicDrive({
driveName: 'Test Drive'
driveName: 'Test Drive',
})

const driveJson = JSON.stringify(created, null, 2)

await writeFile('drive.json', driveJson)
await writeFile(join('extras', 'drive.json'), driveJson)

const txId = created.find(x => x.type === 'bundle').bundleTxId
console.log(`txId: ${txId}`)
Expand Down
2 changes: 2 additions & 0 deletions seed/extras/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
!/README.md
3 changes: 3 additions & 0 deletions seed/extras/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# extras

Some seed scripts may write intermediate data files into this folder.
13 changes: 0 additions & 13 deletions seed/generate-wallet.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion seed/mine.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { arlocalUrl } from './utils/arweaveInstance.mjs'
import { arlocalUrl } from './utils/arweave.mjs'

const qty = process.argv[2] || '1'

Expand Down
2 changes: 1 addition & 1 deletion seed/mint.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { arlocalUrl } from './utils/arweaveInstance.mjs'
import { arlocalUrl } from './utils/arweave.mjs'
import { getWalletAddress } from './utils/getWalletAddress.mjs'

const amount = process.argv[2] || '1000000000'
Expand Down
6 changes: 0 additions & 6 deletions seed/package.json

This file was deleted.

49 changes: 49 additions & 0 deletions seed/publish-scheduler-location.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node

import { readFile } from 'node:fs/promises'

import { instance as arweave } from './utils/arweave.mjs'

const wallet = JSON.parse(await readFile(import.meta.resolve('../wallets/scheduler-location-publisher-wallet.json').slice(7), 'utf8'))
const address = await arweave.wallets.getAddress(wallet)

console.log('address', address)

const tx = await arweave.createTransaction(
{
data: Math.random.toString().slice(-4),
tags: [
{
name: 'Data-Protocol',
value: 'ao',
},
{
name: 'Type',
value: 'Scheduler-Location',
},
{
name: 'Variant',
value: 'ao.LN.1',
},
{
name: 'URL',
value: 'http://localhost:4003',
},
{
name: 'Time-To-Live',
value: '86400',
},
].map(({ name, value }) => ({
name: Buffer.from(name).toString('base64'),
value: Buffer.from(value).toString('base64'),
})),
},
)

await arweave.transactions.sign(tx, wallet)

console.log(tx)

const res = await arweave.transactions.post(tx)

console.log(`${res.status} ${res.statusText}`)
2 changes: 1 addition & 1 deletion seed/reset.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { arlocalUrl } from './utils/arweaveInstance.mjs'
import { arlocalUrl } from './utils/arweave.mjs'

const url = `${arlocalUrl}/reset`

Expand Down
5 changes: 2 additions & 3 deletions seed/.gitignore → seed/utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/package.json

/package-lock.json
/pnpm-lock.yaml
/yarn.lock

/drive.json
/wallet.json
14 changes: 14 additions & 0 deletions seed/utils/ardrive.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import safeImport from './safeImport.mjs'
const { arDriveFactory, JWKWallet } = await safeImport('ardrive-core-js')

import { instance as arweave } from './arweave.mjs'
import { loadWallet } from './loadWallet.mjs'

export const arDrive = arDriveFactory({
arweave,
wallet: new JWKWallet(await loadWallet()),
// TODO: should we be using the turbo service?
// turboSettings: {
// turboUrl: 'http://localhost:4005',
// }
})
9 changes: 5 additions & 4 deletions seed/utils/arweaveInstance.mjs → seed/utils/arweave.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Arweave from 'arweave'
import safeImport from './safeImport.mjs'
const { default: Arweave } = await safeImport('arweave')

export const protocol = 'http'
export const host = 'localhost'
Expand All @@ -7,7 +8,7 @@ export const port = 4000
export const arlocalUrl = `${protocol}://${host}:${port}`

export const instance = new Arweave({
protocol: 'http',
host: 'localhost',
port: 4000,
protocol,
host,
port,
})
2 changes: 1 addition & 1 deletion seed/utils/getWalletAddress.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { instance as arweave } from './arweaveInstance.mjs'
import { instance as arweave } from './arweave.mjs'
import { loadWallet } from './loadWallet.mjs'

export async function getWalletAddress () {
Expand Down
7 changes: 5 additions & 2 deletions seed/utils/loadWallet.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'

export async function loadWallet () {
const json = await readFile('wallet.json', 'utf-8')
export async function loadWallet (filename = 'user-wallet.json') {
const __dirname = dirname(import.meta.url.slice(7))
const userWalletFile = join(__dirname, '..', '..', 'wallets', filename)
const json = await readFile(userWalletFile, 'utf-8')
return JSON.parse(json)
}
16 changes: 16 additions & 0 deletions seed/utils/safeImport.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { execFileSync } from 'node:child_process'
import { dirname } from 'node:path'

export default async function safeImport (module) {
try {
return await import(module);
} catch (e) {
console.log(`Installing '${module}' from npm...`)
execFileSync('npm', ['install', module], {
cwd: dirname(import.meta.url).slice(7),
encoding: 'utf-8',
})
console.log(`Installed '${module}'.`)
return await import(module);
}
}

0 comments on commit 8c78bb7

Please sign in to comment.