Skip to content

Commit 09a2277

Browse files
committed
fix: replace BufferSource with Uint8Array in type definitions and function parameters
- Updated type definitions in `types.ts` across multiple files to replace `Uint8Array<ArrayBuffer>` with `Uint8Array` for better clarity and consistency. - Adjusted function parameters in `base-x.js`, `ec-compression.js`, `utf8.js`, and others to reflect the new type definitions. - Refactored utility functions to ensure compatibility with the updated types, enhancing maintainability and reducing potential type-related issues.
1 parent aadcc3a commit 09a2277

File tree

32 files changed

+117
-128
lines changed

32 files changed

+117
-128
lines changed

packages/iso-base/src/base-x.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import _baseX from 'base-x'
1010
import { utf8 } from './utf8.js'
11-
import { isBufferSource, u8 } from './utils.js'
11+
import { isUint8Array, u8 } from './utils.js'
1212

1313
/** @typedef {import('./types.js').Codec} Codec */
1414

@@ -35,13 +35,11 @@ export function baseX(base) {
3535
return _baseX(alphabet).encode(u8(input))
3636
},
3737
decode(input) {
38-
if (isBufferSource(input)) {
38+
if (isUint8Array(input)) {
3939
input = utf8.encode(input)
4040
}
4141

42-
return /** @type {Uint8Array<ArrayBuffer>} */ (
43-
_baseX(alphabet).decode(input)
44-
)
42+
return /** @type {Uint8Array} */ (_baseX(alphabet).decode(input))
4543
},
4644
}
4745
}

packages/iso-base/src/ec-compression.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function isCompressed(key) {
7474
}
7575

