All files / src escrow.ts

92% Statements 92/100
50% Branches 8/16
100% Functions 9/9
91.91% Lines 91/99

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 2483x 3x                   3x 3x 3x 3x                                             2x             2x 2x 2x 2x 2x     2x 2x 2x   2x 2x     4x 4x 4x 4x     2x 2x 2x 2x 2x 2x                     1x       1x 1x 1x 1x       1x 1x 1x   1x     2x 2x 2x 2x     1x 1x 1x 1x 1x 1x 1x                   1x       1x 1x 1x 1x         1x 1x 1x   1x     2x     1x 1x 1x 1x 1x 1x 1x                                   1x   1x     1x           1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x                                             1x     2x                           2x 2x 2x     1x 1x 1x 1x 1x 1x 1x        
export { MinaNFTEscrow, EscrowTransferData };
import {
  Mina,
  PrivateKey,
  PublicKey,
  AccountUpdate,
  fetchAccount,
  Signature,
  Field,
  Account,
} from "o1js";
import { MinaNFT } from "./minanft";
import { Escrow, EscrowDeposit } from "./plugins/escrow";
import { EscrowTransfer } from "./contract/escrow";
import { sleep } from "./mina";
 
interface EscrowTransferData {
  data: EscrowTransfer;
  escrow: PrivateKey;
  sellerDeposited: EscrowDeposit;
  buyerDeposited: EscrowDeposit;
  nft: PublicKey;
  nameService: PublicKey;
  tokenId: Field;
  seller: PublicKey;
  buyer: PublicKey;
  isKYCpassed: boolean;
}
 
class MinaNFTEscrow {
  address?: PublicKey;
 
  /**
   * Create MinaNFTEscrow
   * @param address Public key of the deployed NFT zkApp
   */
  constructor(address?: PublicKey) {
    this.address = address;
  }
 
  public async deploy(
    deployer: PrivateKey,
    privateKey: PrivateKey | undefined = undefined
  ): Promise<Mina.PendingTransaction | undefined> {
    const sender = deployer.toPublicKey();
    const zkAppPrivateKey = privateKey ?? PrivateKey.random();
    const zkAppPublicKey = zkAppPrivateKey.toPublicKey();
    await MinaNFT.compileEscrow();
    console.log(
      `deploying the Escrow contract to an address ${zkAppPublicKey.toBase58()} using the deployer with public key ${sender.toBase58()}...`
    );
    await fetchAccount({ publicKey: sender });
    await fetchAccount({ publicKey: zkAppPublicKey });
    const hasAccount = Mina.hasAccount(zkAppPublicKey);
 
    const zkApp = new Escrow(zkAppPublicKey);
    const transaction = await Mina.transaction(
      { sender, fee: await MinaNFT.fee(), memo: "minanft.io" },
      async () => {
        if (!hasAccount) AccountUpdate.fundNewAccount(sender);
        await zkApp.deploy({});
        zkApp.account.tokenSymbol.set("ESCROW");
        zkApp.account.zkappUri.set("https://minanft.io/@escrow");
      }
    );
    transaction.sign([deployer, zkAppPrivateKey]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "escrow deploy", false);
    if (tx.status === "pending") {
      this.address = zkAppPublicKey;
      return tx;
    } else Ereturn undefined;
  }
 
  public async deposit(
    data: EscrowTransfer,
    buyer: PrivateKey,
    escrow: PublicKey
  ): Promise<
    { tx: Mina.PendingTransaction; deposited: EscrowDeposit } | undefined
  > {
    Iif (this.address === undefined) {
      throw new Error("Escrow not deployed");
    }
 
    await MinaNFT.compileEscrow();
    const sender = buyer.toPublicKey();
    const zkApp = new Escrow(this.address);
    const signature: Signature = Signature.create(
      buyer,
      EscrowTransfer.toFields(data)
    );
    const deposited: EscrowDeposit = { data, signature } as EscrowDeposit;
    await fetchAccount({ publicKey: sender });
    await fetchAccount({ publicKey: this.address });
 
    const transaction = await Mina.transaction(
      { sender, fee: await MinaNFT.fee(), memo: "minanft.io" },
      async () => {
        await zkApp.deposit(deposited, buyer.toPublicKey());
        const senderUpdate = AccountUpdate.create(buyer.toPublicKey());
        senderUpdate.requireSignature();
        senderUpdate.send({ to: escrow, amount: data.price });
      }
    );
    await sleep(100); // alow GC to run
    await transaction.prove();
    transaction.sign([buyer]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "deposit", false);
    if (tx.status === "pending") {
      return { tx, deposited };
    } else Ereturn undefined;
  }
 
