All files / src minanftnames.ts

85.33% Statements 64/75
47.61% Branches 10/21
100% Functions 6/6
88.73% Lines 63/71

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 15327x 27x                   27x 27x 27x                           14x 14x               13x   13x   13x 13x 13x 13x 13x       13x 13x 13x   13x 13x   13x               26x 26x 26x 26x 26x     13x 13x 13x 13x 13x 13x 13x                 1x   1x 1x 1x 1x   1x 1x   1x 1x       1x 1x 1x   1x 1x 1x   1x               2x 2x     1x 1x 1x 1x 1x 1x 1x               11x 11x   11x   11x       11x           11x      
export { MinaNFTNameService };
import {
  Mina,
  PrivateKey,
  PublicKey,
  AccountUpdate,
  Field,
  Signature,
  Account,
  VerificationKey,
} from "o1js";
import { MinaNFT } from "./minanft";
import { MinaNFTNameServiceContract, NFTMintData } from "./contract/names";
import { fetchMinaAccount } from "./fetch";
 
class MinaNFTNameService {
  address?: PublicKey;
  oraclePrivateKey?: PrivateKey;
  tokenId?: Field;
 
  /**
   * Create MinaNFTNameService object
   * @param value Object with address and oraclePrivateKey fields
   * @param value.address Public key of the deployed Names Service
   * @param value.oraclePrivateKey Private key of the oracle
   */
  constructor(value: { address?: PublicKey; oraclePrivateKey?: PrivateKey }) {
    this.address = value.address;
    this.oraclePrivateKey = value.oraclePrivateKey;
  }
 
  public async deploy(
    deployer: PrivateKey,
    privateKey: PrivateKey | undefined = undefined,
    nonce?: number
  ): Promise<Mina.PendingTransaction | undefined> {
    const sender = deployer.toPublicKey();
 
    Iif (this.oraclePrivateKey === undefined)
      throw new Error("Oracle private key is not set");
    const oracle = this.oraclePrivateKey.toPublicKey();
    const zkAppPrivateKey = privateKey ?? PrivateKey.random();
    const zkAppPublicKey = zkAppPrivateKey.toPublicKey();
    await MinaNFT.compile();
    console.log(
      `deploying the MinaNFTNameServiceContract to an address ${zkAppPublicKey.toBase58()} using the deployer with public key ${sender.toBase58()}...`
    );
 
    const zkApp = new MinaNFTNameServiceContract(zkAppPublicKey);
    await fetchMinaAccount({ publicKey: sender });
    await fetchMinaAccount({ publicKey: zkAppPublicKey });
    const deployNonce =
      nonce ?? Number(Mina.getAccount(sender).nonce.toBigint());
    const hasAccount = Mina.hasAccount(zkAppPublicKey);
 
    const transaction = await Mina.transaction(
      {
        sender,
        fee: await MinaNFT.fee(),
        memo: "minanft.io",
        nonce: deployNonce,
      },
      async () => {
        if (!hasAccount) AccountUpdate.fundNewAccount(sender);
        await zkApp.deploy({});
        zkApp.oracle.set(oracle);
        zkApp.account.zkappUri.set("https://minanft.io");
        zkApp.account.tokenSymbol.set("NFT");
      }
    );
    transaction.sign([deployer, zkAppPrivateKey]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "name service deploy", false);
    if (tx.status === "pending") {
      this.address = zkAppPublicKey;
      this.tokenId = zkApp.deriveTokenId();
      return tx;
    } else Ereturn undefined;
  }
 
  public async upgrade(
    deployer: PrivateKey,
    privateKey: PrivateKey,
    nonce?: number
  ): Promise<Mina.PendingTransaction | undefined> {
    const sender = deployer.toPublicKey();
 
    Iif (this.address === undefined) throw new Error("Address is not set");
    const zkAppPrivateKey = privateKey;
    const zkAppPublicKey = zkAppPrivateKey.toPublicKey();
    Iif (this.address.toBase58() !== zkAppPublicKey.toBase58())
      throw new Error("Address mismatch");
    await MinaNFT.compile();
    Iif (MinaNFT.namesVerificationKey === undefined)
      throw new Error("Compilation error: Verification key is not set");
    const verificationKey: VerificationKey = MinaNFT.namesVerificationKey;
    console.log(
      `upgrading the MinaNFTNameServiceContract on address ${zkAppPublicKey.toBase58()} using the deployer with public key ${sender.toBase58()}...`
    );
 
    const zkApp = new MinaNFTNameServiceContract(zkAppPublicKey);
    await fetchMinaAccount({ publicKey: sender });
    await fetchMinaAccount({ publicKey: zkAppPublicKey });
    const deployNonce =
      nonce ?? Number(Mina.getAccount(sender).nonce.toBigint());
    const hasAccount = Mina.hasAccount(zkAppPublicKey);
    Iif (!hasAccount) throw new Error("Account does not exist");
 
    const transaction = await Mina.transaction(
      {
        sender,
        fee: await MinaNFT.fee(),
        memo: "minanft.io",
        nonce: deployNonce,
      },
      async () => {
        const update = AccountUpdate.createSigned(zkAppPublicKey);
        update.account.verificationKey.set(verificationKey);
      }
    );
    transaction.sign([deployer, zkAppPrivateKey]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "name service upgrade", false);
    if (tx.status === "pending") {
      this.address = zkAppPublicKey;
      this.tokenId = zkApp.deriveTokenId();
      return tx;
    } else Ereturn undefined;
  }
 
  public async issueNameSignature(
    nft: NFTMintData,
    verificationKeyHash: Field
  ): Promise<Signature> {
    Iif (nft.address === undefined) throw new Error("NFT address is not set");
    Iif (nft.name.toJSON() !== nft.initialState[0].toJSON())
      throw new Error("Name mismatch");
    Iif (this.address === undefined)
      throw new Error("Names service address is not set");
    Iif (this.oraclePrivateKey === undefined)
      throw new Error("Oracle is not set");
 
    // TODO: change to api call
    const signature: Signature = Signature.create(this.oraclePrivateKey, [
      ...nft.address.toFields(),
      nft.name,
      verificationKeyHash,
      ...this.address.toFields(),
    ]);
    return signature;
  }
}