Skip to content

Commit 33f0e8e

Browse files
Merge pull request #7695 from BitGo/WIN-8153
feat(sdk-coin-iota): refactor iota sdk for beter readability
2 parents 3075ec1 + 81b347f commit 33f0e8e

File tree

8 files changed

+995
-626
lines changed

8 files changed

+995
-626
lines changed

modules/sdk-coin-iota/src/iota.ts

Lines changed: 263 additions & 123 deletions
Large diffs are not rendered by default.
Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,90 @@
1+
// ========================================
2+
// Address and Digest Length Constants
3+
// ========================================
4+
5+
/**
6+
* Length of IOTA addresses in characters (excluding 0x prefix).
7+
* IOTA uses 64-character hexadecimal addresses.
8+
*/
19
export const IOTA_ADDRESS_LENGTH = 64;
10+
11+
/**
12+
* Length of transaction digest in bytes.
13+
* Used for transaction IDs and hashes.
14+
*/
215
export const IOTA_TRANSACTION_DIGEST_LENGTH = 32;
16+
17+
/**
18+
* Length of block digest in bytes.
19+
* Used for block IDs and references.
20+
*/
321
export const IOTA_BLOCK_DIGEST_LENGTH = 32;
22+
23+
// ========================================
24+
// Cryptographic Constants
25+
// ========================================
26+
27+
/**
28+
* Length of Ed25519 signatures in bytes.
29+
* IOTA uses Ed25519 for transaction signing.
30+
*/
431
export const IOTA_SIGNATURE_LENGTH = 64;
5-
export const IOTA_KEY_BYTES_LENGTH = 32; // Ed25519 public key is 32 bytes
32+
33+
/**
34+
* Length of Ed25519 public keys in bytes.
35+
* Standard Ed25519 key size.
36+
*/
37+
export const IOTA_KEY_BYTES_LENGTH = 32;
38+
39+
// ========================================
40+
// Transaction Object Limits
41+
// ========================================
42+
43+
/**
44+
* Maximum number of input objects allowed in a single transaction.
45+
* This limit ensures transactions can be processed efficiently by the network.
46+
*/
647
export const MAX_INPUT_OBJECTS = 2048;
48+
49+
/**
50+
* Maximum number of gas payment objects allowed per transaction.
51+
* When exceeded, objects must be merged before the transaction can be built.
52+
* This prevents transaction size from becoming too large.
53+
*/
754
export const MAX_GAS_PAYMENT_OBJECTS = 256;
55+
56+
/**
57+
* Maximum number of recipients in a transfer transaction.
58+
* Limits the number of outputs to keep transaction size manageable.
59+
*/
60+
export const MAX_RECIPIENTS = 256;
61+
62+
// ========================================
63+
// Gas Configuration Limits
64+
// ========================================
65+
66+
/**
67+
* Maximum gas budget allowed for a transaction.
68+
* Used for dry-run simulations to estimate gas costs.
69+
* Set to a very high value (50 billion) to ensure simulation completes.
70+
*/
871
export const MAX_GAS_BUDGET = 50000000000;
72+
73+
/**
74+
* Maximum gas price for simulated transactions.
75+
* Used when building dry-run transactions for gas estimation.
76+
*/
977
export const MAX_GAS_PRICE = 100000;
10-
export const MAX_RECIPIENTS = 256; // Maximum number of recipients in a transfer transaction
78+
79+
// ========================================
80+
// Transaction Command Types
81+
// ========================================
82+
83+
/**
84+
* Valid command types for transfer transactions.
85+
* Transfer transactions can only contain these three command types:
86+
* - SplitCoins: Split a coin into multiple coins with specified amounts
87+
* - MergeCoins: Merge multiple coins into a single coin
88+
* - TransferObjects: Transfer coins/objects to recipients
89+
*/
1190
export const TRANSFER_TRANSACTION_COMMANDS = ['SplitCoins', 'MergeCoins', 'TransferObjects'];

modules/sdk-coin-iota/src/lib/iface.ts

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,199 @@ import {
66
TransactionType,
77
} from '@bitgo/sdk-core';
88

9+
/**
10+
* Extended transaction explanation for IOTA transactions.
11+
* Includes IOTA-specific fields like sender and optional sponsor.
12+
*
13+
* @example
14+
* ```typescript
15+
* const explanation: TransactionExplanation = {
16+
* type: TransactionType.Send,
17+
* sender: '0x1234...',
18+
* sponsor: '0x5678...', // Optional gas sponsor
19+
* outputAmount: '1000000',
20+
* outputs: [...],
21+
* fee: { fee: '5000' }
22+
* };
23+
* ```
24+
*/
925
export interface TransactionExplanation extends BaseTransactionExplanation {
26+
/** The type of transaction (e.g., Send, Receive) */
1027
type: BitGoTransactionType;
28+
29+
/** The address initiating the transaction */
1130
sender: string;
31+
32+
/**
33+
* Optional gas sponsor address.
34+
* When present, this address pays for the transaction's gas fees
35+
* instead of the sender.
36+
*/
1237
sponsor?: string;
1338
}
1439

