Skip to content

Commit

Permalink
Merge branch '2.0' into version-signaling-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Nov 23, 2023
2 parents 403b115 + 7c397a2 commit 7e03f52
Show file tree
Hide file tree
Showing 54 changed files with 468 additions and 557 deletions.
4 changes: 2 additions & 2 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use iota_sdk::{
DelegationId, FoundryId, NftId, OutputId, TokenScheme,
},
payload::{dto::PayloadDto, signed_transaction::TransactionId},
BlockDto, BlockId, IssuerId,
BlockDto, BlockId,
},
utils::serde::{option_string, string},
};
Expand Down Expand Up @@ -126,7 +126,7 @@ pub enum ClientMethod {
#[serde(rename_all = "camelCase")]
BuildBasicBlock {
/// The issuer's ID.
issuer_id: IssuerId,
issuer_id: AccountId,
/// The block payload.
#[serde(default)]
payload: Option<PayloadDto>,
Expand Down
4 changes: 4 additions & 0 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ pub enum WalletMethod {
/// Returns the implicit account creation address of the wallet if it is Ed25519 based.
/// Expected response: [`Bech32Address`](crate::Response::Bech32Address)
ImplicitAccountCreationAddress,
/// Prepares to transition an implicit account to an account.
/// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction)
#[serde(rename_all = "camelCase")]
PrepareImplicitAccountTransition { output_id: OutputId },
/// Returns the implicit accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
ImplicitAccounts,
Expand Down
4 changes: 4 additions & 0 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
let implicit_account_creation_address = wallet.implicit_account_creation_address().await?;
Response::Bech32Address(implicit_account_creation_address)
}
WalletMethod::PrepareImplicitAccountTransition { output_id } => {
let data = wallet.prepare_implicit_account_transition(&output_id).await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
WalletMethod::ImplicitAccounts => {
let implicit_accounts = wallet.implicit_accounts().await;
Response::OutputsData(implicit_accounts.iter().map(OutputDataDto::from).collect())
Expand Down
1 change: 1 addition & 0 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub enum Response {
/// - [`PrepareStopParticipating`](crate::method::WalletMethod::PrepareStopParticipating)
/// - [`PrepareTransaction`](crate::method::WalletMethod::PrepareTransaction)
/// - [`PrepareVote`](crate::method::WalletMethod::PrepareVote)
/// - [`PrepareImplicitAccountTransition`](crate::method::WalletMethod::PrepareImplicitAccountTransition)
PreparedTransaction(PreparedTransactionDataDto),
/// Response for:
/// - [`PrepareCreateNativeToken`](crate::method::WalletMethod::PrepareCreateNativeToken),
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use iota_sdk::{
types::{
block::{
payload::{dto::PayloadDto, Payload, TaggedDataPayload},
Block, BlockBodyDto, IssuerId,
Block, BlockBodyDto,
},
TryFromDto,
},
Expand Down Expand Up @@ -126,7 +126,7 @@ async fn client_from_wallet() -> Result<()> {
// let response = call_client_method(
// &client,
// ClientMethod::BuildBasicBlock {
// issuer_id: IssuerId::null(),
// issuer_id: AccountId::null(),
// payload: Some(payload.clone()),
// },
// )
Expand Down
13 changes: 8 additions & 5 deletions bindings/nodejs/examples/client/05-get-address-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ async function run() {
for (const outputResponse of addressOutputs) {
const output = outputResponse['output'];
if (output instanceof CommonOutput) {
(output as CommonOutput).getNativeTokens()?.forEach((token) => {
totalNativeTokens[token.id] =
(totalNativeTokens[token.id] || BigInt(0)) +
token.amount;
});
const nativeTokenFeature = (
output as CommonOutput
).getNativeToken();
if (nativeTokenFeature != undefined) {
totalNativeTokens[nativeTokenFeature.id] =
(totalNativeTokens[nativeTokenFeature.id] ||
BigInt(0)) + nativeTokenFeature.amount;
}
}

totalAmount += output.getAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ async function run() {
outputs.forEach(({ output, address }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native tokens: %s',
'- address: %s\n- amount: %d\n- native token: %s',
Utils.hexToBech32(address.toString(), 'rms'),
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeTokens()
? (output as CommonOutput).getNativeToken() ?? []
: [],
);
});
Expand Down Expand Up @@ -91,7 +91,7 @@ async function run() {
Utils.hexToBech32(address.toString(), 'rms'),
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeTokens()
? (output as CommonOutput).getNativeToken()
: undefined,
);
});
Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
NftId,
FoundryId,
DelegationId,
IssuerId,
UnsignedBlock,
parseUnsignedBlock,
} from '../types/block';
Expand Down Expand Up @@ -273,7 +272,7 @@ export class Client {
* @returns The block ID followed by the block containing the payload.
*/
async buildBasicBlock(
issuerId: IssuerId,
issuerId: AccountId,
payload?: Payload,
): Promise<UnsignedBlock> {
const response = await this.methodHandler.callMethod({
Expand Down
10 changes: 5 additions & 5 deletions bindings/nodejs/lib/types/block/core/signed-block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { IssuerId } from '../id';
import { AccountId } from '../id';
import { Signature, SignatureDiscriminator } from '../signature';
import { SlotCommitmentId, SlotIndex } from '../slot';
import { u64 } from '../../utils/type-aliases';
Expand Down Expand Up @@ -38,7 +38,7 @@ class SignedBlock {
/**
* The identifier of the account that issued this block.
*/
readonly issuerId!: IssuerId;
readonly issuerId!: AccountId;

@Type(() => Block, {
discriminator: BlockDiscriminator,
Expand All @@ -59,7 +59,7 @@ class SignedBlock {
issuingTime: u64,
slotCommitmentId: SlotCommitmentId,
latestFinalizedSlot: SlotIndex,
issuerId: IssuerId,
issuerId: AccountId,
block: Block,
signature: Signature,
) {
Expand Down Expand Up @@ -147,7 +147,7 @@ class UnsignedBlock {
/**
* The identifier of the account that issued this block.
*/
readonly issuerId!: IssuerId;
readonly issuerId!: AccountId;

@Type(() => Block, {
discriminator: BlockDiscriminator,
Expand All @@ -160,7 +160,7 @@ class UnsignedBlock {
issuingTime: u64,
slotCommitmentId: SlotCommitmentId,
latestFinalizedSlot: SlotIndex,
issuerId: IssuerId,
issuerId: AccountId,
block: Block,
) {
this.protocolVersion = protocolVersion;
Expand Down
5 changes: 0 additions & 5 deletions bindings/nodejs/lib/types/block/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export type TokenId = HexEncodedString;
*/
export type FoundryId = HexEncodedString;

/**
* An Issuer ID represented as hex-encoded string.
*/
export type IssuerId = HexEncodedString;

/**
* Unique identifier of the Delegation Output, which is the BLAKE2b-256 hash of the Output ID that created it.
*/
Expand Down
40 changes: 39 additions & 1 deletion bindings/nodejs/lib/types/block/output/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
BlockIssuerKey,
BlockIssuerKeyDiscriminator,
} from './block-issuer-key';
import { u64 } from '../../utils/type-aliases';
import { u256, u64 } from '../../utils/type-aliases';
import { EpochIndex } from '../../block/slot';
import { INativeToken } from '../../models/native-token';
import { HexEncodedString } from '../../utils/hex-encoding';

/**
* All of the feature block types.
Expand Down Expand Up @@ -113,6 +115,40 @@ class TagFeature extends Feature {
}
}

/**
* Native token feature.
*/
class NativeTokenFeature extends Feature {
/**
* Identifier of the native token.
*/
readonly id: HexEncodedString;
/**
* Amount of native tokens of the given Token ID.
*/
readonly amount: u256;

/**
* Creates a new `NativeTokenFeature`.
* @param nativeToken The native token stored with the feature.
*/
constructor(nativeToken: INativeToken) {
super(FeatureType.NativeToken);
this.id = nativeToken.id;
this.amount = nativeToken.amount;
}

/**
* Returns the native token contained in this feature.
*/
public asNativeToken(): INativeToken {
return {
id: this.id,
amount: this.amount,
};
}
}

/**
* Block Issuer feature.
*/
Expand Down Expand Up @@ -178,6 +214,7 @@ const FeatureDiscriminator = {
{ value: IssuerFeature, name: FeatureType.Issuer as any },
{ value: MetadataFeature, name: FeatureType.Metadata as any },
{ value: TagFeature, name: FeatureType.Tag as any },
{ value: NativeTokenFeature, name: FeatureType.NativeToken as any },
{ value: BlockIssuerFeature, name: FeatureType.BlockIssuer as any },
{ value: StakingFeature, name: FeatureType.Staking as any },
],
Expand All @@ -191,6 +228,7 @@ export {
IssuerFeature,
MetadataFeature,
TagFeature,
NativeTokenFeature,
BlockIssuerFeature,
StakingFeature,
};
28 changes: 12 additions & 16 deletions bindings/nodejs/lib/types/block/output/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
UnlockCondition,
UnlockConditionDiscriminator,
} from './unlock-condition';
import { Feature, FeatureDiscriminator } from './feature';
import { Feature, FeatureDiscriminator, NativeTokenFeature } from './feature';

// Temp solution for not double parsing JSON
import { plainToInstance, Type } from 'class-transformer';
import { HexEncodedString, hexToBigInt, NumericString, u64 } from '../../utils';
import { HexEncodedString, NumericString, u64 } from '../../utils';
import { TokenScheme, TokenSchemeDiscriminator } from './token-scheme';
import { INativeToken } from '../../models';
import { AccountId, NftId, AnchorId, DelegationId } from '../id';
import { EpochIndex } from '../../block/slot';
import { INativeToken } from '../../models/native-token';

export type OutputId = HexEncodedString;

Expand Down Expand Up @@ -102,9 +102,6 @@ abstract class CommonOutput extends Output {
})
readonly unlockConditions: UnlockCondition[];

// Getter transforms it into nativeTokens with a proper number
private nativeTokens?: INativeToken[];

/**
* The features contained by the output.
*/
Expand All @@ -126,22 +123,21 @@ abstract class CommonOutput extends Output {
super(type, amount);
this.unlockConditions = unlockConditions;
}

/**
* The native tokens held by the output.
* The native token held by the output.
*/
getNativeTokens(): INativeToken[] | undefined {
if (!this.nativeTokens) {
return this.nativeTokens;
getNativeToken(): INativeToken | undefined {
if (!this.features) {
return undefined;
}

// Make sure the amount of native tokens are of bigint type.
for (let i = 0; i < this.nativeTokens.length; i++) {
const token = this.nativeTokens[i];
if (typeof token.amount === 'string') {
this.nativeTokens[i].amount = hexToBigInt(token.amount);
for (const feature of this.features) {
if (feature instanceof NativeTokenFeature) {
return (feature as NativeTokenFeature).asNativeToken();
}
}
return this.nativeTokens;
return undefined;
}
}

Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/lib/types/client/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
SignedBlock,
BlockId,
FoundryId,
IssuerId,
AnchorId,
NftId,
DelegationId,
Expand Down Expand Up @@ -111,7 +110,7 @@ export interface __SignatureUnlockMethod__ {
export interface __BuildBasicBlockMethod__ {
name: 'buildBasicBlock';
data: {
issuerId: IssuerId;
issuerId: AccountId;
payload?: Payload;
};
}
Expand Down
Loading

0 comments on commit 7e03f52

Please sign in to comment.