0.3.9 • Published 2 years ago

@via-org/arweave-client v0.3.9

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

An Arweave client implementation written for Via

Features

  • Small: Under 7KB minified + gzipped
  • Isomorphic: zero reliance on Node built-ins
  • Versatile: Support for ANS-204 compliant bundles

Installation

npm i @via-org/arweave-client
# or
yarn add @via-org/arweave-client

Usage

Creating and submitting transactions
import { readFileSync } from 'fs'
import { Arweave } from '@via-org/arweave-client'

// Import your wallet
const key = JSON.parse(readFileSync('./key.json').toString())

// Instantiate the client
const arweave = new Arweave('https://arweave.net') // or new Arweave({ protocol: 'https', host: 'arweave.net' })

// Get transaction params ready
const data = JSON.stringify({ hello: 'world' })
const tags = [
  ['App-Name', 'Via'],
  ['Content-Type', 'application/json'],
]

// Create transaction
const transaction = await arweave.transactions.create({ data, tags }, key)
assert(/^[a-z0-9-_]{43}$/i.test(transaction.id))

// Post transaction
const status = await arweave.transactions.post(transaction)
assert(status === 'PENDING' || status.number_of_confirmations > 0)
Creating, submitting, and unpacking bundles
// Get data items ready
const itemA = {
  data: JSON.stringify({ testing: true }),
  tags: [['Hello', 'World']],
}
const itemB = {
  data: JSON.stringify({ testing: 123 }),
  tags: [['Sup', 'Earth']],
}

// Create bundle
const bundle = await arweave.bundles.create([itemA, itemB], key)
assert(bundle.items.length === 2)

// Post bundle
const status = await arweave.bundles.post(bundle)
assert(status === 'PENDING' || status.number_of_confirmations > 0)

// Unpack bundle
const unpackedBundle = arweave.bundles.unpack(bundle.data)
assert.deepEqual(bundle, unpackedBundle)
Fetching transaction data
// Fetch the price of transaction data as a winston string
const price = await arweave.transactions.getPrice(bundle.data)

// Fetch the status of an Arweave transaction
const status = await arweave.transactions.getStatus(transaction.id)

// Fetch the associated offset data of an Arweave transaction
const offset = await arweave.transactions.getOffset(transaction.id)

// Fetch Arweave data by transaction ID
const data = await client.transactions.getData(transaction.id)

// Fetch (optimistically) cached Arweave data by transaction ID
const data = await client.transactions.getCachedData(transaction.id)
Utilities
import { Wallet } from '@via-org/arweave-client/utilities'

// Generate a wallet
const { address, key } = await Wallet.generate()