  public async approveSale(
    data: EscrowTransfer,
    seller: PrivateKey
  ): Promise<
    { tx: Mina.PendingTransaction; deposited: EscrowDeposit } | undefined
  > {
    Iif (this.address === undefined) {
      throw new Error("Escrow not deployed");
    }
 
    await MinaNFT.compileEscrow();
    const sender = seller.toPublicKey();
    const zkApp = new Escrow(this.address);
    const signature: Signature = Signature.create(
      seller,
      EscrowTransfer.toFields(data)
    );
    //console.log("signature length", signature.toFields().length);
    const deposited: EscrowDeposit = { data, signature } as EscrowDeposit;
    await fetchAccount({ publicKey: sender });
    await fetchAccount({ publicKey: this.address });
 
    const transaction = await Mina.transaction(
      { sender, fee: await MinaNFT.fee(), memo: "minanft.io" },
      async () => {
        await zkApp.approveSale(deposited, seller.toPublicKey());
      }
    );
    await sleep(100); // alow GC to run
    await transaction.prove();
    transaction.sign([seller]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "approve sale", false);
    if (tx.status === "pending") {
      return { tx, deposited };
    } else Ereturn undefined;
  }
 
  public async transfer(
    transferData: EscrowTransferData
  ): Promise<Mina.PendingTransaction | undefined> {
    const {
      data,
      escrow,
      sellerDeposited,
      buyerDeposited,
      nft,
      nameService,
      tokenId,
      seller,
      buyer,
      isKYCpassed,
    } = transferData;
 
    Iif (this.address === undefined) {
      throw new Error("Escrow not deployed");
    }
    Iif (isKYCpassed === false) {
      throw new Error(
        "KYC not passed. It is obligation of the escrow agent to check the KYC status of the buyer and the seller."
      );
    }
 
    await MinaNFT.compileEscrow();
    const sender = escrow.toPublicKey();
    const zkApp = new Escrow(this.address);
    const signature: Signature = Signature.create(
      escrow,
      EscrowTransfer.toFields(data)
    );
    await fetchAccount({ publicKey: sender });
    await fetchAccount({ publicKey: this.address });
    await fetchAccount({ publicKey: nameService });
    await fetchAccount({ publicKey: nft, tokenId });
    const hasAccount = Mina.hasAccount(nft, tokenId);
    const account = Mina.getAccount(nft, tokenId);
    const balance = Mina.getBalance(nft, tokenId);
    console.log(
      `transfer checks result:`,
      hasAccount,
      tokenId.toJSON(),
      account.balance.toString(),
      balance.toString()
    );
    /*
      @method transfer(
        nft: PublicKey,
        data: EscrowTransfer,
        signature1: Signature,
        signature2: Signature,
        signature3: Signature,
        escrow1: PublicKey,
        escrow2: PublicKey,
        escrow3: PublicKey,
        amount: UInt64,
        seller: PublicKey,
        buyer: PublicKey
      ) {
    */
 
    const transaction = await Mina.transaction(
      { sender, fee: await MinaNFT.fee(), memo: "minanft.io" },
      async () => {
        await zkApp.transfer(
          nft,
          nameService,
          data,
          sellerDeposited.signature,
          buyerDeposited.signature,
          signature,
          seller,
          buyer,
          escrow.toPublicKey(),
          data.price,
          seller,
          buyer
        );
        const senderUpdate = AccountUpdate.create(escrow.toPublicKey());
        senderUpdate.requireSignature();
        senderUpdate.send({ to: seller, amount: data.price });
      }
    );
    await sleep(100); // alow GC to run
    await transaction.prove();
    transaction.sign([escrow]);
    const tx = await transaction.send();
    await MinaNFT.transactionInfo(tx, "transfer", false);
    if (tx.status === "pending") {
      return tx;
    } else Ereturn undefined;
  }
}