hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

How to utilize `generateKeyPairWithConfiguration` #119

Closed s8sato closed 1 year ago

s8sato commented 1 year ago

This might be rather a Q&A issue than a bug reporting.

Steps to Reproduce

Clone the reporting repository https://github.com/outSH/iroha2_check/tree/7e148419ae1d886a1f41b045bcc4dda1e379bfff and

tsx generate-key.ts

Expected Behavior

The script, which is trying to utilize generateKeyPairWithConfiguration, successfully runs

Actual Behavior

As described in the script https://github.com/outSH/iroha2_check/blob/7e148419ae1d886a1f41b045bcc4dda1e379bfff/generate-key.ts

Possible Causes

Possible Solution

0x009922 commented 1 year ago

Hah, TypeScript tried to prevent the error!

The Bug

The bug is here:

const config = new (crypto as any).KeyGenConfiguration()
  .useSeed(Uint8Array.from(SEED_BYTES))
  .withAlgorithm(keyAlgo);

You cannot create underlying crypto classes directly via constructors, and @iroha2/crypto-core declares it. That is why you can see (crypto as any) here.

The Fix

- const config = new (crypto as any).KeyGenConfiguration()
+ const config = crypto.createKeyGenConfiguration()
  .useSeed(Uint8Array.from(SEED_BYTES))
  .withAlgorithm(keyAlgo);

But... why it is not failed on new KeyGenConfiguration()?

Implementation details I tried to hide behind TypeScript declarations.

Why we cannot use crypto classes directly?

https://github.com/hyperledger/iroha-javascript/pull/69#issue-963187691 (start from "Don't forget to free() crypto structures!")