0.3.2 • Published 5 months ago

@aldea/wallet-lib v0.3.2

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
5 months ago

Wallet How-To

These are the basics on how to use the wallet.

Step 1: Choose your storage

We have 2 types of storage at the moment: LowDB and memory. Low db allows you to use several supports:

import { Memory } from "lowdb"
import { JSONFile, TextFile } from "lowdb/node"
import { LowDbStorage, LowDbData } from "@aldea/wallet-lib"

// lowdb + memory
const storage = new LowDbStorage(new Memory<LowDbData>())
// lowdb + jsonfile
const storage = new LowDbStorage(new JSONFile<LowDbData>('path/to/my/json/file.json'))
// lowdb + textfile
const storage = new LowDbStorage(new JSONFile<LowDbData>('path/to/my/file.txt'))

And memory (transient storage):

import { LowDbStorage, MemoryStorage } from "@aldea/wallet-lib"

const storage = new MemoryStorage()

Step 2: Create a wallet instance

Again, 2 options: Single key wallet and HDWallet

import { SingleKeyWallet, HdWallet } from "@aldea/wallet-lib"
import { Aldea, HDPrivKey, PrivKey } from "@aldea/sdk"

const pk = PrivKey.fromRandom()
const aldea = new Aldea()
const wallet1 = new SingleKeyWallet(pk, aldea, storage)
const wallet2 = new HdWallet(storage, aldea, hdPriv)

Step 3: Mint some coins

const kyResponse = await aldea.api.post('mint', { 
  headers: { 
    'Content-Type': 'application/json' 
  }, 
  body: JSON.stringify({ 
    address: await wallet.getNextAddress().then(a => a.toString()), 
    amount: 500 
  })
})

const output = Output.fromJson(await kyResponse.json())
await wallet.addUtxo(output)

Step 4: Do some txs

There is a wraper around aldea.createTx that signs, fund and broadcast

const response = await wallet.fundSignAndBroadcastTx(async builder => {
  const pkgIdx = builder.import('ea9225bcf8572c3a9fa75d186b62ab976d017d96b0614612f59d5fa5087b7fa3')
  const nftIdx = builder.new(pkgIdx, 'NFT', ['name', 32, 'moreName'])
  builder.lock(nftIdx, await wallet.getNextAddress())
})

Another option is doing this by hand:

const tx = aldea.createTx(async builder => {
  const pkgIdx = builder.import('ea9225bcf8572c3a9fa75d186b62ab976d017d96b0614612f59d5fa5087b7fa3')
  const nftIdx = builder.new(pkgIdx, 'NFT', ['name', 32, 'moreName'])
  builder.lock(nftIdx, await wallet.getNextAddress())
  await wallet.fundTx(builder)
  await wallet.signTx(builder)
})

const commitResponse = await aldea.commitTX(tx)
await wallet.processTx(tx, commitResponse.output.map(Output.fromJson))

Step 5: Viewing Your Balance

const utxos = wallet.getInventory()
const coins = utxos.filter(u => u.classPtr.id === new Array(32).fill(0).join(''))
0.3.2

5 months ago

0.3.0

6 months ago

0.3.1

6 months ago

0.2.11

6 months ago

0.2.10

7 months ago

0.2.9

8 months ago

0.2.8

10 months ago

0.2.7

11 months ago

0.2.1

12 months ago

0.2.6

11 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.5

11 months ago

0.2.4

12 months ago

0.2.0

12 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago

0.0.1-beta.4

1 year ago

0.0.1-beta.3

1 year ago

0.0.1-beta.2

1 year ago

0.0.1-beta.1

1 year ago