Skip to content
10 changes: 6 additions & 4 deletions src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ export class Asset {
*
* - `native`,
* - `credit_alphanum4`,
* - `credit_alphanum12`, or
* - `unknown` as the error case (which should never occur)
* - `credit_alphanum12`
* @throws {Error} Throws `Error` if asset type is unsupported.
*/
getAssetType(): AssetType | "unknown" {
getAssetType(): AssetType {
switch (this.getRawAssetType().value) {
case xdr.AssetType.assetTypeNative().value:
return AssetType.native;
Expand All @@ -204,7 +204,9 @@ export class Asset {
case xdr.AssetType.assetTypeCreditAlphanum12().value:
return AssetType.credit12;
default:
return "unknown";
throw new Error(
"Supported asset types are: native, credit_alphanum4, credit_alphanum12",
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/fee_bump_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { encodeMuxedAccountToAddress } from "./util/decode_encode_muxed_account.
* submitting to the network or forwarding on to additional signers.
*/
export class FeeBumpTransaction extends TransactionBase<xdr.FeeBumpTransaction> {
_feeSource: string;
_innerTransaction: Transaction;
private _feeSource: string;
private _innerTransaction: Transaction;

/**
* @param envelope - transaction envelope object or base64 encoded string.
Expand Down
15 changes: 12 additions & 3 deletions src/numbers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,33 @@ export function scValToBigInt(scv: xdr.ScVal): bigint {
case "scvI64":
case "scvTimepoint":
case "scvDuration":
if (scIntType === undefined) {
throw TypeError(`invalid integer type for ${switchName}`);
}
return new XdrLargeInt(
scIntType as ScIntType,
scIntType,
value as xdr.Int64 | xdr.Uint64,
).toBigInt();

case "scvU128":
case "scvI128": {
if (scIntType === undefined) {
throw TypeError(`invalid integer type for ${switchName}`);
}
const int128Value = value as xdr.Int128Parts | xdr.UInt128Parts;
return new XdrLargeInt(scIntType as ScIntType, [
return new XdrLargeInt(scIntType, [
int128Value.lo(),
int128Value.hi(),
]).toBigInt();
}

case "scvU256":
case "scvI256": {
if (scIntType === undefined) {
throw TypeError(`invalid integer type for ${switchName}`);
}
const int256Value = value as xdr.Int256Parts | xdr.UInt256Parts;
return new XdrLargeInt(scIntType as ScIntType, [
return new XdrLargeInt(scIntType, [
int256Value.loLo(),
int256Value.loHi(),
int256Value.hiLo(),
Expand Down
20 changes: 15 additions & 5 deletions src/numbers/xdr_large_int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type ScIntType =
*/
export class XdrLargeInt {
int: LargeInt;
type: string;
type: ScIntType;

/**
* @param type - specifies a data type to use to represent the integer, one
Expand Down Expand Up @@ -257,7 +257,10 @@ export class XdrLargeInt {
case "duration":
return this.toDuration();
default:
throw TypeError(`invalid type: ${this.type}`);
// This should be unreachable if the compiler enforces valid types
// This serves as a runtime check if the type is somehow invalid
// (e.g. from user input or a future extension)
throw TypeError(`invalid type: ${this.type as string}`);
}
}

Expand Down Expand Up @@ -286,7 +289,7 @@ export class XdrLargeInt {
}

/** Returns true if the given string is a valid XDR large integer type name. */
static isType(type: string): boolean {
static isType(type: string): type is ScIntType {
switch (type) {
case "i64":
case "i128":
Expand All @@ -307,8 +310,15 @@ export class XdrLargeInt {
* to a type description for {@link XdrLargeInt} construction (e.g. 'i128')
*
* @param scvType - the `xdr.ScValType` as a string
* @returns the corresponding {@link ScIntType} if it's an integer type, or
* `undefined` if it's not an integer type
*/
static getType(scvType: string): string {
return scvType.slice(3).toLowerCase();
static getType(scvType: string): ScIntType | undefined {
const type = scvType.slice(3).toLowerCase();
if (this.isType(type)) {
return type;
}

return undefined;
}
}
11 changes: 8 additions & 3 deletions src/operations/set_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,16 @@ export function setOptions(

if (setValues !== 1) {
throw new Error(
"Signer object must contain exactly one of signer.ed25519PublicKey, signer.sha256Hash, signer.preAuthTx.",
"Signer object must contain exactly one of signer.ed25519PublicKey, signer.sha256Hash, signer.preAuthTx, or signer.ed25519SignedPayload.",
);
}

signer = new xdr.Signer({ key: key!, weight: weight! });
if (weight === undefined) {
throw new Error("signer weight is required.");
}
if (key === undefined) {
throw new Error("signer key is required.");
}
signer = new xdr.Signer({ key: key, weight: weight });
}

const setOptionsOp = new xdr.SetOptionsOp({
Expand Down
34 changes: 16 additions & 18 deletions src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface LiquidityPoolWithdrawOpts {
export interface AllowTrustOpts {
trustor: string;
assetCode: string;
authorize?: boolean | number;
authorize?: TrustLineFlag | boolean;
source?: string;
}

Expand Down Expand Up @@ -105,8 +105,8 @@ export interface SignerOpts {

export interface SetOptionsOpts {
inflationDest?: string;
clearFlags?: number | string;
setFlags?: number | string;
clearFlags?: AuthFlags;
setFlags?: AuthFlags;
masterWeight?: number | string;
lowThreshold?: number | string;
medThreshold?: number | string;
Expand Down Expand Up @@ -411,20 +411,18 @@ export type OperationType =

// Literal types matching the AuthRequiredFlag/AuthRevocableFlag/AuthImmutableFlag/AuthClawbackEnabledFlag
// constants exported from src/operation.ts.
// TODO: Once src/index.js is migrated to src/index.ts, replace these literals with
// `typeof AuthRequiredFlag` etc. to avoid duplication with the runtime constants.
export namespace AuthFlag {
export type required = 1;
export type revocable = 2;
export type immutable = 4;
export type clawbackEnabled = 8;
}
export type AuthFlag =
| AuthFlag.clawbackEnabled
| AuthFlag.immutable
| AuthFlag.required
| AuthFlag.revocable;

export const AuthFlag = {
required: 1,
revocable: 2,
immutable: 4,
clawbackEnabled: 8,
} as const;
export type AuthFlag = (typeof AuthFlag)[keyof typeof AuthFlag];
/**
* A single {@link AuthFlag} or multiple flags combined with `|` (e.g. `AuthRequiredFlag | AuthRevocableFlag`).
*/
export type AuthFlags = AuthFlag | (number & {});
export namespace TrustLineFlag {
export type deauthorize = 0;
export type authorize = 1;
Expand Down Expand Up @@ -525,8 +523,8 @@ export interface SetOptionsResult<
// hold raw uint32 bitmasks, so combined values (e.g. AuthRequired | AuthRevocable = 3)
// are valid but not expressible as AuthFlag. Use bitwise AND to test individual flags:
// if (result.clearFlags & AuthRequiredFlag) { ... }
clearFlags?: AuthFlag;
setFlags?: AuthFlag;
clearFlags?: AuthFlags;
setFlags?: AuthFlags;
masterWeight?: number;
lowThreshold?: number;
medThreshold?: number;
Expand Down
2 changes: 1 addition & 1 deletion src/scval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export function nativeToScVal(

default:
if (XdrLargeInt.isType(optType)) {
return new XdrLargeInt(optType as ScIntType, val).toScVal();
return new XdrLargeInt(optType, val).toScVal();
}

throw new TypeError(
Expand Down
22 changes: 11 additions & 11 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ import { OperationRecord } from "./operations/types.js";
export class Transaction extends TransactionBase<
xdr.Transaction | xdr.TransactionV0
> {
_envelopeType: xdr.EnvelopeType;
_source: string = "";
_memo: xdr.Memo;
_sequence: string;
_operations: OperationRecord[];
_timeBounds?: { minTime: string; maxTime: string };
_ledgerBounds?: { minLedger: number; maxLedger: number };
_minAccountSequence?: string;
private _envelopeType: xdr.EnvelopeType;
private _source: string = "";
private _memo: xdr.Memo;
private _sequence: string;
private _operations: OperationRecord[];
private _timeBounds?: { minTime: string; maxTime: string };
private _ledgerBounds?: { minLedger: number; maxLedger: number };
private _minAccountSequence?: string;
// TODO: types/index.d.ts declares this as `number`, but the XDR type is
// Duration = Uint64 (64-bit), which can exceed Number.MAX_SAFE_INTEGER.
// This is a breaking type change that should be surfaced when types/index.d.ts is updated.
_minAccountSequenceAge?: xdr.Uint64;
_minAccountSequenceLedgerGap?: number;
_extraSigners?: xdr.SignerKey[];
private _minAccountSequenceAge?: xdr.Uint64;
private _minAccountSequenceLedgerGap?: number;
private _extraSigners?: xdr.SignerKey[];

/**
* @param envelope - transaction envelope object or base64 encoded string
Expand Down
8 changes: 4 additions & 4 deletions src/transaction_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Keypair } from "./keypair.js";
export class TransactionBase<
TTx extends xdr.FeeBumpTransaction | xdr.Transaction | xdr.TransactionV0,
> {
_tx: TTx;
_signatures: xdr.DecoratedSignature[];
_fee: string;
_networkPassphrase: string;
private _tx: TTx;
private _signatures: xdr.DecoratedSignature[];
private _fee: string;
private _networkPassphrase: string;

constructor(
tx: TTx,
Expand Down
2 changes: 1 addition & 1 deletion src/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class TransactionBuilder {

const builder = new TransactionBuilder(source, builderOpts);

tx._tx.operations().forEach((op) => builder.addOperation(op));
tx.tx.operations().forEach((op) => builder.addOperation(op));

return builder;
}
Expand Down
Loading