From: Chris Duncan Date: Tue, 31 Mar 2026 10:29:06 +0000 (-0700) Subject: Use TypeScript Record convention for keyed objects. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=fab0f9e265abcbfc77e5bf5f50c3d04aae84430e;p=libnemo.git Use TypeScript Record convention for keyed objects. --- diff --git a/src/lib/account/index.ts b/src/lib/account/index.ts index cedc753..969d3bf 100644 --- a/src/lib/account/index.ts +++ b/src/lib/account/index.ts @@ -370,7 +370,7 @@ export class Account { static #isKeyPair (input: unknown): input is KeyPair { if (typeof input === 'object') { - const obj = input as { [key: string]: unknown } + const obj = input as Record if ('index' in obj && typeof obj.index === 'number') { return true } diff --git a/src/lib/block.ts b/src/lib/block.ts index b919a00..d2fb261 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -25,7 +25,7 @@ export class Block { if (typeof block !== 'object') { throw new TypeError('Invalid block') } - const b = block as { [key: string]: unknown } + const b = block as Record if (b.account == null) { throw new Error('Account missing') } @@ -157,7 +157,7 @@ export class Block { * * @returns {string} JSON representation of the block */ - toJSON (): { [key: string]: string } { + toJSON (): Record { try { if (this.link == null) { throw new Error('Link is required') diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 7cbe29a..3358245 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -23,7 +23,7 @@ export const DIFFICULTY_RECEIVE = 0xfffffe0000000000n export const DIFFICULTY_SEND = 0xfffffff800000000n export const XNO = 'Ó¾' -export const UNITS: { [key: string]: number } = Object.freeze({ +export const UNITS: Record = Object.freeze({ RAW: 0, RAI: 24, NYANO: 24, diff --git a/src/lib/crypto/wallet-aes-gcm.ts b/src/lib/crypto/wallet-aes-gcm.ts index 0880fc9..b0825d7 100644 --- a/src/lib/crypto/wallet-aes-gcm.ts +++ b/src/lib/crypto/wallet-aes-gcm.ts @@ -6,7 +6,7 @@ export class WalletAesGcm { static encoder: TextEncoder = new TextEncoder() - static decrypt (type: string, key: CryptoKey, iv: ArrayBuffer, encrypted: ArrayBuffer): Promise<{ [key: string]: ArrayBuffer }> { + static decrypt (type: string, key: CryptoKey, iv: ArrayBuffer, encrypted: ArrayBuffer): Promise> { const seedLength = type === 'BLAKE2b' ? 32 : 64 const additionalData = this.encoder.encode(type) return crypto.subtle @@ -19,7 +19,7 @@ export class WalletAesGcm { }) } - static encrypt (type: string, key: CryptoKey, seed: ArrayBuffer, mnemonic?: ArrayBuffer): Promise<{ [key: string]: ArrayBuffer }> { + static encrypt (type: string, key: CryptoKey, seed: ArrayBuffer, mnemonic?: ArrayBuffer): Promise> { if (type == null) { throw new Error('Wallet type missing') } diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index b93ef4b..13ceaea 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -48,7 +48,7 @@ export class Ledger { static #queue: { task: Function, resolve: Function, reject: Function }[] = [] static #status: LedgerStatus = 'DISCONNECTED' static #transport: typeof TransportHID | typeof TransportBLE | typeof TransportUSB - static #ADPU_CODES: { [key: string]: number } = Object.freeze({ + static #ADPU_CODES: Record = Object.freeze({ class: 0xa1, bip32DerivationLevel: 0x03, version: 0x01, diff --git a/src/lib/rpc.ts b/src/lib/rpc.ts index 404c5a1..36f860b 100644 --- a/src/lib/rpc.ts +++ b/src/lib/rpc.ts @@ -29,10 +29,10 @@ export class Rpc { * @param {object} [data] - JSON to send to the node as defined by the action * @returns {Promise} JSON-formatted RPC results from the node */ - async post (action: string, data?: { [key: string]: unknown }): Promise { + async post (action: string, data?: Record): Promise { var process: any = process || null this.#validate(action) - const headers: { [key: string]: string } = {} + const headers: Record = {} headers['Content-Type'] = 'application/json' if (this.#n && process?.env?.LIBNEMO_RPC_API_KEY) { headers[this.#n] = process.env.LIBNEMO_RPC_API_KEY diff --git a/src/lib/vault/passkey.ts b/src/lib/vault/passkey.ts index 254a3ce..3cc5d02 100644 --- a/src/lib/vault/passkey.ts +++ b/src/lib/vault/passkey.ts @@ -2,7 +2,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later export class Passkey { - static create (action: string, salt: ArrayBuffer, data: { [key: string]: unknown }): Promise { + static create (action: string, salt: ArrayBuffer, data: Record): Promise { // Allowlisted wallet actions if (['create', 'load', 'unlock', 'update'].includes(action)) { diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index c980c2d..075a8df 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -384,7 +384,7 @@ function verify (seed?: ArrayBuffer, mnemonicPhrase?: string): Record) { try { // Import requires seed or mnemonic phrase if (action === 'load' && data.seed == null && data.mnemonicPhrase == null) { @@ -556,7 +556,7 @@ function _load (type?: 'BIP-44' | 'BLAKE2b' | 'Exodus', key?: CryptoKey, keySalt } // Action for selecting method execution -function _parseAction (data: { [key: string]: unknown }) { +function _parseAction (data: Record) { if (data.action == null) { throw new TypeError('Wallet action is required') } @@ -588,7 +588,7 @@ function _parseData (data: unknown): Record { // Salt created to derive CryptoKey from password; subsequently required to // derive the same key for unlock requests -function _parseKeySalt (action: string, data: { [key: string]: unknown }): ArrayBuffer { +function _parseKeySalt (action: string, data: Record): ArrayBuffer { if (action === 'unlock') { if (data.keySalt instanceof ArrayBuffer) { return data.keySalt @@ -602,7 +602,7 @@ function _parseKeySalt (action: string, data: { [key: string]: unknown }): Array // Initialization vector created to encrypt and lock the vault; subsequently // required to decrypt and unlock the vault -function _parseIv (action: string, data: { [key: string]: unknown }) { +function _parseIv (action: string, data: Record): ArrayBuffer | undefined { if (action === 'unlock') { if (!(data.iv instanceof ArrayBuffer)) { throw new TypeError('Initialization vector required to unlock wallet') @@ -614,7 +614,7 @@ function _parseIv (action: string, data: { [key: string]: unknown }) { } // Algorithm used for wallet functions -function _parseType (action: string, data: { [key: string]: unknown }) { +function _parseType (action: string, data: Record) { if (['create', 'load', 'unlock'].includes(action)) { if (data.type !== 'BIP-44' && data.type !== 'BLAKE2b' && data.type !== 'Exodus' && data.type !== 'Ledger') { throw new TypeError(`Type is required to ${action} wallet`) diff --git a/src/lib/wallet/config.ts b/src/lib/wallet/config.ts index 286c060..ad00e1a 100644 --- a/src/lib/wallet/config.ts +++ b/src/lib/wallet/config.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { WalletType } from '.' import { Ledger } from '../ledger' import { Vault } from '../vault' -import { WalletType } from '.' export async function _config (type: WalletType, vault: Vault, settings: { connection: 'hid' | 'ble' | 'usb' } | { timeout: number }): Promise export async function _config (type: WalletType, vault: Vault, settings: unknown): Promise { @@ -11,7 +11,7 @@ export async function _config (type: WalletType, vault: Vault, settings: unknown if (settings == null || typeof settings !== 'object') { throw new TypeError('Invalid configuration settings') } - const { connection, timeout } = settings as { [key: string]: unknown } + const { connection, timeout } = settings as Record if (type === 'Ledger') { if (connection !== undefined && connection !== 'hid' && connection !== 'ble' && connection !== 'usb') { throw new Error('Ledger connection must be hid, ble, or usb', { cause: connection }) diff --git a/src/lib/wallet/verify.ts b/src/lib/wallet/verify.ts index 02d7baa..55c4d83 100644 --- a/src/lib/wallet/verify.ts +++ b/src/lib/wallet/verify.ts @@ -15,7 +15,7 @@ export async function _verify (type: WalletType, vault: Vault, secret: unknown): if (type === 'Ledger') { return await Ledger.verify(secret) } else { - const data: { [key: string]: string | ArrayBuffer } = { + const data: Record = { action: 'verify' } if (/^(?:[A-F0-9]{64}){1,2}$/i.test(secret)) {