Skip to content

Commit

Permalink
Explicitly specify hash in client applications
Browse files Browse the repository at this point in the history
For some signing implementations, such as ed25519, a non-default hash
implementation must be specified when creating the Gateway connection in
client applications. Rather than relying on the default hash algorithm,
it is probably good practice in general to specify an algorithm that is
compatible with your signing implementation.

This change explicitly specifies the hash algorithm to raise visibility
of the option to select the hash algorithm.

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday committed Oct 7, 2024
1 parent e37e991 commit 489b422
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 172 deletions.
2 changes: 2 additions & 0 deletions asset-transfer-basic/application-gateway-go/assetTransfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/hyperledger/fabric-gateway/pkg/client"
"github.com/hyperledger/fabric-gateway/pkg/hash"
"github.com/hyperledger/fabric-gateway/pkg/identity"
"github.com/hyperledger/fabric-protos-go-apiv2/gateway"
"google.golang.org/grpc"
Expand Down Expand Up @@ -50,6 +51,7 @@ func main() {
gw, err := client.Connect(
id,
client.WithSign(sign),
client.WithHash(hash.SHA256),
client.WithClientConnection(clientConnection),
// Default timeouts for different gRPC calls
client.WithEvaluateTimeout(5*time.Second),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hyperledger.fabric.client.EndorseException;
import org.hyperledger.fabric.client.Gateway;
import org.hyperledger.fabric.client.GatewayException;
import org.hyperledger.fabric.client.Hash;
import org.hyperledger.fabric.client.SubmitException;
import org.hyperledger.fabric.client.identity.Identities;
import org.hyperledger.fabric.client.identity.Identity;
Expand Down Expand Up @@ -60,7 +61,11 @@ public static void main(final String[] args) throws Exception {
// this endpoint.
var channel = newGrpcConnection();

var builder = Gateway.newInstance().identity(newIdentity()).signer(newSigner()).connection(channel)
var builder = Gateway.newInstance()
.identity(newIdentity())
.signer(newSigner())
.hash(Hash.SHA256)
.connection(channel)
// Default timeouts for different gRPC calls
.evaluateOptions(options -> options.withDeadlineAfter(5, TimeUnit.SECONDS))
.endorseOptions(options -> options.withDeadlineAfter(15, TimeUnit.SECONDS))
Expand Down Expand Up @@ -131,7 +136,7 @@ public void run() throws GatewayException, CommitException {
// Update an asset which does not exist.
updateNonExistentAsset();
}

/**
* This type of transaction would typically only be run once by an application
* the first time it was started after its initial deployment. A new version of
Expand All @@ -152,7 +157,7 @@ private void getAllAssets() throws GatewayException {
System.out.println("\n--> Evaluate Transaction: GetAllAssets, function returns all the current assets on the ledger");

var result = contract.evaluateTransaction("GetAllAssets");

System.out.println("*** Result: " + prettyJson(result));
}

Expand Down Expand Up @@ -202,15 +207,15 @@ private void transferAssetAsync() throws EndorseException, SubmitException, Comm
throw new RuntimeException("Transaction " + status.getTransactionId() +
" failed to commit with status code " + status.getCode());
}

System.out.println("*** Transaction committed successfully");
}

private void readAssetById() throws GatewayException {
System.out.println("\n--> Evaluate Transaction: ReadAsset, function returns asset attributes");

var evaluateResult = contract.evaluateTransaction("ReadAsset", assetId);

System.out.println("*** Result:" + prettyJson(evaluateResult));
}

Expand All @@ -221,9 +226,9 @@ private void readAssetById() throws GatewayException {
private void updateNonExistentAsset() {
try {
System.out.println("\n--> Submit Transaction: UpdateAsset asset70, asset70 does not exist and should return an error");

contract.submitTransaction("UpdateAsset", "asset70", "blue", "5", "Tomoko", "300");

System.out.println("******** FAILED to return an error");
} catch (EndorseException | SubmitException | CommitStatusException e) {
System.out.println("*** Successfully caught the error: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const grpc = require('@grpc/grpc-js');
const { connect, signers } = require('@hyperledger/fabric-gateway');
const { connect, hash, signers } = require('@hyperledger/fabric-gateway');
const crypto = require('node:crypto');
const fs = require('node:fs/promises');
const path = require('node:path');
Expand Down Expand Up @@ -79,6 +79,7 @@ async function main() {
client,
identity: await newIdentity(),
signer: await newSigner(),
hash: hash.sha256,
// Default timeouts for different gRPC calls
evaluateOptions: () => {
return { deadline: Date.now() + 5000 }; // 5 seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import * as grpc from '@grpc/grpc-js';
import { connect, Contract, Identity, Signer, signers } from '@hyperledger/fabric-gateway';
import { connect, Contract, hash, Identity, Signer, signers } from '@hyperledger/fabric-gateway';
import * as crypto from 'crypto';
import { promises as fs } from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -46,6 +46,7 @@ async function main(): Promise<void> {
client,
identity: await newIdentity(),
signer: await newSigner(),
hash: hash.sha256,
// Default timeouts for different gRPC calls
evaluateOptions: () => {
return { deadline: Date.now() + 5000 }; // 5 seconds
Expand Down Expand Up @@ -243,4 +244,4 @@ function displayInputParameters(): void {
console.log(`tlsCertPath: ${tlsCertPath}`);
console.log(`peerEndpoint: ${peerEndpoint}`);
console.log(`peerHostAlias: ${peerHostAlias}`);
}
}
2 changes: 2 additions & 0 deletions asset-transfer-basic/rest-api-go/web/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/hyperledger/fabric-gateway/pkg/client"
"github.com/hyperledger/fabric-gateway/pkg/hash"
"github.com/hyperledger/fabric-gateway/pkg/identity"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand All @@ -24,6 +25,7 @@ func Initialize(setup OrgSetup) (*OrgSetup, error) {
gateway, err := client.Connect(
id,
client.WithSign(sign),
client.WithHash(hash.SHA256),
client.WithClientConnection(clientConnection),
client.WithEvaluateTimeout(5*time.Second),
client.WithEndorseTimeout(15*time.Second),
Expand Down
2 changes: 2 additions & 0 deletions asset-transfer-events/application-gateway-go/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/hyperledger/fabric-gateway/pkg/client"
"github.com/hyperledger/fabric-gateway/pkg/hash"
)

const (
Expand All @@ -35,6 +36,7 @@ func main() {
gateway, err := client.Connect(
id,
client.WithSign(sign),
client.WithHash(hash.SHA256),
client.WithClientConnection(clientConnection),
client.WithEvaluateTimeout(5*time.Second),
client.WithEndorseTimeout(15*time.Second),
Expand Down
Loading

0 comments on commit 489b422

Please sign in to comment.