@wasserstoff/tribes-sdk
Version:
SDK for integrating with Tribes by Astrix platform on any EVM compatible chain
70 lines (69 loc) • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSpendSignature = createSpendSignature;
exports.createPointRedemptionSignature = createPointRedemptionSignature;
exports.verifySpendSignature = verifySpendSignature;
exports.verifyPointRedemptionSignature = verifyPointRedemptionSignature;
const ethers_1 = require("ethers");
/**
* Create a signature for spending tokens on behalf of an organization
* @param signer Signer to sign the message
* @param organization Organization address
* @param recipient Recipient address
* @param amount Amount to spend
* @param reason Reason for spending
* @returns Signature that can be used with spendWithSignature
*/
async function createSpendSignature(signer, organization, recipient, amount, reason) {
// Create message hash (matches the one in TokenDispenser.sol)
const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(['address', 'address', 'uint256', 'string'], [organization, recipient, amount, reason]));
// Create Ethereum signed message
const signature = await signer.signMessage(ethers_1.ethers.getBytes(messageHash));
return signature;
}
/**
* Create a signature for point redemption
* @param signer Signer to sign the message
* @param user User address
* @param points Number of points
* @param collectibleType Type of collectible
* @returns Signature that can be used with redeemPoints
*/
async function createPointRedemptionSignature(signer, user, points, collectibleType) {
// Create message hash (matches the one in CommunityPoints.sol)
const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256', 'uint256'], [user, points, collectibleType]));
// Create Ethereum signed message
const signature = await signer.signMessage(ethers_1.ethers.getBytes(messageHash));
return signature;
}
/**
* Verify a signature for spending tokens
* @param signature Signature to verify
* @param organization Organization address
* @param recipient Recipient address
* @param amount Amount to spend
* @param reason Reason for spending
* @returns Address of the signer
*/
function verifySpendSignature(signature, organization, recipient, amount, reason) {
// Create message hash (matches the one in TokenDispenser.sol)
const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(['address', 'address', 'uint256', 'string'], [organization, recipient, amount, reason]));
// Recover signer
const signer = ethers_1.ethers.verifyMessage(ethers_1.ethers.getBytes(messageHash), signature);
return signer;
}
/**
* Verify a signature for point redemption
* @param signature Signature to verify
* @param user User address
* @param points Number of points
* @param collectibleType Type of collectible
* @returns Address of the signer
*/
function verifyPointRedemptionSignature(signature, user, points, collectibleType) {
// Create message hash (matches the one in CommunityPoints.sol)
const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256', 'uint256'], [user, points, collectibleType]));
// Recover signer
const signer = ethers_1.ethers.verifyMessage(ethers_1.ethers.getBytes(messageHash), signature);
return signer;
}