* of three derived classes: SendBlock, ReceiveBlock, ChangeBlock.
*/
class Block {
+ /**
+ * Validates block data.
+ *
+ * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate
+ */
+ static #validate (block: unknown): asserts block is Block {
+ if (typeof block !== 'object') {
+ throw new TypeError('Invalid block')
+ }
+ const b = block as { [key: string]: unknown }
+ if (b.account == null) {
+ throw new Error('Account missing')
+ }
+ if (b.previous == null || b.previous === '') {
+ throw new Error('Frontier missing')
+ }
+ if (b.representative == null) {
+ throw new Error('Representative missing')
+ }
+ if (b.balance == null) {
+ throw new Error('Balance missing')
+ }
+ if (typeof b.balance !== 'number' && typeof b.balance !== 'bigint') {
+ throw new TypeError('Balance must be number or bigint')
+ }
+ if (b.balance < 0) {
+ throw new Error('Negative balance')
+ }
+ if (b.subtype === 'send') {
+ if (b.link == null || b.link === '') {
+ throw new Error('Recipient missing')
+ }
+ }
+ if (b.subtype === 'receive') {
+ if (b.link == null) {
+ throw new Error('Origin send block hash missing')
+ }
+ }
+ if (b.subtype === 'change') {
+ if (b.link == null) {
+ throw new Error('Change block link missing')
+ }
+ if (+(b.link) !== 0) {
+ throw new Error('Invalid change block link')
+ }
+ }
+ }
+
subtype?: 'send' | 'receive' | 'change'
account: Account
balance: bigint
* @returns {Promise<string>} Hash of the processed block
*/
async process (rpc: Rpc): Promise<string> {
- validate(this)
+ Block.#validate(this)
if (!this.signature) {
throw new Error('Block is missing signature. Use sign() and try again.')
}
}
}
}
-
-/**
- * Validates block data.
- *
- * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate
- */
-function validate (block: unknown): asserts block is Block {
- if (typeof block !== 'object') {
- throw new TypeError('Invalid block')
- }
- const b = block as { [key: string]: unknown }
- if (b.account == null) {
- throw new Error('Account missing')
- }
- if (b.previous == null || b.previous === '') {
- throw new Error('Frontier missing')
- }
- if (b.representative == null) {
- throw new Error('Representative missing')
- }
- if (b.balance == null) {
- throw new Error('Balance missing')
- }
- if (typeof b.balance !== 'number' && typeof b.balance !== 'bigint') {
- throw new TypeError('Balance must be number or bigint')
- }
- if (b.balance < 0) {
- throw new Error('Negative balance')
- }
- if (b.subtype === 'send') {
- if (b.link == null || b.link === '') {
- throw new Error('Recipient missing')
- }
- }
- if (b.subtype === 'receive') {
- if (b.link == null) {
- throw new Error('Origin send block hash missing')
- }
- }
- if (b.subtype === 'change') {
- if (b.link == null) {
- throw new Error('Change block link missing')
- }
- if (+(b.link) !== 0) {
- throw new Error('Invalid change block link')
- }
- }
-}