Skip to content

Commit 787a198

Browse files
authored
Rewrite Crypto WASM using the original iroha_crypto crate (#188)
* [refactor]: update WASM API, wip `cargo check --target wasm32-unknown-unknown` fails: `getrandom` crate features are invalid Signed-off-by: Dmitry Balashov <[email protected]> * [feat]: complete refactoring (upstream is WIP) Signed-off-by: Dmitry Balashov <[email protected]> * [refactor]: update upstream to hyperledger-iroha/iroha#4341 Signed-off-by: Dmitry Balashov <[email protected]> * [refactor]: update crypto, finally Signed-off-by: Dmitry Balashov <[email protected]> --------- Signed-off-by: Dmitry Balashov <[email protected]>
1 parent b0ebbd3 commit 787a198

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3667
-6465
lines changed

.changeset/itchy-humans-look.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@iroha2/crypto-core': major
3+
'@iroha2/crypto-target-bundler': major
4+
'@iroha2/crypto-target-node': major
5+
'@iroha2/crypto-target-web': major
6+
---
7+
8+
**Breaking:** Complete rewrite of crypto WASM, and major update of the surrounding API.
9+
10+
- 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.
11+
- Remove `KeyGenConfiguration`. Use `KeyPair.deriveFromSeed`, `KeyPair.deriveFromPrivateKey`, and `KeyPair.random` instead.
12+
- Normalise API across `PublicKey`, `PrivateKey`, `KeyPair`, and `Signature` classes (JSON methods, raw conversion methods etc.)
13+
- Introduce `Bytes` utility to accept binary input either as `Bytes.array([1, 2, 3])` or `Bytes.hex('001122')`
14+
- Export more types¡
15+
16+
See the [issue](https://github.com/hyperledger/iroha-javascript/issues/186) for related context.

.changeset/proud-pugs-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@iroha2/client': minor
3+
---
4+
5+
**refactor:** handle the major update of `@iroha2/crypto-core`

.changeset/spotty-cows-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@iroha2/crypto-util': minor
3+
---
4+
5+
**feat:** add `Bytes` util class. Use `Bytes.hex` or `Bytes.array` to pass byte input in a convenient way.

packages/client/src/lib.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Events, Status & Health check.
66
*/
77

8-
import { cryptoTypes, freeScope } from '@iroha2/crypto-core'
8+
import { Bytes, cryptoTypes, freeScope } from '@iroha2/crypto-core'
99
import { RustResult, datamodel, variant } from '@iroha2/data-model'
1010
import { Except } from 'type-fest'
1111
import { SetupBlocksStreamParams, SetupBlocksStreamReturn, setupBlocksStream } from './blocks-stream'
@@ -39,9 +39,9 @@ export class Signer {
3939
this.keyPair = keyPair
4040
}
4141

42-
public sign(...message: cryptoTypes.BytesInputTuple): datamodel.Signature {
42+
public sign(message: Bytes): datamodel.Signature {
4343
return freeScope(() => {
44-
const signature = this.keyPair.sign(...message)
44+
const signature = this.keyPair.sign(message)
4545
const publicKey = signature.publicKey().toDataModel()
4646

4747
return datamodel.Signature({
@@ -81,12 +81,12 @@ export function makeTransactionPayload(params: MakeTransactionPayloadParams): da
8181
}
8282

8383
export function computeTransactionHash(payload: datamodel.TransactionPayload): Uint8Array {
84-
return cryptoHash('array', datamodel.TransactionPayload.toBuffer(payload))
84+
return cryptoHash(Bytes.array(datamodel.TransactionPayload.toBuffer(payload)))
8585
}
8686

8787
export function signTransaction(payload: datamodel.TransactionPayload, signer: Signer): datamodel.Signature {
8888
const hash = computeTransactionHash(payload)
89-
return signer.sign('array', hash)
89+
return signer.sign(Bytes.array(hash))
9090
}
9191

9292
export function makeSignedTransaction(
@@ -141,12 +141,12 @@ export function makeQueryPayload(params: MakeQueryPayloadParams): datamodel.Quer
141141
}
142142

143143
export function computeQueryHash(payload: datamodel.QueryPayload): Uint8Array {
144-
return cryptoHash('array', datamodel.QueryPayload.toBuffer(payload))
144+
return cryptoHash(Bytes.array(datamodel.QueryPayload.toBuffer(payload)))
145145
}
146146

147147
export function signQuery(payload: datamodel.QueryPayload, signer: Signer): datamodel.Signature {
148148
const hash = computeQueryHash(payload)
149-
return signer.sign('array', hash)
149+
return signer.sign(Bytes.array(hash))
150150
}
151151

152152
export function makeSignedQuery(payload: datamodel.QueryPayload, signer: Signer): datamodel.SignedQuery {

packages/client/src/util.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ import { Debugger } from 'debug'
33
import Emittery from 'emittery'
44
import JsonBigIntParseFactory from 'json-bigint/lib/parse.js'
55
import { getCryptoAnyway } from './crypto-singleton'
6-
import { cryptoTypes, freeScope } from '@iroha2/crypto-core'
7-
8-
export function cryptoHash(...input: cryptoTypes.BytesInputTuple): Uint8Array {
9-
return freeScope(() =>
10-
getCryptoAnyway()
11-
.Hash.hash(...input)
12-
.bytes(),
13-
)
6+
import { Bytes, freeScope } from '@iroha2/crypto-core'
7+
8+
export function cryptoHash(input: Bytes): Uint8Array {
9+
return freeScope(() => getCryptoAnyway().Hash.hash(input).bytes())
1410
}
1511

1612
export function transformProtocolInUrlFromHttpToWs(url: string): string {

packages/client/test/integration/test-node/test/multisignature.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
makeSignedTransaction,
1111
makeTransactionPayload,
1212
} from '@iroha2/client'
13-
import { freeScope } from '@iroha2/crypto-core'
13+
import { freeScope, Bytes } from '@iroha2/crypto-core'
1414
import { datamodel, sugar } from '@iroha2/data-model'
1515
import { pipe } from 'fp-ts/function'
1616
import { produce } from 'immer'
@@ -32,8 +32,8 @@ describe('MST (Multi-Signature Transaction)', () => {
3232

3333
const KEYS = freeScope((scope) => {
3434
const keys = [
35-
crypto.KeyGenConfiguration.default().useSeed('hex', '001122').generate(),
36-
crypto.KeyGenConfiguration.default().useSeed('hex', '332211').generate(),
35+
crypto.KeyPair.deriveFromSeed(Bytes.hex('001122')),
36+
crypto.KeyPair.deriveFromSeed(Bytes.hex('332211')),
3737
] as const
3838

3939
for (const x of keys) scope.forget(x)
@@ -116,7 +116,7 @@ describe('MST (Multi-Signature Transaction)', () => {
116116
'V1',
117117
datamodel.SignedTransactionV1({
118118
payload: mintTransactionPayload,
119-
signatures: datamodel.SortedVecSignature([signer1.sign('array', txHash)]),
119+
signatures: datamodel.SortedVecSignature([signer1.sign(Bytes.array(txHash))]),
120120
}),
121121
)
122122

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

162162
await blocks.wait(async () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-unknown-unknown]
2+
runner = 'wasm-bindgen-test-runner'

0 commit comments

Comments
 (0)