@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
288 lines (287 loc) • 7.82 kB
JavaScript
import { bcs, fromB58, fromB64, fromHEX, toB58, toB64, toHEX } from "@mysten/bcs";
import { isValidSuiAddress, normalizeSuiAddress, SUI_ADDRESS_LENGTH } from "../utils/sui-types.js";
import { TypeTagSerializer } from "./type-tag-serializer.js";
function unsafe_u64(options) {
return bcs.u64({
name: "unsafe_u64",
...options
}).transform({
input: (val) => val,
output: (val) => Number(val)
});
}
function optionEnum(type) {
return bcs.enum("Option", {
None: null,
Some: type
});
}
const Address = bcs.bytes(SUI_ADDRESS_LENGTH).transform({
validate: (val) => {
const address = typeof val === "string" ? val : toHEX(val);
if (!address || !isValidSuiAddress(normalizeSuiAddress(address))) {
throw new Error(`Invalid Sui address ${address}`);
}
},
input: (val) => typeof val === "string" ? fromHEX(normalizeSuiAddress(val)) : val,
output: (val) => normalizeSuiAddress(toHEX(val))
});
const ObjectDigest = bcs.vector(bcs.u8()).transform({
name: "ObjectDigest",
input: (value) => fromB58(value),
output: (value) => toB58(new Uint8Array(value)),
validate: (value) => {
if (fromB58(value).length !== 32) {
throw new Error("ObjectDigest must be 32 bytes");
}
}
});
const SuiObjectRef = bcs.struct("SuiObjectRef", {
objectId: Address,
version: bcs.u64(),
digest: ObjectDigest
});
const SharedObjectRef = bcs.struct("SharedObjectRef", {
objectId: Address,
initialSharedVersion: bcs.u64(),
mutable: bcs.bool()
});
const ObjectArg = bcs.enum("ObjectArg", {
ImmOrOwnedObject: SuiObjectRef,
SharedObject: SharedObjectRef,
Receiving: SuiObjectRef
});
const CallArg = bcs.enum("CallArg", {
Pure: bcs.struct("Pure", {
bytes: bcs.vector(bcs.u8()).transform({
input: (val) => typeof val === "string" ? fromB64(val) : val,
output: (val) => toB64(new Uint8Array(val))
})
}),
Object: ObjectArg
});
const InnerTypeTag = bcs.enum("TypeTag", {
bool: null,
u8: null,
u64: null,
u128: null,
address: null,
signer: null,
vector: bcs.lazy(() => InnerTypeTag),
struct: bcs.lazy(() => StructTag),
u16: null,
u32: null,
u256: null
});
const TypeTag = InnerTypeTag.transform({
input: (typeTag) => typeof typeTag === "string" ? TypeTagSerializer.parseFromStr(typeTag, true) : typeTag,
output: (typeTag) => TypeTagSerializer.tagToString(typeTag)
});
const Argument = bcs.enum("Argument", {
GasCoin: null,
Input: bcs.u16(),
Result: bcs.u16(),
NestedResult: bcs.tuple([bcs.u16(), bcs.u16()])
});
const ProgrammableMoveCall = bcs.struct("ProgrammableMoveCall", {
package: Address,
module: bcs.string(),
function: bcs.string(),
typeArguments: bcs.vector(TypeTag),
arguments: bcs.vector(Argument)
});
const Command = bcs.enum("Command", {
/**
* A Move Call - any public Move function can be called via
* this transaction. The results can be used that instant to pass
* into the next transaction.
*/
MoveCall: ProgrammableMoveCall,
/**
* Transfer vector of objects to a receiver.
*/
TransferObjects: bcs.struct("TransferObjects", {
objects: bcs.vector(Argument),
address: Argument
}),
// /**
// * Split `amount` from a `coin`.
// */
SplitCoins: bcs.struct("SplitCoins", {
coin: Argument,
amounts: bcs.vector(Argument)
}),
// /**
// * Merge Vector of Coins (`sources`) into a `destination`.
// */
MergeCoins: bcs.struct("MergeCoins", {
destination: Argument,
sources: bcs.vector(Argument)
}),
// /**
// * Publish a Move module.
// */
Publish: bcs.struct("Publish", {
modules: bcs.vector(
bcs.vector(bcs.u8()).transform({
input: (val) => typeof val === "string" ? fromB64(val) : val,
output: (val) => toB64(new Uint8Array(val))
})
),
dependencies: bcs.vector(Address)
}),
// /**
// * Build a vector of objects using the input arguments.
// * It is impossible to export construct a `vector<T: key>` otherwise,
// * so this call serves a utility function.
// */
MakeMoveVec: bcs.struct("MakeMoveVec", {
type: optionEnum(TypeTag).transform({
input: (val) => val === null ? {
None: true
} : {
Some: val
},
output: (val) => val.Some ?? null
}),
elements: bcs.vector(Argument)
}),
Upgrade: bcs.struct("Upgrade", {
modules: bcs.vector(
bcs.vector(bcs.u8()).transform({
input: (val) => typeof val === "string" ? fromB64(val) : val,
output: (val) => toB64(new Uint8Array(val))
})
),
dependencies: bcs.vector(Address),
package: Address,
ticket: Argument
})
});
const ProgrammableTransaction = bcs.struct("ProgrammableTransaction", {
inputs: bcs.vector(CallArg),
commands: bcs.vector(Command)
});
const TransactionKind = bcs.enum("TransactionKind", {
ProgrammableTransaction,
ChangeEpoch: null,
Genesis: null,
ConsensusCommitPrologue: null
});
const TransactionExpiration = bcs.enum("TransactionExpiration", {
None: null,
Epoch: unsafe_u64()
});
const StructTag = bcs.struct("StructTag", {
address: Address,
module: bcs.string(),
name: bcs.string(),
typeParams: bcs.vector(InnerTypeTag)
});
const GasData = bcs.struct("GasData", {
payment: bcs.vector(SuiObjectRef),
owner: Address,
price: bcs.u64(),
budget: bcs.u64()
});
const TransactionDataV1 = bcs.struct("TransactionDataV1", {
kind: TransactionKind,
sender: Address,
gasData: GasData,
expiration: TransactionExpiration
});
const TransactionData = bcs.enum("TransactionData", {
V1: TransactionDataV1
});
const IntentScope = bcs.enum("IntentScope", {
TransactionData: null,
TransactionEffects: null,
CheckpointSummary: null,
PersonalMessage: null
});
const IntentVersion = bcs.enum("IntentVersion", {
V0: null
});
const AppId = bcs.enum("AppId", {
Sui: null
});
const Intent = bcs.struct("Intent", {
scope: IntentScope,
version: IntentVersion,
appId: AppId
});
function IntentMessage(T) {
return bcs.struct(`IntentMessage<${T.name}>`, {
intent: Intent,
value: T
});
}
const CompressedSignature = bcs.enum("CompressedSignature", {
ED25519: bcs.fixedArray(64, bcs.u8()),
Secp256k1: bcs.fixedArray(64, bcs.u8()),
Secp256r1: bcs.fixedArray(64, bcs.u8()),
ZkLogin: bcs.vector(bcs.u8())
});
const PublicKey = bcs.enum("PublicKey", {
ED25519: bcs.fixedArray(32, bcs.u8()),
Secp256k1: bcs.fixedArray(33, bcs.u8()),
Secp256r1: bcs.fixedArray(33, bcs.u8()),
ZkLogin: bcs.vector(bcs.u8())
});
const MultiSigPkMap = bcs.struct("MultiSigPkMap", {
pubKey: PublicKey,
weight: bcs.u8()
});
const MultiSigPublicKey = bcs.struct("MultiSigPublicKey", {
pk_map: bcs.vector(MultiSigPkMap),
threshold: bcs.u16()
});
const MultiSig = bcs.struct("MultiSig", {
sigs: bcs.vector(CompressedSignature),
bitmap: bcs.u16(),
multisig_pk: MultiSigPublicKey
});
const base64String = bcs.vector(bcs.u8()).transform({
input: (val) => typeof val === "string" ? fromB64(val) : val,
output: (val) => toB64(new Uint8Array(val))
});
const SenderSignedTransaction = bcs.struct("SenderSignedTransaction", {
intentMessage: IntentMessage(TransactionData),
txSignatures: bcs.vector(base64String)
});
const SenderSignedData = bcs.vector(SenderSignedTransaction, {
name: "SenderSignedData"
});
export {
Address,
AppId,
Argument,
CallArg,
Command,
CompressedSignature,
GasData,
Intent,
IntentMessage,
IntentScope,
IntentVersion,
MultiSig,
MultiSigPkMap,
MultiSigPublicKey,
ObjectArg,
ObjectDigest,
ProgrammableMoveCall,
ProgrammableTransaction,
PublicKey,
SenderSignedData,
SenderSignedTransaction,
SharedObjectRef,
StructTag,
SuiObjectRef,
TransactionData,
TransactionDataV1,
TransactionExpiration,
TransactionKind,
TypeTag,
base64String
};
//# sourceMappingURL=bcs.js.map