40+
/**
41+
* Represents an IOTA object (coin or NFT) used as transaction input.
42+
* Objects in IOTA are versioned and content-addressable.
43+
*
44+
* @example
45+
* ```typescript
46+
* const coinObject: TransactionObjectInput = {
47+
* objectId: '0x1234...', // Unique object identifier
48+
* version: '42', // Object version number
49+
* digest: 'ABC123...' // Content hash
50+
* };
51+
* ```
52+
*/
1553
export type TransactionObjectInput = {
54+
/** Unique identifier for the object (64-character hex string) */
1655
objectId: string;
56+
57+
/** Version number of the object (as string) */
1758
version: string;
59+
60+
/** Base58-encoded digest of the object's content */
1861
digest: string;
1962
};
2063

64+
/**
65+
* Gas configuration for IOTA transactions.
66+
* All fields are optional to support both simulation and real transactions.
67+
*
68+
* @example
69+
* ```typescript
70+
* const gasData: GasData = {
71+
* gasBudget: 5000000, // Maximum gas units to spend
72+
* gasPrice: 1000, // Price per gas unit
73+
* gasPaymentObjects: [coinObject1, coinObject2] // Coins to pay gas
74+
* };
75+
* ```
76+
*/
2177
export type GasData = {
78+
/**
79+
* Maximum amount of gas units this transaction can consume.
80+
* Measured in gas units.
81+
*/
2282
gasBudget?: number;
83+
84+
/**
85+
* Price per gas unit in MIST (smallest IOTA unit).
86+
* Total fee = gasBudget * gasPrice
87+
*/
2388
gasPrice?: number;
89+
90+
/**
91+
* Array of coin objects used to pay for gas.
92+
* These objects will be consumed to cover the transaction fee.
93+
*/
2494
gasPaymentObjects?: TransactionObjectInput[];
2595
};
2696

2797
/**
28-
* The transaction data returned from the toJson() function of a transaction
98+
* Base transaction data returned from the toJson() function.
99+
* Contains common fields present in all IOTA transactions.
100+
*
101+
* @example
102+
* ```typescript
103+
* const txData: TxData = {
104+
* type: TransactionType.Send,
105+
* sender: '0x1234...',
106+
* gasBudget: 5000000,
107+
* gasPrice: 1000,
108+
* gasPaymentObjects: [...],
109+
* gasSponsor: '0x5678...' // Optional
110+
* };
111+
* ```
29112
*/
30113
export interface TxData {
114+
/** Transaction ID (digest), available after transaction is built */
31115
id?: string;
116+
117+
/** Address of the transaction sender */
32118
sender: string;
119+
120+
/** Maximum gas units allocated for this transaction */
33121
gasBudget?: number;
122+
123+
/** Price per gas unit in MIST */
34124
gasPrice?: number;
125+
126+
/** Coin objects used to pay for gas */
35127
gasPaymentObjects?: TransactionObjectInput[];
128+
129+
/**
130+
* Optional address that sponsors (pays for) the gas.
131+
* If not provided, the sender pays for gas.
132+
*/
36133
gasSponsor?: string;
134+
135+
/** Type of the transaction */
37136
type: TransactionType;
38137
}
39138

139+
/**
140+
* Transfer transaction data with recipient information.
141+
* Extends TxData with transfer-specific fields.
142+
*
143+
* @example
144+
* ```typescript
145+
* const transferData: TransferTxData = {
146+
* type: TransactionType.Send,
147+
* sender: '0x1234...',
148+
* recipients: [
149+
* { address: '0xabcd...', amount: '1000000' },
150+
* { address: '0xef01...', amount: '2000000' }
151+
* ],
152+
* paymentObjects: [coinObject],
153+
* gasBudget: 5000000,
154+
* gasPrice: 1000,
155+
* gasPaymentObjects: [gasObject]
156+
* };
157+
* ```
158+
*/
40159
export interface TransferTxData extends TxData {
160+
/**
161+
* Array of recipients and the amounts they receive.
162+
* Each recipient must have a valid IOTA address and amount.
163+
*/
41164
recipients: TransactionRecipient[];
165+
166+
/**
167+
* Optional coin objects used as payment sources.
168+
* These are split and transferred to recipients.
169+
* If not provided, gas objects are used for payment.
170+
*/
42171
paymentObjects?: TransactionObjectInput[];
43172
}
44173

174+
/**
175+
* Options for explaining an IOTA transaction.
176+
*
177+
* @example
178+
* ```typescript
179+
* const explanation = await iota.explainTransaction({
180+
* txHex: '0x1234...' // Raw transaction hex
181+
* });
182+
* ```
183+
*/
45184
export interface ExplainTransactionOptions {
185+
/** Raw transaction data in hexadecimal format */
46186
txHex: string;
47187
}
48188

189+
/**
190+
* Options for parsing an IOTA transaction.
191+
* Extends base parsing options with IOTA-specific requirements.
192+
*
193+
* @example
194+
* ```typescript
195+
* const parsed = await iota.parseTransaction({
196+
* txHex: '0x1234...' // Raw transaction hex
197+
* });
198+
* // Returns: { inputs: [...], outputs: [...], fee: BigNumber }
199+
* ```
200+
*/
49201
export interface IotaParseTransactionOptions extends BaseParseTransactionOptions {
202+
/** Raw transaction data in hexadecimal format */
50203
txHex: string;
51204
}

0 commit comments

Comments
 (0)