Skip to content

Commit

Permalink
feat/node: More type hints (#1340)
Browse files Browse the repository at this point in the history
* more type hints

* added bech32 type

* fmt

* cursor back to string

* changedname

* Apply suggestions from code review

* bech32 in account

* Nits

* Changelog

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 5499587 commit 9e1fb6a
Show file tree
Hide file tree
Showing 26 changed files with 4,379 additions and 4,178 deletions.
6 changes: 6 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.1.0-rc.3 - 2023-MM-DD

### Changed

- More type hints;

## 1.1.0-rc.2 - 2023-09-28

Changes from the 1.0 track.
Expand Down
46 changes: 30 additions & 16 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ import {
MilestonePayload,
TreasuryOutput,
Output,
MilestoneId,
AliasId,
NftId,
FoundryId,
} from '../types/block';
import { HexEncodedString } from '../utils';
import { HexEncodedString, NumericString } from '../utils';
import {
IBlockMetadata,
INodeInfo,
Expand All @@ -48,6 +52,8 @@ import {
UTXOInput,
Response,
OutputId,
TransactionId,
Bech32Address,
} from '../types';
import {
IMilestoneUtxoChangesResponse,
Expand Down Expand Up @@ -431,7 +437,7 @@ export class Client {
/**
* Get the token supply.
*/
async getTokenSupply(): Promise<string> {
async getTokenSupply(): Promise<NumericString> {
return (await this.getProtocolParameters()).tokenSupply;
}

Expand Down Expand Up @@ -571,7 +577,7 @@ export class Client {
* @returns A milestone UTXO changes response.
*/
async getUtxoChangesById(
milestoneId: string,
milestoneId: MilestoneId,
): Promise<IMilestoneUtxoChangesResponse> {
const response = await this.methodHandler.callMethod({
name: 'getUtxoChangesById',
Expand Down Expand Up @@ -665,7 +671,7 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlock(transactionId: string): Promise<Block> {
async getIncludedBlock(transactionId: TransactionId): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlock',
data: {
Expand All @@ -682,7 +688,9 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlockMetadata(transactionId: string): Promise<Block> {
async getIncludedBlockMetadata(
transactionId: TransactionId,
): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlockMetadata',
data: {
Expand All @@ -700,7 +708,10 @@ export class Client {
* @param bech32Hrp The Bech32 HRP (human readable part) to be used.
* @returns The corresponding Bech32 address.
*/
async hexToBech32(hex: string, bech32Hrp?: string): Promise<string> {
async hexToBech32(
hex: HexEncodedString,
bech32Hrp?: string,
): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'hexToBech32',
data: {
Expand All @@ -720,9 +731,9 @@ export class Client {
* @returns The corresponding Bech32 address.
*/
async aliasIdToBech32(
aliasId: string,
aliasId: AliasId,
bech32Hrp?: string,
): Promise<string> {
): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'aliasIdToBech32',
data: {
Expand All @@ -741,7 +752,10 @@ export class Client {
* @param bech32Hrp The Bech32 HRP (human readable part) to be used.
* @returns The corresponding Bech32 address.
*/
async nftIdToBech32(nftId: string, bech32Hrp?: string): Promise<string> {
async nftIdToBech32(
nftId: NftId,
bech32Hrp?: string,
): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'nftIdToBech32',
data: {
Expand All @@ -761,9 +775,9 @@ export class Client {
* @returns The corresponding Bech32 address.
*/
async hexPublicKeyToBech32Address(
hex: string,
hex: HexEncodedString,
bech32Hrp?: string,
): Promise<string> {
): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'hexPublicKeyToBech32Address',
data: {
Expand Down Expand Up @@ -800,7 +814,7 @@ export class Client {
* @param aliasId An Alias ID.
* @returns The corresponding output ID.
*/
async aliasOutputId(aliasId: string): Promise<string> {
async aliasOutputId(aliasId: AliasId): Promise<OutputId> {
const response = await this.methodHandler.callMethod({
name: 'aliasOutputId',
data: {
Expand Down Expand Up @@ -836,7 +850,7 @@ export class Client {
* @param nftId An NFT ID.
* @returns The corresponding output ID.
*/
async nftOutputId(nftId: string): Promise<string> {
async nftOutputId(nftId: NftId): Promise<OutputId> {
const response = await this.methodHandler.callMethod({
name: 'nftOutputId',
data: {
Expand Down Expand Up @@ -872,7 +886,7 @@ export class Client {
* @param foundryId A Foundry ID.
* @returns The corresponding output ID.
*/
async foundryOutputId(foundryId: string): Promise<string> {
async foundryOutputId(foundryId: FoundryId): Promise<OutputId> {
const response = await this.methodHandler.callMethod({
name: 'foundryOutputId',
data: {
Expand All @@ -891,7 +905,7 @@ export class Client {
* @returns An array of corresponding output responses.
*/
async getOutputsIgnoreErrors(
outputIds: string[],
outputIds: OutputId[],
): Promise<OutputResponse[]> {
const response = await this.methodHandler.callMethod({
name: 'getOutputsIgnoreErrors',
Expand Down Expand Up @@ -1211,7 +1225,7 @@ export class Client {
*/
async requestFundsFromFaucet(
url: string,
address: string,
address: Bech32Address,
): Promise<string> {
const response = await this.methodHandler.callMethod({
name: 'requestFundsFromFaucet',
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/secret_manager/secret-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class SecretManager {
*/
async generateEvmAddresses(
generateAddressesOptions: IGenerateAddressesOptions,
): Promise<string[]> {
): Promise<HexEncodedString[]> {
const response = await this.methodHandler.callMethod({
name: 'generateEvmAddresses',
data: {
Expand Down
6 changes: 6 additions & 0 deletions bindings/nodejs/lib/types/block/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { plainToInstance } from 'class-transformer';
import { HexEncodedString } from '../utils';
import { AliasId, NftId } from './id';

/**
* An address prepended by its network type.
*/
type Bech32Address = string;

/**
* Address type variants.
*/
Expand Down Expand Up @@ -138,6 +143,7 @@ const AddressDiscriminator = {

export {
AddressDiscriminator,
Bech32Address,
Address,
AddressType,
Ed25519Address,
Expand Down
3 changes: 2 additions & 1 deletion bindings/nodejs/lib/types/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { HexEncodedString } from '../utils/hex-encoding';
import { Payload, PayloadDiscriminator } from './payload';
import { Type } from 'class-transformer';
import { NumericString } from '../utils/numeric';

/**
* Block layout.
Expand All @@ -27,5 +28,5 @@ export class Block {
/**
* The nonce for the block.
*/
nonce!: string;
nonce!: NumericString;
}
8 changes: 4 additions & 4 deletions bindings/nodejs/lib/types/block/output/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { Feature, FeatureDiscriminator } from './feature';

// Temp solution for not double parsing JSON
import { plainToInstance, Type } from 'class-transformer';
import { HexEncodedString, hexToBigInt } from '../../utils';
import { HexEncodedString, hexToBigInt, NumericString } from '../../utils';
import { TokenScheme, TokenSchemeDiscriminator } from './token-scheme';
import { INativeToken } from '../../models';

export type OutputId = string;
export type OutputId = HexEncodedString;

/**
* All of the output types.
Expand All @@ -35,15 +35,15 @@ enum OutputType {
* The base class for outputs.
*/
abstract class Output /*implements ICommonOutput*/ {
readonly amount: string;
readonly amount: NumericString;

readonly type: OutputType;

/**
* @param type The type of output.
* @param amount The amount of the output as big-integer or string.
*/
constructor(type: OutputType, amount: bigint | string) {
constructor(type: OutputType, amount: bigint | NumericString) {
this.type = type;
if (typeof amount == 'bigint') {
this.amount = amount.toString(10);
Expand Down
5 changes: 3 additions & 2 deletions bindings/nodejs/lib/types/block/output/unlock-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { plainToInstance, Type } from 'class-transformer';
import { NumericString } from '../../utils';
import { Address, AddressDiscriminator, AliasAddress } from '../address';

/**
Expand Down Expand Up @@ -118,7 +119,7 @@ class StorageDepositReturnUnlockCondition extends UnlockCondition /*implements I
/**
* The amount the consuming transaction must deposit to `returnAddress`.
*/
readonly amount: string;
readonly amount: NumericString;

/**
* The address to return the amount to.
Expand All @@ -132,7 +133,7 @@ class StorageDepositReturnUnlockCondition extends UnlockCondition /*implements I
* @param returnAddress The address to return the amount to.
* @param amount The amount the consuming transaction must deposit to `returnAddress`.
*/
constructor(returnAddress: Address, amount: bigint | string) {
constructor(returnAddress: Address, amount: bigint | NumericString) {
super(UnlockConditionType.StorageDepositReturn);
if (typeof amount == 'bigint') {
this.amount = amount.toString(10);
Expand Down
Loading

0 comments on commit 9e1fb6a

Please sign in to comment.