All files / src/admin whitelisted.ts

86.6% Statements 97/112
0% Branches 0/1
81.81% Functions 18/22
85.71% Lines 90/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426            5x                                       5x                             5x 5x   5x   5x                                                                               1006x               1009x 1009x       5x                                 1x                             30x   30x   30x   30x   30x                                                           30x                               15x 15x 15x 15x 15x 15x         3x               3x                       24x 24x 24x           24x 24x 24x 24x 24x 24x 24x               1x 3x 3x       3x                   3x           3x     3x     3x 3x         3x                 1x 3x 3x   3x       3x 3x 3x 3x 3x 3x     3x               3x                   1x 3x                     1x 3x                         1x 3x                       1x           3x                     1x 3x 3x 3x             1x 3x 3x 3x 3x 3x 3x             1x 3x 3x 3x 3x 3x 3x                 1x 3x 3x 3x 3x             3x       1x    
/**
 * The `NFTWhitelistedAdminContract` is an implementation of an admin contract that uses a whitelist to control access to certain actions within the NFT ecosystem.
 * This contract ensures that only whitelisted addresses can perform specific actions such as minting, updating, transferring, buying, or selling NFTs.
 * It also introduces functionality for pausing and resuming the contract, upgrading the contract's verification key, and transferring ownership.
 */
 
import {
  Bool,
  DeployArgs,
  method,
  Permissions,
  PublicKey,
  SmartContract,
  State,
  state,
  VerificationKey,
  UInt64,
  Provable,
  Field,
  AccountUpdate,
  Mina,
  Poseidon,
  Experimental,
  UInt32,
  Struct,
} from "o1js";
import {
  MintRequest,
  NFTState,
  NFTAdminBase,
  MintParamsOption,
  UpgradeAuthorityBase,
  VerificationKeyUpgradeData,
  UpgradableContract,
  UpgradeAuthorityContractConstructor,
  Storage,
  PauseEvent,
  PausableContract,
  OwnershipChangeEvent,
  OwnableContract,
} from "../contracts";
import { loadIndexedMerkleMap, createIpfsURL } from "zkcloudworker";
export { NFTWhitelistedAdminContract, Whitelist, PauseData };
 
const { IndexedMerkleMap } = Experimental;
type IndexedMerkleMap = Experimental.IndexedMerkleMap;
const WHITELIST_HEIGHT = 20;
 
/** Represents the whitelist using an Indexed Merkle Map. */
class Whitelist extends IndexedMerkleMap(WHITELIST_HEIGHT) {}
 
/**
 * Deployment properties for the `NFTWhitelistedAdminContract`.
 */
export interface NFTWhitelistedAdminDeployProps
  extends Exclude<DeployArgs, undefined> {
  /** The public key of the admin or owner of the contract. */
  admin: PublicKey;
  /** The public key of the Upgrade Authority Contract. */
  upgradeAuthority: PublicKey;
  /** The root hash of the Merkle tree representing the whitelist. */
  whitelistRoot: Field;
  /** Off-chain storage information, typically an IPFS hash pointing to the whitelist data. */
  storage: Storage;
  /** The URI of the zkApp. */
  uri: string;
  /** Flag indicating whether the contract can be paused. */
  canPause: Bool;
  /** Flag indicating whether the contract is currently paused. */
  isPaused: Bool;
}
 
/**
 * Represents pause-related data, containing flags for pause functionality.
 */
class PauseData extends Struct({
  /** Indicates whether the contract can be paused. */
  canPause: Bool,
  /** Indicates whether the contract is currently paused. */
  isPaused: Bool,
}) {
  /**
   * Packs the pause data into a `Field`.
   * @returns A `Field` representing the packed pause data.
   */
  pack(): Field {
    return Field.fromBits([this.canPause, this.isPaused]);
  }
  /**
   * Unpacks a `Field` into `PauseData`.
   * @param field The `Field` to unpack.
   * @returns An instance of `PauseData`.
   */
  static unpack(field: Field): PauseData {
    const [canPause, isPaused] = field.toBits(2);
    return new PauseData({ canPause, isPaused });
  }
}
 
