Skip to content

Commit

Permalink
add protocol parameters hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Dec 1, 2023
1 parent 5a0ad4e commit 013f3cf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
6 changes: 6 additions & 0 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ pub enum UtilsMethod {
serial_number: u32,
token_scheme_type: u8,
},
/// Computes the hash of the given protocol parameters.
#[serde(rename_all = "camelCase")]
ProtocolParametersHash {
/// Network Protocol Parameters
protocol_parameters: ProtocolParameters,
},
/// Computes the signing hash of a transaction.
TransactionSigningHash {
/// The transaction.
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let foundry_id = FoundryId::build(&AccountAddress::new(account_id), serial_number, token_scheme_type);
Response::TokenId(TokenId::from(foundry_id))
}
UtilsMethod::ProtocolParametersHash { protocol_parameters } => {
Response::Hash(protocol_parameters.hash().to_string())
}
UtilsMethod::TransactionSigningHash { transaction } => {
Response::Hash(Transaction::try_from_dto(transaction)?.signing_hash().to_string())
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__IsAddressValidMethod__,
__ProtocolParametersHashMethod__,
__TransactionSigningHashMethod__,
__VerifyEd25519SignatureMethod__,
__VerifySecp256k1EcdsaSignatureMethod__,
Expand Down Expand Up @@ -45,6 +46,7 @@ export type __UtilsMethods__ =
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __IsAddressValidMethod__
| __ProtocolParametersHashMethod__
| __TransactionSigningHashMethod__
| __VerifyEd25519SignatureMethod__
| __VerifySecp256k1EcdsaSignatureMethod__
Expand Down
7 changes: 7 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ export interface __IsAddressValidMethod__ {
};
}

export interface __ProtocolParametersHashMethod__ {
name: 'protocolParametersHash';
data: {
protocol_parameters: ProtocolParameters;
};
}

export interface __TransactionSigningHashMethod__ {
name: 'transactionSigningHash';
data: {
Expand Down
17 changes: 17 additions & 0 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,23 @@ export class Utils {
});
}

/**
* Compute the hash of an instance of ProtocolParameters.
*
* @param protocol_parameters A ProtocolParameters instance.
* @returns The hash of the protocol parameters as a hex-encoded string.
*/
static protocolParametersHash(
protocol_parameters: ProtocolParameters,
): HexEncodedString {
return callUtilsMethod({
name: 'protocolParametersHash',
data: {
protocol_parameters,
},
});
}

/**
* Compute the signing hash of a transaction.
*
Expand Down
33 changes: 12 additions & 21 deletions bindings/nodejs/tests/types/protocol_parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,25 @@
import { plainToInstance, Type } from 'class-transformer';
import { describe, it } from '@jest/globals';
import { expect } from '@jest/globals';
import * as protocol_parameters from '../../../../sdk/tests/types/fixtures/protocol_parameters.json';
import { ProtocolParameters, WorkScoreParameters, ManaParameters } from '../../lib/types/models/info/node-info-protocol';
import { StorageScoreParameters } from '../../lib/types/models';
import { Utils } from '../../lib/utils/utils';
import * as protocol_parameters from '../../../../sdk/tests/types/fixtures/protocol_parameters.json';

describe('ProtocolParameters tests', () => {

it('creates ProtocolParameters from a fixture', async () => {

// TODO: is there a way around `as unknown`? The string->bigint parameters are the issue.
const params = protocol_parameters.params as unknown as ProtocolParameters;

// Check whether parameters were correctly set
expect(params.type).toEqual(0);
expect(params.version).toEqual(3);
expect(params.networkName).toEqual("TestJungle");
expect(params.bech32Hrp).toEqual("tgl");
it('compares ProtocolParameters bytes from a fixture', async () => {
// TODO: serialize to ProtocolParameters to bytes
});

// TODO: is there a way around `as unknown`? The string->bigint parameters are the issue.
const storageScoreParams = protocol_parameters.params.storageScoreParameters as unknown as StorageScoreParameters;
it('compares ProtocolParameters hash from a fixture', async () => {
// TODO: do we want to hash from binding side? Feels unnecessary tbh.

expect(storageScoreParams.storageCost).toEqual("0");
expect(storageScoreParams.factorData).toEqual(0);
expect(storageScoreParams.offsetOutputOverhead).toEqual("0");
expect(storageScoreParams.offsetEd25519BlockIssuerKey).toEqual("0");
expect(storageScoreParams.offsetStakingFeature).toEqual("0");
expect(storageScoreParams.offsetDelegation).toEqual("0");
const params = protocol_parameters.params as unknown as ProtocolParameters;
const hash = Utils.protocolParametersHash(params);

const workScoreParameters = protocol_parameters.params.workScoreParameters as WorkScoreParameters;
const manaParameters = protocol_parameters.params.manaParameters as ManaParameters;
const expected_hash = protocol_parameters.hash;

expect(hash).toEqual(expected_hash);
});
});

0 comments on commit 013f3cf

Please sign in to comment.