7676
/**
77-
* @param {Uint8Array<ArrayBuffer>} key
77+
* @param {Uint8Array} key
7878
*/
7979
export function isUncompressed(key) {
8080
return key[0] === 0x04
@@ -83,7 +83,7 @@ export function isUncompressed(key) {
8383
/**
8484
* Elliptic Curve point compression
8585
*
86-
* @param { Uint8Array<ArrayBuffer> } pubkeyBytes
86+
* @param { Uint8Array } pubkeyBytes
8787
*/
8888
export function compress(pubkeyBytes) {
8989
if (!isUncompressed(pubkeyBytes)) {
@@ -105,7 +105,7 @@ export function compress(pubkeyBytes) {
105105
/**
106106
* Elliptic Curve point decompression
107107
*
108-
* @param {Uint8Array<ArrayBuffer>} comp - Compressed public key. 1st byte: 0x02 for even or 0x03 for odd. Following curve size n bytes: x coordinate expressed as big-endian.
108+
* @param {Uint8Array} comp - Compressed public key. 1st byte: 0x02 for even or 0x03 for odd. Following curve size n bytes: x coordinate expressed as big-endian.
109109
* @param {Curve} curve
110110
*/
111111
export function decompress(comp, curve = 'P-256') {
@@ -148,7 +148,7 @@ const pIdentSecp256k1 = (primeSecp256k1 + 1n) / 4n
148148
/**
149149
* secp256k1 decompression
150150
*
151-
* @param {Uint8Array<ArrayBuffer>} compressed
151+
* @param {Uint8Array} compressed
152152
*/
153153
export function decompressSecp256k1(compressed) {
154154
const signY = BigInt(compressed[0] - 2)

packages/iso-base/src/rfc4648.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/** @typedef {import('./types').Codec} Codec */
99

1010
import { utf8 } from './utf8.js'
11-
import { isBufferSource, u8 } from './utils.js'
11+
import { isUint8Array, u8 } from './utils.js'
1212

1313
/**
1414
* Decode
@@ -144,7 +144,7 @@ export function rfc4648(base, padding = false, normalize) {
144144
return encode(u8(input), alphabet, bits, pad ?? padding)
145145
},
146146
decode(input) {
147-
if (isBufferSource(input)) {
147+
if (isUint8Array(input)) {
148148
input = utf8.encode(input)
149149
}
150150

packages/iso-base/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export interface Codec {
1818
* @param data - Data to be encoded to string
1919
* @param pad - Should have padding. Defaults: true
2020
*/
21-
encode: (data: BufferSource | string, pad?: boolean) => string
22-
decode: (data: BufferSource | string) => Uint8Array<ArrayBuffer>
21+
encode: (data: Uint8Array | string, pad?: boolean) => string
22+
decode: (data: Uint8Array | string) => Uint8Array
2323
}
2424

2525
/**

packages/iso-base/src/utf8.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @module
55
*/
6-
import { isBufferSource, u8 } from './utils.js'
6+
import { isUint8Array, u8 } from './utils.js'
77

88
const textDecoder = new TextDecoder()
99
const textEncoder = new TextEncoder()
@@ -19,7 +19,7 @@ export const utf8 = {
1919
return textDecoder.decode(input)
2020
},
2121
decode(input) {
22-
if (isBufferSource(input)) {
22+
if (isUint8Array(input)) {
2323
return u8(input)
2424
}
2525
return textEncoder.encode(input)

packages/iso-base/src/utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,21 @@ export function isBufferSource(value) {
123123
return isTypedArray(value) || isArrayBuffer(value)
124124
}
125125

126+
/**
127+
* @param {unknown} value
128+
* @returns {value is Uint8Array}
129+
*/
130+
export function isUint8Array(value) {
131+
return (
132+
value instanceof Uint8Array ||
133+
(ArrayBuffer.isView(value) && value.constructor.name === 'Uint8Array')
134+
)
135+
}
136+
126137
/**
127138
* Cast typedarray to Uint8Array
128139
*
129-
* @param {BufferSource} arr
140+
* @param {ArrayBuffer | Int8Array | Uint8ClampedArray | Uint8Array | Uint16Array | Int16Array | Uint32Array | Int32Array } arr
130141
*/
131142
export function u8(arr) {
132143
return ArrayBuffer.isView(arr)

packages/iso-did/src/key.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as EC from 'iso-base/ec-compression'
2-
import { u8 } from 'iso-base/utils'
32
import { tag, varint } from 'iso-base/varint'
43
import { base58btc } from 'multiformats/bases/base58'
54
import { CODE_KEY_TYPE, KEY_TYPE_CODE } from './common.js'
@@ -15,7 +14,7 @@ const DID_KEY_PREFIX = 'did:key:'
1514
* Validate raw public key length
1615
*
1716
* @param {number} code
18-
* @param {Uint8Array<ArrayBuffer>} key
17+
* @param {Uint8Array} key
1918
*/
2019
export function validateRawPublicKeyLength(code, key) {
2120
switch (code) {
@@ -87,7 +86,7 @@ export class DIDKey extends DIDCore {
8786
*
8887
* @param {T.DIDURLObject} did
8988
* @param {T.KeyType} type
90-
* @param {Uint8Array<ArrayBuffer>} key
89+
* @param {Uint8Array} key
9190
*/
9291
constructor(did, type, key) {
9392
super(did)
@@ -124,15 +123,15 @@ export class DIDKey extends DIDCore {
124123
* Create a DIDKey from a public key bytes
125124
*
126125
* @param {T.KeyType} type
127-
* @param {Uint8Array<ArrayBuffer>} key
126+
* @param {Uint8Array} key
128127
*/
129128
static fromPublicKey(type, key) {
130129
const code = KEY_TYPE_CODE[type]
131130
if (!code) {
132131
throw new TypeError(`Unsupported key type "${type}"`)
133132
}
134133

135-
const keyBytes = validateRawPublicKeyLength(code, u8(key))
134+
const keyBytes = validateRawPublicKeyLength(code, key)
136135
const id = base58btc.encode(tag(code, keyBytes))
137136

138137
return new DIDKey(

packages/iso-did/test/key.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DIDKey } from '../src/key.js'
66
* @typedef {import('../src/types').KeyType} PublicKeyType
77
*/
88

9-
/** @type {Record<PublicKeyType, {did: string, pub: Uint8Array<ArrayBuffer>}[]>} */
9+
/** @type {Record<PublicKeyType, {did: string, pub: Uint8Array}[]>} */
1010
const VECTORS = {
1111
Ed25519: [
1212
{

packages/iso-passkeys/src/cose.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function didFromCose(key) {
131131
/**
132132
* Hash based on the cose alg using web crypto
133133
*
134-
* @param {Uint8Array<ArrayBuffer> | string} data
134+
* @param {Uint8Array | string} data
135135
* @param {import('./cose.js').COSEALG} alg - Cose alg, defaults to -7 (ES256) SHA-256 for web crypto
136136
*/
137137
export async function hash(data, alg = -7) {
@@ -141,5 +141,7 @@ export async function hash(data, alg = -7) {
141141

142142
const subtleAlg = mapCoseAlgToWebCryptoAlg(alg)
143143

144-
return new Uint8Array(await webcrypto.subtle.digest(subtleAlg, data))
144+
return new Uint8Array(
145+
await webcrypto.subtle.digest(subtleAlg, /** @type {BufferSource} */ (data))
146+
)
145147
}

packages/iso-passkeys/src/parsing.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function toPublicKeyCredentialDescriptor(descriptor) {
126126

127127
return {
128128
...descriptor,
129-
id: base64url.decode(id),
129+
id: /** @type {BufferSource} */ (base64url.decode(id)),
130130
}
131131
}
132132

@@ -145,7 +145,9 @@ export function parseRequestOptionsFromJSON(options) {
145145
)
146146
}
147147
return {
148-
challenge: base64url.decode(options.challenge),
148+
challenge: /** @type {BufferSource} */ (
149+
base64url.decode(options.challenge)
150+
),
149151
timeout: options.timeout,
150152
rpId: options.rpId,
151153
allowCredentials,
@@ -175,10 +177,12 @@ export function parseCreationOptionsFromJSON(options) {
175177
type: 'public-key',
176178
})),
177179
...options,
178-
challenge: base64url.decode(options.challenge),
180+
challenge: /** @type {BufferSource} */ (
181+
base64url.decode(options.challenge)
182+
),
179183
user: {
180184
...options.user,
181-
id: utf8.decode(options.user.id),
185+
id: /** @type {BufferSource} */ (utf8.decode(options.user.id)),
182186
},
183187

184188
excludeCredentials,
@@ -194,7 +198,7 @@ export function parseCreationOptionsFromJSON(options) {
194198
* @param {import('./types').AuthenticationPublicKeyCredential | import('./types').RegistrationPublicKeyCredential} credential
195199
*/
196200
export function publicKeyCredentialToJSON(credential) {
197-
// @ts-ignore
201+
// @ts-expect-error TODO: fix this
198202
if (credential.response.signature) {
199203
const { rawId, id, type, authenticatorAttachment, response } =
200204
/** @type {import('./types').AuthenticationPublicKeyCredential} */ (
@@ -239,7 +243,6 @@ export function publicKeyCredentialToJSON(credential) {
239243
attestationObject: base64url.encode(
240244
new Uint8Array(response.attestationObject)
241245
),
242-
// @ts-ignore
243246
transports: response.getTransports(),
244247
},
245248
}

0 commit comments

Comments
 (0)