Overview
Discover how the SUI Extension integrates with the Credenza platform to leverage blockchain technology for digital rights management. This powerful tool enables developers to manage digital assets and interactions efficiently within a Web SDK setting.
Installation
To install the Credenza SUI Extension, use the following npm command:
npm i @mysten/sui
npm i @credenza3/core-web-sui-extimport { SuiExtension } from '@credenza3/core-web-sui-ext'Create SDK Instance
To create an SDK instance with the SUI Extension, use the CredenzaSDK constructor. You need to pass an array of extensions where you include an instance of SuiExtension. By default, the network is set to SuiExtension.SUI_NETWORK.MAINNET.
Here is a code snippet illustrating this setup:
const sdk = new CredenzaSDK({
extensions: [
new SuiExtension({ suiNetwork: SuiExtension.SUI_NETWORK.MAINNET })
],
});
Choose the Right Network Environment
Selecting the correct network is key for proper SDK operation. Incorrect choices can lead to issues like data mismatches or transaction errors. Always verify your network setting during initialization to avoid disruptions.
Methods and Properties
Switch Network
To switch networks using the Credenza SDK, use the switchNetwork method from the SuiExtension. This allows you to dynamically change the network environment.
Here is a code snippet demonstrating network switching:
await sdk.sui.switchNetwork(SuiExtension.SUI_NETWORK.<NETWORK_NAME>);```Replace <NETWORK_NAME> with DEVNET, TESTNET, or MAINNET.
The switchNetwork method returns an object with the following properties:
client: SuiClient instancenetwork: The name of the network as a string
Retrieve SUI SDK Client
sdk.sui.getSuiClient(): SuiClient
To get the SUI SDK client, use the getSuiClient method from the sdk.sui object. This method returns an instance of SuiClient.
const client = sdk.sui.getSuiClient(): SuiClient
Retrieve SUI SDK GraphQL Client
sdk.sui.getSuiGqlClient(): SuiGraphQLClient
To retrieve the SUI SDK GraphQL client, you can use the provided method.
The following code snippet demonstrates how to get the SUI SDK GraphQL client:
const gqlClient = sdk.sui.getSuiGqlClient(): SuiGraphQLClient
The object returned by this method is of type SuiGraphQLClient.
Retrieve Current SUI Network Name
sdk.sui.getNetworkName(): string
To retrieve the current SUI network name, you can use the getNetworkName method of the Credenza SDK's SuiExtension. This method returns a string representing the network name.
const network = sdk.sui.getNetworkName();
console.log(network); // Outputs the current network name, e.g., 'mainnet'Retrieve Current SUI Address
sdk.sui.getAddress(): Promise<string>
To retrieve the current SUI address, use the getAddress method of the sdk.sui object. The method returns a string type value representing the current SUI address.
Here is a sample code snippet demonstrating how to get the current SUI address:
const address = await sdk.sui.getAddress();
console.log(address); // Outputs the current SUI address
Sign Personal Message
sdk.sui.signPersonalMessage(message: string): Promise<{ signature: string; bytes: string }>
To sign a personal message using the SUI SDK, you can use the signPersonalMessage method. This method requires a string message as input and returns a promise that resolves to an object containing the signature and bytes of the signed message.
Here is the code snippet for signing a personal message:
const message = 'Your message here';
const result = await sdk.sui.signPersonalMessage(message);
console.log(result.signature); // Signed message signature
console.log(result.bytes); // Signed message bytes
The signPersonalMessage method returns a Promise that resolves to an object with two properties:
* signature: the signature of the signed message.
* bytes: the bytes of the signed message.
Sign Transaction Block
sdk.sui.signTransactionBlock(tbx: Transaction): Promise<{ signature: string; transactionBlock: Uint8Array }>
You can sign a transaction block using the signTransactionBlock method. This method returns a Promise that resolves to an object containing the signature and the transaction block as a Uint8Array.
Here's how to sign a transaction block:
const result = await sdk.sui.signTransactionBlock(tbx: Transaction): Promise<{signature: string; transactionBlock: Uint8Array}>
Sign and Execute Transaction Block
sdk.sui.signAndExecuteTransactionBlock(tbx: Transaction): Promise<{ signature: string; transactionBlock: Uint8Array }>
To sign and execute a transaction block, use the signAndExecuteTransactionBlock method. This method requires a Transaction object as its parameter and returns a Promise.
Below is an example code snippet that demonstrates the process:
const result = await sdk.sui.signAndExecuteTransactionBlock(tbx: Transaction);
The result will be a Promise resolving to an object containing the transaction signature and the transaction block (in Uint8Array format).
Constants
SUI_NETWORK = {
DEVNET: 'devnet',
TESTNET: 'testnet',
MAINNET: 'mainnet',
}