signature?: string
work?: string
- constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string, work?: string)
- constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown, work: unknown) {
+ constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string)
+ constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown) {
try {
if (typeof account === 'string') {
account = Account.import(account)
if (typeof signature === 'string') {
this.signature = signature
}
- if (typeof work === 'string') {
- this.work = work
- }
} catch (err) {
throw new Error('Failed to initialize Block', { cause: err })
}
*
* A successful response sets the `work` property.
*/
- async pow (): Promise<Block> {
+ async pow (work?: string): Promise<Block>
+ async pow (work: unknown): Promise<Block> {
const difficulty: bigint = (this.subtype === 'send' || this.subtype === 'change')
? DIFFICULTY_SEND
: DIFFICULTY_RECEIVE
- const result = await NanoPow.work_generate(this.previous, { difficulty })
- if ('error' in result) {
- throw new Error('Failed to generate work', { cause: result.error })
+ if (work == null) {
+ const result = await NanoPow.work_generate(this.previous, { difficulty })
+ if ('error' in result) {
+ throw new Error('Failed to generate work', { cause: result.error })
+ }
+ work = result.work
+ }
+ if (typeof work !== 'string') {
+ throw new TypeError('Invalid work')
}
- const check = await NanoPow.work_validate(result.work, this.previous, { difficulty })
+ const check = await NanoPow.work_validate(work, this.previous, { difficulty })
if ('error' in check) {
throw new Error('Failed to validate work generated', { cause: check.error })
}
if (check.valid === '0') {
- throw new Error('Wwork generated is invalid', { cause: `${check.difficulty} < ${difficulty}` })
+ throw new Error('Invalid work generated', { cause: `${check.difficulty} < ${difficulty}` })
}
- this.work = result.work
+ this.work = work
return this
}
const testWallet = await Wallet.import('BIP-44', '', secret)\r
await testWallet.unlock('')\r
const testAccount = await testWallet.account(0)\r
- const testOpenBlock = new Block(\r
- testAccount.address,\r
- '0',\r
- testAccount.address,\r
- '0',\r
- testAccount.address,\r
- '0'\r
- )\r
+ const testOpenBlock = new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address,)\r
+ .receive('0')\r
+ .from(testAccount.publicKey)\r
await testWallet.sign(0, testOpenBlock)\r
const testSendBlock = new Block(testAccount.address, '0', bytes.toHex(testOpenBlock.hash), testAccount.address)\r
.send(0)\r
if (!(block.link instanceof Uint8Array)) {\r
throw new TypeError('Invalid block link')\r
}\r
+ if (!(block.representative instanceof Account)) {\r
+ throw new TypeError('Invalid block link')\r
+ }\r
if (!block.signature) {\r
throw new ReferenceError('Cannot cache unsigned block')\r
}\r
if (!(block.link instanceof Uint8Array)) {\r
throw new TypeError('Invalid block link')\r
}\r
+ if (!(block.representative instanceof Account)) {\r
+ throw new TypeError('Invalid block representative')\r
+ }\r
\r
const account = dec.toBytes(index + HARDENED_OFFSET, 4)\r
const previous = hex.toBytes(block.previous, 32)\r
import { Account } from './account'
import { Blake2b } from './blake2b'
-import { SendBlock } from './block'
+import { Block } from './block'
import { UNITS } from './constants'
import { bytes, hex } from './convert'
import { Ledger } from './ledger'
const recipientAccount = Account.import(recipient)
const accounts = await wallet.refresh(rpc, from, to)
for (const account of accounts) {
- if (account.representative?.address && account.frontier) {
- const block = new SendBlock(
- account,
- account.balance?.toString() ?? '0',
- recipientAccount.address,
- account.balance?.toString() ?? '0',
- account.representative.address,
- account.frontier
- )
+ if (account.representative?.address && account.frontier && account.index) {
+ const block = await (await new Block(account)
+ .send(account.balance ?? 0n)
+ .to(recipientAccount)
+ .pow())
+ .sign(wallet, account.index)
const blockRequest: Promise<void> = new Promise(async (resolve) => {
try {
- await block.pow()
- await block.sign()
const hash = await block.process(rpc)
results.push({ status: 'success', address: block.account.address, message: hash })
} catch (err: any) {
account: Account
balance: bigint
previous: string
- representative: Account
+ representative?: Account
link?: Uint8Array<ArrayBuffer>
signature?: string
work?: string
- constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string, work?: string)
+ constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string)
/**
* Calculates the block hash using Blake2b.
*
*
* A successful response sets the `work` property.
*/
- pow (): Promise<Block>
+ pow (work?: string): Promise<Block>
/**
* Sends the block to a node for processing on the network.
*