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 <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday authored and denyeart committed Oct 7, 2024
1 parent e37e991 commit b931df3
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
@@ -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"
@@ -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),
Original file line number Diff line number Diff line change
@@ -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;
@@ -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))
@@ -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
@@ -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));
}

@@ -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));
}

@@ -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: ");
Original file line number Diff line number Diff line change
@@ -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');
@@ -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
Original file line number Diff line number Diff line change
@@ -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';
@@ -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
@@ -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
@@ -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"
@@ -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),
2 changes: 2 additions & 0 deletions asset-transfer-events/application-gateway-go/app.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import (
"time"

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

const (
@@ -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),
Loading

0 comments on commit b931df3

Please sign in to comment.