import { base32, bytes, hex, utf8 } from './convert'\r
import { Rpc } from './rpc'\r
import { NanoNaClWorker, SafeWorker } from '#workers'\r
+import { ChangeBlock, ReceiveBlock, SendBlock } from './block'\r
\r
/**\r
* Represents a single Nano address and the associated public key. To include the\r
this.#weight = BigInt(weight)\r
}\r
\r
+ /**\r
+ * Signs a block using the private key of the account.\r
+ *\r
+ * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block -\r
+ */\r
+ async sign (block: ChangeBlock | ReceiveBlock | SendBlock): Promise<string> {\r
+ if (this.isLocked || this.#prv.buffer.detached) {\r
+ throw new Error('Failed to sign block with locked Account')\r
+ }\r
+ try {\r
+ const headers = {\r
+ method: 'detached'\r
+ }\r
+ const data = {\r
+ privateKey: new Uint8Array(this.#prv).buffer,\r
+ msg: hex.toBytes(block.hash).buffer\r
+ }\r
+ return await NanoNaClWorker.add(headers, data)\r
+ } catch (err) {\r
+ throw new Error(`Failed to sign block`, { cause: err })\r
+ }\r
+ }\r
+\r
/**\r
* Unlocks the account using the same password as used prior to lock it.\r
*\r
*/
abstract class Block {
account: Account
- type: string = 'state'
+ type: 'state' = 'state'
abstract subtype: 'send' | 'receive' | 'change'
abstract previous: string
abstract representative: Account
}
this.signature = result.signature
} else {
- const key = input ?? this.account.privateKey
- if (key == null) {
- throw new Error('No valid key found to sign block')
- }
- const account = await Account.fromPrivateKey(key)
try {
- const headers = {
- method: 'detached'
- }
- const data = {
- privateKey: hex.toBytes(account.privateKey).buffer,
- msg: hex.toBytes(this.hash).buffer
- }
- this.signature = await NanoNaClWorker.add(headers, data)
+ const account = (typeof input === 'string')
+ ? await Account.fromPrivateKey(input)
+ : this.account
+ this.signature = await account.sign(this)
} catch (err) {
throw new Error(`Failed to sign block`, { cause: err })
}