const NFTWhitelistedAdminContractErrors = {
  contractIsPaused: "Contract is paused",
  notWhitelisted: "Address not whitelisted",
  senderNotWhitelisted: "Sender address not whitelisted",
  cannotMint: "Cannot mint",
  verificationKeyHashNotFound: "Verification key hash not found",
  cannotUpgradeVerificationKey: "Cannot upgrade verification key",
};
 
/**
 * Constructs the `NFTWhitelistedAdmin` class, an admin contract that uses a whitelist to control access.
 * @param params Object containing the upgrade contract constructor.
 * @returns The `NFTWhitelistedAdmin` class.
 */
function NFTWhitelistedAdminContract(params: {
  upgradeContract: UpgradeAuthorityContractConstructor;
}) {
  const { upgradeContract } = params;
 
  /**
   * The `NFTWhitelistedAdmin` class ensures that only whitelisted addresses can perform specific actions such as minting, updating, transferring, buying, or selling NFTs.
   * It also provides functionality for pausing and resuming the contract, upgrading the contract's verification key, and transferring ownership.
   */
  class NFTWhitelistedAdmin
    extends SmartContract
    implements
      NFTAdminBase,
      UpgradableContract,
      PausableContract,
      OwnableContract
  {
    /** The public key of the admin or owner of the contract. */
    @state(PublicKey) admin = State<PublicKey>();
    /** The public key of the Upgrade Authority Contract. */
    @state(PublicKey) upgradeAuthority = State<PublicKey>();
    /** The root hash of the Merkle tree representing the whitelist. */
    @state(Field) whitelistRoot = State<Field>();
    /** Off-chain storage information, typically an IPFS hash pointing to the whitelist data. */
    @state(Storage) storage = State<Storage>();
    /** Packed field containing pause-related flags. */
    @state(Field) pauseData = State<Field>();
 
    /**
     * Deploys the `NFTWhitelistedAdmin` contract with the provided initial settings.
     * @param props Deployment properties.
     */
    async deploy(props: NFTWhitelistedAdminDeployProps) {
      await super.deploy(props);
      this.admin.set(props.admin);
      this.upgradeAuthority.set(props.upgradeAuthority);
      this.whitelistRoot.set(props.whitelistRoot);
      this.storage.set(props.storage);
      this.pauseData.set(
        new PauseData({
          canPause: props.canPause,
          isPaused: props.isPaused,
        }).pack()
      );
      this.account.zkappUri.set(props.uri);
      this.account.permissions.set({
        ...Permissions.default(),
        // We want to allow the upgrade authority to set the verification key
        // even in the case when there is no protocol upgrade
        // to allow the upgrade in case of o1js breaking changes
        setVerificationKey:
          Permissions.VerificationKey.proofDuringCurrentVersion(),
        setPermissions: Permissions.impossible(),
      });
    }
 
    events = {
      /** Emitted when the contract's verification key is upgraded. */
      upgradeVerificationKey: Field,
      /** Emitted when the contract is paused. */
      pause: PauseEvent,
      /** Emitted when the contract is resumed. */
      resume: PauseEvent,
      /** Emitted when ownership of the contract changes. */
      ownershipChange: OwnershipChangeEvent,
    };
 
    /**
     * Ensures that the transaction is authorized by the contract owner.
     * @returns An `AccountUpdate` representing the admin's signed transaction.
     */
    async ensureOwnerSignature(): Promise<AccountUpdate> {
      const sender = this.sender.getUnconstrained();
      const admin = this.admin.getAndRequireEquals();
      admin.assertEquals(sender);
      const adminUpdate = AccountUpdate.createSigned(admin);
      adminUpdate.body.useFullCommitment = Bool(true); // prevent memo and fee change
      return adminUpdate;
    }
 
    /** Gets the upgrade contract constructor. */
    get getUpgradeContractConstructor() {
      return upgradeContract;
    }
 
    /**
     * Retrieves the `UpgradeAuthorityBase` contract instance.
     * @returns An instance of the upgrade authority contract.
     */
    async getUpgradeContract(): Promise<UpgradeAuthorityBase> {
      return new this.getUpgradeContractConstructor(
        this.upgradeAuthority.getAndRequireEquals()
      );
    }
 
    /**
     * Checks if a given address is whitelisted for a specific amount.
     * @param address The public key of the address to check.
     * @param amount The amount to check against the whitelist.
     * @returns A `Bool` indicating whether the address is whitelisted for the amount.
     */
    async isWhitelisted(address: PublicKey, amount: UInt64): Promise<Bool> {
      const whitelistRoot = this.whitelistRoot.getAndRequireEquals();
      const storage = this.storage.getAndRequireEquals();
      const map = await Provable.witnessAsync(Whitelist, async () => {
        return await loadIndexedMerkleMap({
          url: createIpfsURL({ hash: storage.toString() }),
          type: Whitelist,
        });
      });
      map.root.assertEquals(whitelistRoot);
      const key = Poseidon.hash(address.toFields());
      map.assertIncluded(key, NFTWhitelistedAdminContractErrors.notWhitelisted);
      const value = map.get(key);
      value.assertLessThanOrEqual(Field(UInt64.MAXINT().value));
      const maxAmount = UInt64.Unsafe.fromField(value);
      return Bool(amount.lessThanOrEqual(maxAmount));
    }
 
    /**
     * Upgrades the contract's verification key using the Upgrade Authority Contract.
     * @param vk The new verification key.
     */
    @method
    async upgradeVerificationKey(vk: VerificationKey) {
      await this.ensureOwnerSignature();
      const upgradeContract = await this.getUpgradeContract();
      // fetchAccount() should be called before calling this method
      // this code should be changed after verification key precondition
      // will be added to the Mina protocol
      const previousVerificationKeyHash = Provable.witness(Field, () => {
        const account = Mina.getAccount(this.address);
        const vkHash = account.zkapp?.verificationKey?.hash;
        Iif (!vkHash) {
          throw Error(
            NFTWhitelistedAdminContractErrors.verificationKeyHashNotFound
          );
        }
        return vkHash;
      });
      const data = new VerificationKeyUpgradeData({
        address: this.address,
        tokenId: this.tokenId,
        previousVerificationKeyHash,
        newVerificationKeyHash: vk.hash,
      });
      const upgradeAuthorityAnswer = await upgradeContract.verifyUpgradeData(
        data
      );
      upgradeAuthorityAnswer.isVerified.assertTrue(
        NFTWhitelistedAdminContractErrors.cannotUpgradeVerificationKey
      );
      this.account.verificationKey.set(vk);
      this.upgradeAuthority.set(
        upgradeAuthorityAnswer.nextUpgradeAuthority.orElse(
          this.upgradeAuthority.getAndRequireEquals()
        )
      );
      this.emitEvent("upgradeVerificationKey", vk.hash);
    }
 
    /**
     * Determines if the minting request can proceed by checking if the owner and sender are whitelisted.
     * @param mintRequest The minting request parameters.
     * @returns A `MintParamsOption` indicating if minting is allowed.
     */
    @method.returns(MintParamsOption)
    async canMint(mintRequest: MintRequest): Promise<MintParamsOption> {
      const pauseData = PauseData.unpack(this.pauseData.getAndRequireEquals());
      pauseData.isPaused.assertFalse("Contract is paused");
 
      const isOwnerWhitelisted = await this.isWhitelisted(
        mintRequest.owner,
        UInt64.zero
      );
      isOwnerWhitelisted.assertTrue("Owner address not whitelisted");
      const sender = this.sender.getUnconstrained();
      const senderUpdate = AccountUpdate.createSigned(sender);
      senderUpdate.body.useFullCommitment = Bool(true); // prevent memo and fee change
      const isSenderWhitelisted = await this.isWhitelisted(sender, UInt64.zero);
      isSenderWhitelisted.assertTrue(
        NFTWhitelistedAdminContractErrors.senderNotWhitelisted
      );
      const mintParams = await Provable.witnessAsync(
        MintParamsOption,
        async () => {
          // only creator can mint
          // can be changed in the future to support CMS
          return MintParamsOption.none();
        }
      );
      return mintParams;
    }
 
    /**
     * Checks whether the NFT's state can be updated, ensuring the new owner is whitelisted.
     * @param input The current state of the NFT.
     * @param output The desired new state of the NFT.
     * @returns A `Bool` indicating whether the update is permitted.
     */
    @method.returns(Bool)
    async canUpdate(input: NFTState, output: NFTState) {
      return await this.isWhitelisted(output.owner, UInt64.zero);
    }
 
    /**
     * Verifies if the transfer between `from` and `to` addresses is allowed based on whitelist status.
     * @param address The address of the NFT.
     * @param from The sender's public key.
     * @param to The receiver's public key.
     * @returns A `Bool` indicating whether the transfer is permitted.
     */
    @method.returns(Bool)
    async canTransfer(address: PublicKey, from: PublicKey, to: PublicKey) {
      return (await this.isWhitelisted(to, UInt64.zero)).and(
        await this.isWhitelisted(from, UInt64.zero)
      );
    }
 
    /**
     * Determines if the seller is permitted to list the NFT for sale at the specified price.
     * @param address The address of the NFT.
     * @param seller The seller's public key.
     * @param price The price at which the NFT is being sold.
     * @returns A `Bool` indicating whether the sale is permissible.
     */
    @method.returns(Bool)
    async canSell(address: PublicKey, seller: PublicKey, price: UInt64) {
      return await this.isWhitelisted(seller, price);
    }
 
    /**
     * Determines if the buyer and seller are allowed to perform the transaction at the specified price.
     * @param address The address of the NFT.
     * @param seller The seller's public key.
     * @param buyer The buyer's public key.
     * @param price The price at which the NFT is being bought.
     * @returns A `Bool` indicating whether the purchase is permitted.
     */
    @method.returns(Bool)
    async canBuy(
      address: PublicKey,
      seller: PublicKey,
      buyer: PublicKey,
      price: UInt64
    ) {
      return (await this.isWhitelisted(buyer, price)).and(
        await this.isWhitelisted(seller, price)
      );
    }
 
    /**
     * Updates the whitelist's Merkle root and the associated off-chain storage reference.
     * @param whitelistRoot The new whitelist root.
     * @param storage The storage reference for the whitelist data.
     */
    @method
    async updateMerkleMapRoot(whitelistRoot: Field, storage: Storage) {
      await this.ensureOwnerSignature();
      this.whitelistRoot.set(whitelistRoot);
      this.storage.set(storage);
    }
 
    /**
     * Pauses the contract, preventing certain administrative actions from being performed.
     */
    @method
    async pause(): Promise<void> {
      await this.ensureOwnerSignature();
      const pauseData = PauseData.unpack(this.pauseData.getAndRequireEquals());
      pauseData.canPause.assertTrue();
      pauseData.isPaused = Bool(true);
      this.pauseData.set(pauseData.pack());
      this.emitEvent("pause", new PauseEvent({ isPaused: Bool(true) }));
    }
 
    /**
     * Resumes the contract, allowing administrative actions to be performed again.
     */
    @method
    async resume(): Promise<void> {
      await this.ensureOwnerSignature();
      const pauseData = PauseData.unpack(this.pauseData.getAndRequireEquals());
      pauseData.canPause.assertTrue();
      pauseData.isPaused = Bool(false);
      this.pauseData.set(pauseData.pack());
      this.emitEvent("resume", new PauseEvent({ isPaused: Bool(false) }));
    }
 
    /**
     * Transfers ownership of the contract to a new admin.
     * @param newOwner The public key of the new admin.
     * @returns The public key of the old owner.
     */
    @method.returns(PublicKey)
    async transferOwnership(newOwner: PublicKey): Promise<PublicKey> {
      await this.ensureOwnerSignature();
      const oldOwner = this.admin.getAndRequireEquals();
      this.admin.set(newOwner);
      this.emitEvent(
        "ownershipChange",
        new OwnershipChangeEvent({
          oldOwner,
          newOwner,
        })
      );
      return oldOwner;
    }
  }
 
  return NFTWhitelistedAdmin;
}