The SafleID SDK is a JavaScript library designed to interact with the SafleID ecosystem. It provides functionality to validate, register, and resolve SafleIDs on blockchain networks through smart contracts and a relayer API.
- Validate SafleIDs against predefined rules.
- Check the registration status of SafleIDs.
- Resolve user addresses to SafleIDs and vice versa.
- Retrieve SafleID registration fees.
- Register a new SafleID using a relayer service.
Install the required dependencies:
npm install @getsafle/safle-gaming-sdk
Add this SDK to your project:
const { SafleID } = require('@getsafle/safle-gaming-sdk');
const { SafleID } = require("@getsafle/safle-gaming-sdk");
const config = {
env: "testnet", // Options: 'testnet' or 'mainnet'
rpcUrl: "https://your-rpc-url.com",
mainContractAddress: "0xMainContractAddress",
storageContractAddress: "0xStorageContractAddress",
chainId: 1, // Ethereum Mainnet or Testnet Chain ID
relayerUrl: "https://your-relayer-url.com",
relayerApiKey: "your-api-key",
};
const safleID = new SafleID(config);
Validates if the given SafleID is valid based on its length and characters.
-
Parameters:
safleId
(string): SafleID to validate.
-
Returns:
true
if valid, otherwisefalse
.
Checks if SafleID registration is paused.
- Returns:
true
if registration is paused, otherwisefalse
.
Fetches the SafleID associated with a user's address.
-
Parameters:
userAddress
(string): Ethereum address.
-
Returns:
- SafleID (string) or an error message.
Fetches the Ethereum address associated with a SafleID.
-
Parameters:
safleId
(string): The SafleID to resolve.
-
Returns:
- Ethereum address (string) or a fallback value.
Fetches the SafleID registration fee.
- Returns:
- Registration fee (string).
Registers a new SafleID through the relayer.
-
Parameters:
payload
(object): ContainsuserAddress
andsafleId
.
-
Returns:
- Registration response (object) or an error message.
(async () => {
const safleId = "mySafleId";
const userAddress = "0xYourEthereumAddress";
// Check if a SafleID is valid
const isValid = await safleID.isSafleIdValid(safleId);
console.log(`Is SafleID valid: ${isValid}`);
// Check registration status
const isPaused = await safleID.isRegistrationPaused();
console.log(`Registration Paused: ${isPaused}`);
// Fetch the SafleID for an address
const userSafleID = await safleID.getSafleId(userAddress);
console.log(`User's SafleID: ${userSafleID}`);
// Fetch the address for an SafleID
const userAddress = await safleID.getAddress(safleId);
console.log(`User's Address: ${userAddress}`);
// Register a new SafleID
const response = await safleID.setSafleId({ userAddress, safleId });
console.log("Registration Response:", response);
})();
The SDK includes predefined error messages stored in ./constants/errors.js
. These errors handle invalid inputs, paused registration, already-taken SafleIDs, and more.
- Ensure the
relayerUrl
andrelayerApiKey
are configured correctly. - Use a secure environment to store API keys and sensitive configurations.
- The
rejectUnauthorized: false
option is for testing purposes only. For production, ensure the HTTPS connection is secure.
MIT
Feel free to contribute to the project or report issues. Happy coding! 🎉