Skip to content

Commit

Permalink
Rewrite Crypto WASM using the original iroha_crypto crate (#188)
Browse files Browse the repository at this point in the history
* [refactor]: update WASM API, wip

`cargo check --target wasm32-unknown-unknown` fails:
`getrandom` crate features are invalid

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>

* [feat]: complete refactoring (upstream is WIP)

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>

* [refactor]: update upstream to hyperledger/iroha#4341

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>

* [refactor]: update crypto, finally

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>

---------

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>
  • Loading branch information
0x009922 committed Mar 14, 2024
1 parent b0ebbd3 commit 787a198
Show file tree
Hide file tree
Showing 41 changed files with 3,667 additions and 6,465 deletions.
16 changes: 16 additions & 0 deletions .changeset/itchy-humans-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@iroha2/crypto-core': major
'@iroha2/crypto-target-bundler': major
'@iroha2/crypto-target-node': major
'@iroha2/crypto-target-web': major
---

**Breaking:** Complete rewrite of crypto WASM, and major update of the surrounding API.

- Now WASM is made from the original `iroha_crypto` from Iroha 2 repo. As one of the outcomes, binary blob size is reduced from 2mb to 600kb.
- Remove `KeyGenConfiguration`. Use `KeyPair.deriveFromSeed`, `KeyPair.deriveFromPrivateKey`, and `KeyPair.random` instead.
- Normalise API across `PublicKey`, `PrivateKey`, `KeyPair`, and `Signature` classes (JSON methods, raw conversion methods etc.)
- Introduce `Bytes` utility to accept binary input either as `Bytes.array([1, 2, 3])` or `Bytes.hex('001122')`
- Export more types¡

See the [issue](https://github.com/hyperledger/iroha-javascript/issues/186) for related context.
5 changes: 5 additions & 0 deletions .changeset/proud-pugs-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@iroha2/client': minor
---

**refactor:** handle the major update of `@iroha2/crypto-core`
5 changes: 5 additions & 0 deletions .changeset/spotty-cows-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@iroha2/crypto-util': minor
---

**feat:** add `Bytes` util class. Use `Bytes.hex` or `Bytes.array` to pass byte input in a convenient way.
14 changes: 7 additions & 7 deletions packages/client/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Events, Status & Health check.
*/

import { cryptoTypes, freeScope } from '@iroha2/crypto-core'
import { Bytes, cryptoTypes, freeScope } from '@iroha2/crypto-core'
import { RustResult, datamodel, variant } from '@iroha2/data-model'
import { Except } from 'type-fest'
import { SetupBlocksStreamParams, SetupBlocksStreamReturn, setupBlocksStream } from './blocks-stream'
Expand Down Expand Up @@ -39,9 +39,9 @@ export class Signer {
this.keyPair = keyPair
}

public sign(...message: cryptoTypes.BytesInputTuple): datamodel.Signature {
public sign(message: Bytes): datamodel.Signature {
return freeScope(() => {
const signature = this.keyPair.sign(...message)
const signature = this.keyPair.sign(message)
const publicKey = signature.publicKey().toDataModel()

return datamodel.Signature({
Expand Down Expand Up @@ -81,12 +81,12 @@ export function makeTransactionPayload(params: MakeTransactionPayloadParams): da
}

export function computeTransactionHash(payload: datamodel.TransactionPayload): Uint8Array {
return cryptoHash('array', datamodel.TransactionPayload.toBuffer(payload))
return cryptoHash(Bytes.array(datamodel.TransactionPayload.toBuffer(payload)))
}

export function signTransaction(payload: datamodel.TransactionPayload, signer: Signer): datamodel.Signature {
const hash = computeTransactionHash(payload)
return signer.sign('array', hash)
return signer.sign(Bytes.array(hash))
}

export function makeSignedTransaction(
Expand Down Expand Up @@ -141,12 +141,12 @@ export function makeQueryPayload(params: MakeQueryPayloadParams): datamodel.Quer
}

export function computeQueryHash(payload: datamodel.QueryPayload): Uint8Array {
return cryptoHash('array', datamodel.QueryPayload.toBuffer(payload))
return cryptoHash(Bytes.array(datamodel.QueryPayload.toBuffer(payload)))
}

export function signQuery(payload: datamodel.QueryPayload, signer: Signer): datamodel.Signature {
const hash = computeQueryHash(payload)
return signer.sign('array', hash)
return signer.sign(Bytes.array(hash))
}

export function makeSignedQuery(payload: datamodel.QueryPayload, signer: Signer): datamodel.SignedQuery {
Expand Down
12 changes: 4 additions & 8 deletions packages/client/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import { Debugger } from 'debug'
import Emittery from 'emittery'
import JsonBigIntParseFactory from 'json-bigint/lib/parse.js'
import { getCryptoAnyway } from './crypto-singleton'
import { cryptoTypes, freeScope } from '@iroha2/crypto-core'

export function cryptoHash(...input: cryptoTypes.BytesInputTuple): Uint8Array {
return freeScope(() =>
getCryptoAnyway()
.Hash.hash(...input)
.bytes(),
)
import { Bytes, freeScope } from '@iroha2/crypto-core'

export function cryptoHash(input: Bytes): Uint8Array {
return freeScope(() => getCryptoAnyway().Hash.hash(input).bytes())
}

export function transformProtocolInUrlFromHttpToWs(url: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
makeSignedTransaction,
makeTransactionPayload,
} from '@iroha2/client'
import { freeScope } from '@iroha2/crypto-core'
import { freeScope, Bytes } from '@iroha2/crypto-core'
import { datamodel, sugar } from '@iroha2/data-model'
import { pipe } from 'fp-ts/function'
import { produce } from 'immer'
Expand All @@ -32,8 +32,8 @@ describe('MST (Multi-Signature Transaction)', () => {

const KEYS = freeScope((scope) => {
const keys = [
crypto.KeyGenConfiguration.default().useSeed('hex', '001122').generate(),
crypto.KeyGenConfiguration.default().useSeed('hex', '332211').generate(),
crypto.KeyPair.deriveFromSeed(Bytes.hex('001122')),
crypto.KeyPair.deriveFromSeed(Bytes.hex('332211')),
] as const

for (const x of keys) scope.forget(x)
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('MST (Multi-Signature Transaction)', () => {
'V1',
datamodel.SignedTransactionV1({
payload: mintTransactionPayload,
signatures: datamodel.SortedVecSignature([signer1.sign('array', txHash)]),
signatures: datamodel.SortedVecSignature([signer1.sign(Bytes.array(txHash))]),
}),
)

Expand Down Expand Up @@ -156,7 +156,7 @@ describe('MST (Multi-Signature Transaction)', () => {
// we use `produce` from `immer` library
// it allows us to produce a new value from `tx1` without touching it in a declarative way
produce(tx1, (draft) => {
draft.enum.content.signatures.push(signer2.sign('array', txHash))
draft.enum.content.signatures.push(signer2.sign(Bytes.array(txHash)))
})

await blocks.wait(async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/crypto/crypto-rs/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
Loading

0 comments on commit 787a198

Please sign in to comment.