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

Utilities to generate keypair from seed and build transaction with existing payload #67

Closed 0x009922 closed 3 years ago

0x009922 commented 3 years ago

Request

There are two tasks:

Key pairs generation

The most efficient way to reach this goal and an ultimative solution for this is to completely port iroha_crypto crate into WASM. It is a big task and requires a lot of refactoring in Rust's sources.

Building transaction

I guess the solution for this will be a special TransactionBuilder helper that will provide several ways to build a transaction. I don't know how it will be looks like because it depends on how the new @iroha/crypto package will looks like.

0x009922 commented 3 years ago

This issue may be resolved with https://github.com/hyperledger/iroha-javascript/pull/69.

Note that to use crypto you have to initialize it firstly. More about it in the README of the @iroha/crypto package

Key pairs generation

import { KeyGenConfiguration, KeyPair } from '@iroha2/crypto';

const SEED_BYTES = [49, 50, 51, 52];

const config = new KeyGenConfiguration().use_seed(Uint8Array.from(SEED_BYTES));
const keyPair = KeyPair.generate_with_configuration(config);

// now `keyPair` is usable in client

Having a payload as bytes, send transaction with it

import { types, createClient } from '@iroha2/client';
import { KeyPair } from '@iroha2/crypto';

// Having a payload of bytes
const PAYLOAD_BYTES = new Uint8Array([1, 2, 3 /* ... */]);
// And a KeyPair
const keyPair: KeyPair = /* ... */;

// firstly, decode payload into JS representation
const payloadDecoded = types.decode('iroha_data_model::transaction::Payload', PAYLOAD_BYTES);

// init a client
const client = createClient({
    toriiURL: 'http://localhost:8080',
});

// and submit a transaction
await client.submitTransaction({
    payload: payloadDecoded,
    signing: keyPair,
});