Skip to content

Commit

Permalink
fix: remove key id (#1971)
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Jul 30, 2024
1 parent 1a941e7 commit cebfcce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
16 changes: 7 additions & 9 deletions packages/askar/src/wallet/AskarBaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export abstract class AskarBaseWallet implements Wallet {
}
} else if (keyBackend === KeyBackend.SecureElement && keyType === KeyType.P256) {
const secureEnvironment = importSecureEnvironment()
const kid = keyId ?? utils.uuid()
const kid = utils.uuid()

// Generate a hardware-backed P-256 keypair
secureEnvironment.generateKeypair(kid)
Expand All @@ -206,7 +206,7 @@ export abstract class AskarBaseWallet implements Wallet {
keyId: kid,
})

return new Key(publicKeyBytes, keyType, kid)
return new Key(publicKeyBytes, keyType)
} else {
// Check if there is a signing key provider for the specified key type.
if (this.signingKeyProviderRegistry.hasProviderForKeyType(keyType)) {
Expand Down Expand Up @@ -277,15 +277,13 @@ export abstract class AskarBaseWallet implements Wallet {
await this.deleteKeyPair(key.publicKeyBase58)
keyPair = undefined
} else {
if (!(await this.doesSecureEnvironmentKeyExist(key.keyId))) {
throw new WalletError(`Secure Environment key with id '${key.keyId}' not found`)
}
const { keyId } = await this.getSecureEnvironmentKey(key.publicKeyBase58)

if (Array.isArray(data[0])) {
throw new WalletError('Multi signature is not supported for the Secure Environment')
}

return Buffer.from(await importSecureEnvironment().sign(key.keyId, new Uint8Array(data as Buffer)))
return Buffer.from(await importSecureEnvironment().sign(keyId, new Uint8Array(data as Buffer)))
}
}

Expand Down Expand Up @@ -518,13 +516,13 @@ export abstract class AskarBaseWallet implements Wallet {
}
}

private async doesSecureEnvironmentKeyExist(keyId: string): Promise<boolean> {
private async getSecureEnvironmentKey(keyId: string): Promise<{ keyId: string }> {
try {
const entryObject = await this.withSession((session) =>
session.fetch({ category: 'SecureEnvironmentKeyRecord', name: keyId })
)

return !!entryObject
return JsonEncoder.fromString(entryObject?.value as string) as { keyId: string }
} catch (error) {
throw new WalletError('Error retrieving Secure Environment record', { cause: error })
}
Expand Down Expand Up @@ -567,7 +565,7 @@ export abstract class AskarBaseWallet implements Wallet {
await this.withSession((session) =>
session.insert({
category: 'SecureEnvironmentKeyRecord',
name: options.keyId,
name: options.publicKeyBase58,
value: JSON.stringify(options),
tags: {
keyType: options.keyType,
Expand Down
10 changes: 1 addition & 9 deletions packages/core/src/crypto/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ export class Key {
public readonly publicKey: Buffer
public readonly keyType: KeyType

/**
*
* the identifier of the key. If not provided in the constructor the base58 encoded public key will be used as the key identifier by default
*
*/
public keyId: string

public constructor(publicKey: Uint8Array, keyType: KeyType, keyId?: string) {
public constructor(publicKey: Uint8Array, keyType: KeyType) {
this.publicKey = Buffer.from(publicKey)
this.keyType = keyType
this.keyId = keyId ?? TypedArrayEncoder.toBase58(this.publicKey)
}

public static fromPublicKey(publicKey: Uint8Array, keyType: KeyType) {
Expand Down

0 comments on commit cebfcce

Please sign in to comment.