diff --git a/prototype/constants/index.ts b/prototype/constants/index.ts index 72f86e1c..a2a3cc94 100644 --- a/prototype/constants/index.ts +++ b/prototype/constants/index.ts @@ -4,7 +4,7 @@ export const ACCOUNT_TEST_PROFILE = { pubkey: "", strkKey: "", // nostrPk: "5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc", - nostrPublicKey:"5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc", + nostrPublicKey: "5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc", nostrPrivateKey: "59a772c0e643e4e2be5b8bac31b2ab5c5582b03a84444c81d6e2eec34a5e6c35", // contract:"0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", contract: @@ -15,32 +15,16 @@ export const ACCOUNT_TEST_PROFILE = { pubkey: "", strkKey: "", /** FIRST TEST */ - - // nostrPrivateKey:"3730616361326139616237323262643536613961316161646165376633396263", - // nostrPk:"70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47", - // contract: - // "0x16a0e5bb60649dfd66e8edf0b1eef7f5a196d99d41826c367f2eeea5794db2f", + /*** Dummy data */ + nostrPrivateKey: "70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47", + nostrPublicKey: "d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171", - - /** Last deploy */ - // nostrPrivateKey:"e076036c244a8794f4dc351d36af05c99e7bff876dcf7c2e8015cc99d6ee318c", - // nostrPk:"043cc0b553450d17f1adcdc3bdeb533892624a8a892d943202add694f52fd4c6", - // contract: - // "0x59e5edd06f305aad0c71da5d1c5ddecdf0cd96766fa69f4d81947a97555165f", - - - /*** Dummy data */ - nostrPrivateKey:"70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47", - - nostrPublicKey:"d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171", - // nostrPk:"d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171", - - contract: - "0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d", + contract: + "0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d", }, - escrow:{ - contract:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48", - SEPOLIA:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48" + escrow: { + contract: "0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48", + SEPOLIA: "0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48" } }; export const ERROR_MESSAGES = { @@ -64,7 +48,7 @@ export const TOKENS_ADDRESS = { // TEST: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4", STRK: "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d", - USDC:"0x02f37c3e00e75ee4135b32bb60c37e0599af264076376a618f138d2f9929ac74", + USDC: "0x02f37c3e00e75ee4135b32bb60c37e0599af264076376a618f138d2f9929ac74", TEST: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4", BIG_TOKEN: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4", diff --git a/prototype/test/escrow.test.ts b/prototype/test/escrow.test.ts index 3617297e..04b30de0 100644 --- a/prototype/test/escrow.test.ts +++ b/prototype/test/escrow.test.ts @@ -1,80 +1,73 @@ import { - connectToStarknet, - createStarknetWallet, provider, } from "../utils/starknet"; import { expect } from "chai"; import { generateKeypair, sendEvent } from "../utils/nostr"; - import { createSocialAccount, createSocialContract, prepareAndConnectContract, } from "../utils/social_account"; -import { createToken, getToken, transferToken } from "../utils/token"; import { Account, byteArray, cairo, uint256 } from "starknet"; -import { SocialPayRequest } from "types"; import { ACCOUNT_TEST_PROFILE, TOKENS_ADDRESS } from "../constants"; import { stringToUint8Array } from "../utils/format"; import dotenv from "dotenv"; -import { finalizeEvent, nip19, serializeEvent } from "nostr-tools"; -import { createEscrowAccount } from "../utils/escrow"; +import { cancel, claimDeposit, createEscrowAccount, deposit } from "../utils/escrow"; dotenv.config(); /** Testing tips flow: - * + * Deploy contract + * Deposit + * Claim + * Cancel */ +// Sepolia params testing +const currentId = 1 +const idToClaim = 1 +const idToCancel = 1 +let escrow_address = ACCOUNT_TEST_PROFILE.escrow.contract // change default address +let token_used_address = TOKENS_ADDRESS.DEVNET.ETH; + describe("Escrow End to end test", () => { - it("Deploy Escrow Deposit and Claim", async function () { + it("Deploy Escrow", async function () { this.timeout(0); // Disable timeout for this test - - // const resp = await provider.getSpecVersion(); - // console.log("rpc version =", resp); const privateKey0 = process.env.DEV_PK as string; const accountAddress0 = process.env.DEV_PUBLIC_KEY as string; - const account = new Account(provider, accountAddress0, privateKey0, "1"); - /*** Init account - * @description - * Get both account for Bob & Alice - * Send request of account bob to alice - ***/ - - /** Generate keypair for both account*/ - // Bob nostr account - // let { privateKey: pkBob, publicKey: bobPublicKey } = generateKeypair(); - - let pkBob = stringToUint8Array(ACCOUNT_TEST_PROFILE?.bob?.nostrPrivateKey); - const bobPublicKey = ACCOUNT_TEST_PROFILE?.bob?.nostrPublicKey; - console.log("privateKey Bob", new Buffer(pkBob).toString("hex")); - console.log("bobPublicKey", bobPublicKey); - - /** @TODO Alice account key */ - // Nostr account - // let { privateKey: privateKeyAlice, publicKey: alicePublicKey } = generateKeypair(); - - // let privateKeyAlice = stringToUint8Array( - // ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey - // ); - let privateKeyAlice = ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey as any; + let escrow; + if (process.env.IS_DEPLOY_CONTRACT == "true") { + let escrowContract = await createEscrowAccount(); - console.log("ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey", ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey); + console.log("escrow address", escrowContract?.contract_address) + escrow = await prepareAndConnectContract( + escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract + account + ); + } else { + escrow = await prepareAndConnectContract( + escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, + account + ); + } + }); + it("Deposit", async function () { + this.timeout(0); // Disable timeout for this test + const privateKey0 = process.env.DEV_PK as string; + const accountAddress0 = process.env.DEV_PUBLIC_KEY as string; + const account = new Account(provider, accountAddress0, privateKey0, "1"); const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey; - console.log("alicePublicKey", alicePublicKey); let escrow; - let token_used_address = TOKENS_ADDRESS.DEVNET.ETH; - if (process.env.IS_DEPLOY_CONTRACT == "true") { - let accountBob = await createEscrowAccount(); + let escrowContract = await createEscrowAccount(); escrow = await prepareAndConnectContract( - accountBob?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract + escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract account ); } else { escrow = await prepareAndConnectContract( - ACCOUNT_TEST_PROFILE?.escrow?.contract, + escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, account ); } @@ -87,101 +80,91 @@ describe("Escrow End to end test", () => { ); /** Deposit */ - - let firstId = 1 - let currentId = 1; // let nextId = currentId // await escrow.get_next_deposit_id(); // function need to be made? console.log("nextId", nextId) - // let depositCurrentId = await escrow.get_deposit(currentId) - // console.log("depositCurrentId",depositCurrentId) - - // const depositParams = { - // amount: cairo.uint256(amount), // amount int. Float need to be convert with bnToUint - // // amount: uint256.bnToUint256(BigInt(amount)), // amount - // token_address: strkToken?.address, // token address - // // nostr_recipient: uint256.bnToUint256(BigInt("0x" + alicePublicKey)), - // nostr_recipient: cairo.uint256(BigInt("0x"+alicePublicKey)), - // timelock: 100, - // }; + let depositCurrentId = await escrow.get_deposit(currentId) + console.log("depositCurrentId", depositCurrentId) + console.log("try approve escrow erc20") + let txApprove = await strkToken.approve( + escrow?.address, + cairo.uint256(amount), // change for decimals float => uint256.bnToUint256("0x"+alicePublicKey) + ) + + await account?.waitForTransaction(txApprove?.transaction_hash) + // Need an approve before + console.log("deposit amount") + + await deposit({ + escrow, + amount, + account, + tokenAddress: strkToken?.address, + timelock: 100, + alicePublicKey: alicePublicKey + }) // expect(cairo.uint256(depositCurrentId?.amount)).to.deep.eq(depositParams?.amount) - // // console.log("try approve escrow erc20") - - // let txApprove = await strkToken.approve(escrow?.address, depositParams?.amount) - - // await account?.waitForTransaction(txApprove?.transaction_hash) - // // Need an approve before - // console.log("deposit amount") - - // let txDeposit = await escrow.deposit(depositParams?.amount, - // depositParams?.token_address, - // depositParams?.nostr_recipient, - // depositParams?.timelock, - - // ); - // console.log("txDeposit",txDeposit) - // await account?.waitForTransaction(txDeposit?.transaction_hash); + }); - // currentId++; + it("Claim", async function () { + this.timeout(0); // Disable timeout for this test + const privateKey0 = process.env.DEV_PK as string; + const accountAddress0 = process.env.DEV_PUBLIC_KEY as string; + const account = new Account(provider, accountAddress0, privateKey0, "1"); + let privateKeyAlice = ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey as any; + const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey; + let escrow; + if (process.env.IS_DEPLOY_CONTRACT == "true") { + let escrowContract = await createEscrowAccount(); + escrow = await prepareAndConnectContract( + escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract + account + ); + } else { + escrow = await prepareAndConnectContract( + escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, + account + ); + } /** Claim */ + let timestamp = 1716285235; // let timestamp = new Date().getTime(); - let timestamp=1716285235; + let content = `claim ${cairo.felt(currentId)}`; - // let content =String(currentId) - - console.log("content event", content); - console.log("create event for claim") - const event = finalizeEvent( - { - kind: 1, - tags: [], - content: content, - created_at: timestamp, - }, - privateKeyAlice - ); + let txClaim = await claimDeposit({ + escrow, + account, + depositId: idToClaim, + content, + timestamp, + alicePublicKey, + privateKey: privateKeyAlice + }) + console.log("tx claim", txClaim) + }); + + it("Cancel", async function () { + this.timeout(0); // Disable timeout for this test + const privateKey0 = process.env.DEV_PK as string; + const accountAddress0 = process.env.DEV_PUBLIC_KEY as string; + const account = new Account(provider, accountAddress0, privateKey0, "1"); + const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey; - console.log( - "event", - event + let escrow = await prepareAndConnectContract( + escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, + account ); - const signature = event.sig; - const signatureR = "0x" + signature.slice(0, signature.length / 2); - const signatureS = "0x" + signature.slice(signature.length / 2); - console.log("signature", signature); - console.log("signatureR", signatureR); - console.log("signatureS", signatureS); - - if (signature) { - // let public_key=uint256.bnToUint256(BigInt("0x" + alicePublicKey)) - let public_key = cairo.uint256(BigInt("0x" + alicePublicKey)) - // expect(depositCurrentId?.recipient).to.eq(BigInt("0x" + alicePublicKey)) - const claimParams = { - public_key: public_key, - created_at: timestamp, - kind: 1, - tags: byteArray.byteArrayFromString("[]"), // tags - // content: content, // currentId in felt - content:cairo.felt(currentId), - signature: { - r: cairo.uint256(signatureR), - s: cairo.uint256(signatureS), - }, - }; - - console.log("claimParams", claimParams); - const tx = await account.execute({ - contractAddress: escrow?.address, - calldata: claimParams, - entrypoint: "claim", - }); - - console.log("tx handle claim", tx); - } - // console.log("Alice balance STRK", balanceAlice); - // expect(balanceAlice).to.eq(cairo.uint256(amount)); + /** Deposit */ + let depositToCancel = await escrow.get_deposit(idToCancel) + console.log("deposit to cancel",depositToCancel) + await cancel({ + escrow, + account, + depositId: idToCancel, + }) + }); }); diff --git a/prototype/utils/escrow.ts b/prototype/utils/escrow.ts index a881f409..029d894d 100644 --- a/prototype/utils/escrow.ts +++ b/prototype/utils/escrow.ts @@ -6,6 +6,7 @@ import { Contract, cairo, uint256, + byteArray, } from "starknet"; import fs from "fs"; import dotenv from "dotenv"; @@ -13,6 +14,7 @@ import { nostrPubkeyToUint256 } from "./format"; import { provider } from "./starknet"; import { transferToken } from "./token"; import path from "path"; +import { finalizeEvent } from "nostr-tools"; dotenv.config(); const PATH_SOCIAL_ACCOUNT = path.resolve( @@ -92,7 +94,7 @@ export const createEscrowAccount = async () => { console.log("Tx deploy", tx); await provider.waitForTransaction(transaction_hash); console.log( - "✅ New contract Social created.\n address =", + "✅ New contract Escrow created.\n address =", contract_address ); @@ -107,3 +109,128 @@ export const createEscrowAccount = async () => { } }; + +export const deposit = async (props: { + escrow: Contract, + account: Account, + amount: number, + tokenAddress: string, + timelock: number, + alicePublicKey: string, + +}) => { + try { + const { escrow, account, amount, tokenAddress, timelock, alicePublicKey } = props + const depositParams = { + amount: cairo.uint256(amount), // amount int. Float need to be convert with bnToUint + token_address: tokenAddress, // token address + nostr_recipient: cairo.uint256(BigInt("0x" + alicePublicKey)), + timelock: timelock, + }; + console.log("depositParams", depositParams); + const tx = await account.execute({ + contractAddress: escrow?.address, + calldata: depositParams, + entrypoint: "deposit", + }); + + await account.waitForTransaction(tx.transaction_hash) + + return tx; + + } catch (e) { + console.log("Error deposit", e) + + } + + + +} + +export const claimDeposit = async (props: { + escrow: Contract, + account: Account, + depositId: number, + content: string, + timestamp: number, + alicePublicKey: string, + privateKey: any, +} +) => { + try { + const { escrow, account, depositId, content, timestamp, alicePublicKey, privateKey } = props + const event = finalizeEvent( + { + kind: 1, + tags: [], + content: content, + created_at: timestamp, + }, + privateKey + ); + + console.log( + "event", + event + ); + const signature = event.sig; + const signatureR = "0x" + signature.slice(0, signature.length / 2); + const signatureS = "0x" + signature.slice(signature.length / 2); + console.log("signature", signature); + console.log("signatureR", signatureR); + console.log("signatureS", signatureS); + let public_key = cairo.uint256(BigInt("0x" + alicePublicKey)) + // expect(depositCurrentId?.recipient).to.eq(BigInt("0x" + alicePublicKey)) + const claimParams = { + public_key: public_key, + created_at: timestamp, + kind: 1, + tags: byteArray.byteArrayFromString("[]"), // tags + // content: content, // currentId in felt + content: cairo.felt(depositId), + signature: { + r: cairo.uint256(signatureR), + s: cairo.uint256(signatureS), + }, + }; + console.log("claimParams", claimParams); + const tx = await account.execute({ + contractAddress: escrow?.address, + calldata: claimParams, + entrypoint: "claim", + }); + + await account.waitForTransaction(tx.transaction_hash) + + return tx; + + } catch (e) { + console.log("Error claim deposit", e) + + } + +} + +export const cancel = async (props: { + escrow: Contract, + account: Account, + depositId: number, +}) => { + try { + const { escrow, account, depositId } = props + const cancelParams = { + deposit_id: cairo.felt(depositId), + }; + console.log("cancelParams", cancelParams); + const tx = await account.execute({ + contractAddress: escrow?.address, + calldata: cancelParams, + entrypoint: "cancel", + }); + await account.waitForTransaction(tx.transaction_hash) + return tx; + } catch (e) { + console.log("Error cancel", e) + } +} + diff --git a/prototype/utils/starknet.ts b/prototype/utils/starknet.ts index 4b825575..23ca425c 100644 --- a/prototype/utils/starknet.ts +++ b/prototype/utils/starknet.ts @@ -3,9 +3,6 @@ dotenv.config() import { Account, RpcProvider, ec, stark } from "starknet"; const STARKNET_URL = process.env.RPC_ENDPOINT || "http://127.0.0.1:5050"; -console.log(" process.env.RPC_ENDPOINT", process.env.RPC_ENDPOINT) -console.log("STARKNET_URL",STARKNET_URL) -/** @TODO Uncomment to use with your own RPC_ENDPOINT, Sepolia by default */ export const provider = new RpcProvider({nodeUrl:STARKNET_URL}); export const createStarknetWallet = () => {