balance: bigint
previous: string
representative: Account
- link?: string
+ link?: Uint8Array<ArrayBuffer>
signature?: string
work?: string
/**
* Calculates the block hash using Blake2b.
*
- * @returns {Promise<string>} Block data hashed to a byte array
+ * @returns {Uint8Array<ArrayBuffer>} Block data hashed to a byte array
*/
- get hash (): string {
+ get hash (): Uint8Array<ArrayBuffer> {
try {
if (this.link == null) {
throw new Error('Block link is required')
this.previous.padStart(64, '0'),
this.representative.publicKey,
dec.toHex(this.balance, 32),
- this.link.padStart(64, '0')
+ this.link
]
const hash = new Blake2b(32)
- data.forEach(str => hash.update(hex.toBytes(str)))
- return hash.digest('hex').toUpperCase()
+ data.forEach(d => typeof d === 'string' ? hash.update(hex.toBytes(d)) : d)
+ return hash.digest()
} catch (err) {
console.error(err)
throw new Error('Failed to hash block', { cause: err })
"previous": this.previous,
"representative": this.representative.address ?? '',
"balance": this.balance.toString(),
- "link": this.link,
+ "link": bytes.toHex(this.link),
"signature": this.signature ?? '',
"work": this.work ?? ''
}
}
try {
this.subtype = 'change'
- this.link = Account.import(BURN_ADDRESS).publicKey
+ this.link = hex.toBytes(Account.import(BURN_ADDRESS).publicKey)
return this
} catch (err) {
this.subtype = undefined
throw new TypeError('Invalid subtype')
}
this.link = (typeof send === 'string')
- ? send
+ ? hex.toBytes(send)
: send.hash
return this
} catch (err) {
throw new TypeError('Invalid input')
} else if (typeof input === 'string') {
- const sig = await NanoNaCl.detached(hex.toBytes(this.hash), hex.toBytes(input))
+ const sig = await NanoNaCl.detached(this.hash, hex.toBytes(input))
this.signature = bytes.toHex(sig)
return this
}
case 'send': {
this.link = (typeof account === 'string')
- ? Account.import(account).publicKey
- : account.publicKey
+ ? hex.toBytes(Account.import(account).publicKey)
+ : hex.toBytes(account.publicKey)
break
}
default: {
throw new Error('Provide a key for block signature verification.')
}
try {
- return await NanoNaCl.verify(hex.toBytes(this.hash), hex.toBytes(this.signature ?? ''), hex.toBytes(key))
+ return await NanoNaCl.verify(this.hash, hex.toBytes(this.signature ?? ''), hex.toBytes(key))
} catch (err) {
throw new Error(`Failed to derive public key from private key`, { cause: err })
}