Skip to content

Commit

Permalink
fix(askar): generate nonce suitable for anoncreds (openwallet-foundat…
Browse files Browse the repository at this point in the history
…ion#1295)

Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris committed Feb 13, 2023
1 parent c63350c commit ecce0a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/askar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
"dependencies": {
"@aries-framework/core": "0.3.3",
"@hyperledger/aries-askar-shared": "^0.1.0-dev.1",
"bn.js": "^5.2.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"rxjs": "^7.2.0",
"tsyringe": "^4.7.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@hyperledger/aries-askar-nodejs": "^0.1.0-dev.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^4.0.7",
Expand Down
7 changes: 5 additions & 2 deletions packages/askar/src/wallet/AskarWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
FileSystem,
WalletNotFoundError,
} from '@aries-framework/core'
// eslint-disable-next-line import/order
import {
StoreKeyMethod,
KeyAlgs,
Expand All @@ -43,6 +42,8 @@ import {
Key as AskarKey,
keyAlgFromString,
} from '@hyperledger/aries-askar-shared'
// eslint-disable-next-line import/order
import BigNumber from 'bn.js'

const isError = (error: unknown): error is Error => error instanceof Error

Expand Down Expand Up @@ -669,7 +670,9 @@ export class AskarWallet implements Wallet {

public async generateNonce(): Promise<string> {
try {
return TypedArrayEncoder.toUtf8String(CryptoBox.randomNonce())
// generate an 80-bit nonce suitable for AnonCreds proofs
const nonce = CryptoBox.randomNonce().slice(0, 10)
return new BigNumber(nonce).toString()
} catch (error) {
if (!isError(error)) {
throw new AriesFrameworkError('Attempted to throw error, but it was not of type Error', { cause: error })
Expand Down
4 changes: 3 additions & 1 deletion packages/askar/src/wallet/__tests__/AskarWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('AskarWallet basic operations', () => {
})

test('Generate Nonce', async () => {
await expect(askarWallet.generateNonce()).resolves.toEqual(expect.any(String))
const nonce = await askarWallet.generateNonce()

expect(nonce).toMatch(/[0-9]+/)
})

test('Create ed25519 keypair', async () => {
Expand Down

0 comments on commit ecce0a7

Please sign in to comment.