From 8750b6251292bd8f4ac0fa686fa8afb32955dd2c Mon Sep 17 00:00:00 2001 From: MSghais Date: Fri, 14 Jun 2024 12:59:14 +0200 Subject: [PATCH] escrow script --- prototype/.env.exemple | 2 + prototype/constants/index.ts | 4 + prototype/test/escrow.test.ts | 216 ++++++++++++++ prototype/test/event.test.ts | 64 ++-- prototype/test/tip_end_to_end.test.ts | 410 +++++++++++++------------- prototype/utils/escrow.ts | 109 +++++++ 6 files changed, 568 insertions(+), 237 deletions(-) create mode 100644 prototype/test/escrow.test.ts create mode 100644 prototype/utils/escrow.ts diff --git a/prototype/.env.exemple b/prototype/.env.exemple index 8a23a6d3..32494407 100644 --- a/prototype/.env.exemple +++ b/prototype/.env.exemple @@ -23,3 +23,5 @@ POSTGRES_HOST=db POSTGRES_PORT=5432 DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} NOSTR_RELAYER_WEBSOCKET=ws://localhost:3000 + +ESCROW_CLASS_HASH=0x3f70abaaeef59cc900b597d45a2c97c08abb5260c03327cce9e45d4c7a8ad0 # Sepolia diff --git a/prototype/constants/index.ts b/prototype/constants/index.ts index cf789d9f..a24cac8b 100644 --- a/prototype/constants/index.ts +++ b/prototype/constants/index.ts @@ -38,6 +38,10 @@ export const ACCOUNT_TEST_PROFILE = { contract: "0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d", }, + escrow:{ + contract:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48", + SEPOLIA:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48" + } }; export const ERROR_MESSAGES = { EVENT_NOTE_INVALID: { diff --git a/prototype/test/escrow.test.ts b/prototype/test/escrow.test.ts new file mode 100644 index 00000000..0f4d098a --- /dev/null +++ b/prototype/test/escrow.test.ts @@ -0,0 +1,216 @@ +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"; +dotenv.config(); +/** Testing tips flow: + * + */ +describe("Escrow End to end test", () => { + it("Deploy Escrow Deposit and Claim", 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?.nostrPk; + console.log("privateKey Bob", new Buffer(pkBob).toString("hex")); + console.log("bobPublicKey", bobPublicKey); + + /** @TODO Alice account key */ + // Nostr account + // let { privateKey: pkAlice, publicKey: alicePublicKey } = generateKeypair(); + + let pkAlice = stringToUint8Array( + ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey + ); + const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPk; + console.log("pkAlice", new Buffer(pkAlice).toString("hex")); + console.log("alicePublicKey", alicePublicKey); + // Bob contract/A.A + // @TODO Finish SNIP-6 to use it + // Use your ENV or Generate public and private key pair when A.A SNIP-06. + // const AAprivateKey = process.env.AA_PRIVATE_KEY ?? stark.randomAddress(); + // console.log("New account:\nprivateKey=", AAprivateKey); + // const AAstarkKeyPub = + // process.env.AA_PUBKEY ?? ec.starkCurve.getStarkKey(AAprivateKey); + // console.log("publicKey=", AAstarkKeyPub); + // await transferToken( + // account, + // accountAddress0, + // TOKENS_ADDRESS?.DEVNET?.ETH, + // // token?.address, // TOKENS_ADDRESS.SEPOLIA.TEST, + // 10 + // ); + + /** @description Uncomment to create your social account or comment and change your old contract in the constant ACCOUNT_TEST_PROFILE or direcly below***/ + // console.log("create social account"); + + // let accountBob = await createSocialContract(bobPublicKey); + /** uncomment to use social account deploy when SNIP-6 finish */ + + // let accountBob = await createSocialAccount( + // bobPublicKey, + // AAprivateKey, + // AAstarkKeyPub + // ); + // let accountBob = { + // contract_address: undefined, + // }; + // console.log("accountBob?.contract_address ", accountBob?.contract_address); + let escrow; + + if (process.env.IS_DEPLOY_CONTRACT == "true") { + let accountBob = await createEscrowAccount(); + + escrow = await prepareAndConnectContract( + accountBob?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract + account + ); + } else { + escrow = await prepareAndConnectContract( + ACCOUNT_TEST_PROFILE?.escrow?.contract, + account + ); + } + + /** Send a note */ + let amount: number = 1; + let strkToken = await prepareAndConnectContract( + TOKENS_ADDRESS?.SEPOLIA?.BIG_TOKEN, + account + ); + + /** Deposit */ + + let currentId = 1; + let nextId = 2 // await escrow.get_next_deposit_id(); // function get 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: alicePublicKey, + nostr_recipient: uint256.bnToUint256(BigInt("0x" + alicePublicKey)), + timelock: 100, + }; + + expect(cairo.uint256(depositCurrentId?.amount)).to.deep.eq(depositParams?.amount) + // let firstDeposit = 1; + + + // 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++; + + /** Claim */ + let timestamp = new Date().getTime(); + // let content = cairo.felt(currentId); + let content = 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, + }, + pkAlice + ); + + console.log( + "event", + event + ); + const signature = event.sig; + const signatureR = signature.slice(0, signature.length / 2); + const signatureS = 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)) + 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 + signature: { + r: uint256.bnToUint256(BigInt("0x"+signatureR)), + s: uint256.bnToUint256(BigInt("0x"+signatureS)), + // r: cairo.uint256(BigInt("0x" + signatureR)), + // s: cairo.uint256(BigInt("0x" + 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)); + }); +}); diff --git a/prototype/test/event.test.ts b/prototype/test/event.test.ts index 24d63bd4..5368699e 100644 --- a/prototype/test/event.test.ts +++ b/prototype/test/event.test.ts @@ -57,44 +57,44 @@ describe("Event", () => { /*** @description uncomment this test to deploy your own contract, nostr account etc ***/ it("Check event", async function () { - this.timeout(0); // Disable timeout for this test + // 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 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"); + // const account = new Account(provider, accountAddress0, privateKey0, "1"); - let pkBob = stringToUint8Array(ACCOUNT_TEST_PROFILE?.bob?.nostrPrivateKey); - const bobPublicKey = ACCOUNT_TEST_PROFILE?.bob?.nostrPk; - console.log("pkBob", new Buffer(pkBob).toString("hex")); - console.log("bobPublicKey", bobPublicKey); + // let pkBob = stringToUint8Array(ACCOUNT_TEST_PROFILE?.bob?.nostrPrivateKey); + // const bobPublicKey = ACCOUNT_TEST_PROFILE?.bob?.nostrPk; + // console.log("pkBob", new Buffer(pkBob).toString("hex")); + // console.log("bobPublicKey", bobPublicKey); - // Bob contract/A.A + // // Bob contract/A.A - /** Send a note */ - let amount: number = 1; - // let contentRequest = "@joyboy send 10 STRK to @alice.xyz"; - let contentRequest = `@joyboy send ${amount} STRK to @alice.xyz`; + // /** Send a note */ + // let amount: number = 1; + // // let contentRequest = "@joyboy send 10 STRK to @alice.xyz"; + // let contentRequest = `@joyboy send ${amount} STRK to @alice.xyz`; - let content = "a test"; - // Check request, need to be undefined - let request = checkAndFilterSocialPayContent(content); - logDev(`first request need to be undefined request ${request}`); - expect(request).to.eq(undefined); - // Check request, need to be defined with sender, amount, token, recipient - request = checkAndFilterSocialPayContent(contentRequest); - logDev(`second request = ${JSON.stringify(request)}`); - expect(true).to.eq(true); - console.log("request", request); - expect(request).to.deep.eq({ - sender: "@joyboy", - receiver: "@alice.xyz", - currency: "STRK", - amount: amount, - isValidAddress: false, - }); + // let content = "a test"; + // // Check request, need to be undefined + // let request = checkAndFilterSocialPayContent(content); + // logDev(`first request need to be undefined request ${request}`); + // expect(request).to.eq(undefined); + // // Check request, need to be defined with sender, amount, token, recipient + // request = checkAndFilterSocialPayContent(contentRequest); + // logDev(`second request = ${JSON.stringify(request)}`); + // expect(true).to.eq(true); + // console.log("request", request); + // expect(request).to.deep.eq({ + // sender: "@joyboy", + // receiver: "@alice.xyz", + // currency: "STRK", + // amount: amount, + // isValidAddress: false, + // }); /** @TODO prepare request */ diff --git a/prototype/test/tip_end_to_end.test.ts b/prototype/test/tip_end_to_end.test.ts index 9c696cb8..3b3d3247 100644 --- a/prototype/test/tip_end_to_end.test.ts +++ b/prototype/test/tip_end_to_end.test.ts @@ -53,238 +53,238 @@ dotenv.config(); * @description test1 to use predefined contract with token */ describe("End to end test", () => { - it("Deploy account and Pay tips End to end test", async function () { - this.timeout(0); // Disable timeout for this test + // it("Deploy account and Pay tips End to end test", 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 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 - ***/ + // 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(); + // /** 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?.nostrPk; - console.log("privateKey Bob", new Buffer(pkBob).toString("hex")); - console.log("bobPublicKey", bobPublicKey); + // let pkBob = stringToUint8Array(ACCOUNT_TEST_PROFILE?.bob?.nostrPrivateKey); + // const bobPublicKey = ACCOUNT_TEST_PROFILE?.bob?.nostrPk; + // console.log("privateKey Bob", new Buffer(pkBob).toString("hex")); + // console.log("bobPublicKey", bobPublicKey); - /** @TODO Alice account key */ - // Nostr account - // let { privateKey: pkAlice, publicKey: alicePublicKey } = generateKeypair(); + // /** @TODO Alice account key */ + // // Nostr account + // // let { privateKey: pkAlice, publicKey: alicePublicKey } = generateKeypair(); - let pkAlice = stringToUint8Array( - ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey - ); - const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPk; + // let pkAlice = stringToUint8Array( + // ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey + // ); + // const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPk; - // Bob contract/A.A - // @TODO Finish SNIP-6 to use it - // Use your ENV or Generate public and private key pair when A.A SNIP-06. - // const AAprivateKey = process.env.AA_PRIVATE_KEY ?? stark.randomAddress(); - // console.log("New account:\nprivateKey=", AAprivateKey); - // const AAstarkKeyPub = - // process.env.AA_PUBKEY ?? ec.starkCurve.getStarkKey(AAprivateKey); - // console.log("publicKey=", AAstarkKeyPub); - // await transferToken( - // account, - // accountAddress0, - // TOKENS_ADDRESS?.DEVNET?.ETH, - // // token?.address, // TOKENS_ADDRESS.SEPOLIA.TEST, - // 10 - // ); + // // Bob contract/A.A + // // @TODO Finish SNIP-6 to use it + // // Use your ENV or Generate public and private key pair when A.A SNIP-06. + // // const AAprivateKey = process.env.AA_PRIVATE_KEY ?? stark.randomAddress(); + // // console.log("New account:\nprivateKey=", AAprivateKey); + // // const AAstarkKeyPub = + // // process.env.AA_PUBKEY ?? ec.starkCurve.getStarkKey(AAprivateKey); + // // console.log("publicKey=", AAstarkKeyPub); + // // await transferToken( + // // account, + // // accountAddress0, + // // TOKENS_ADDRESS?.DEVNET?.ETH, + // // // token?.address, // TOKENS_ADDRESS.SEPOLIA.TEST, + // // 10 + // // ); - /** @description Uncomment to create your social account or comment and change your old contract in the constant ACCOUNT_TEST_PROFILE or direcly below***/ - // console.log("create social account"); + // /** @description Uncomment to create your social account or comment and change your old contract in the constant ACCOUNT_TEST_PROFILE or direcly below***/ + // // console.log("create social account"); - // let accountBob = await createSocialContract(bobPublicKey); - /** uncomment to use social account deploy when SNIP-6 finish */ + // // let accountBob = await createSocialContract(bobPublicKey); + // /** uncomment to use social account deploy when SNIP-6 finish */ - // let accountBob = await createSocialAccount( - // bobPublicKey, - // AAprivateKey, - // AAstarkKeyPub - // ); - // let accountBob = { - // contract_address: undefined, - // }; - // console.log("accountBob?.contract_address ", accountBob?.contract_address); - let socialPayBob; - let socialAlice; + // // let accountBob = await createSocialAccount( + // // bobPublicKey, + // // AAprivateKey, + // // AAstarkKeyPub + // // ); + // // let accountBob = { + // // contract_address: undefined, + // // }; + // // console.log("accountBob?.contract_address ", accountBob?.contract_address); + // let socialPayBob; + // let socialAlice; - if (process.env.IS_DEPLOY_CONTRACT == 'true') { - let accountBob = await createSocialContract(bobPublicKey); + // if (process.env.IS_DEPLOY_CONTRACT == 'true') { + // let accountBob = await createSocialContract(bobPublicKey); - socialPayBob = await prepareAndConnectContract( - accountBob?.contract_address ?? // uncomment if you recreate a contract - ACCOUNT_TEST_PROFILE?.bob?.contract ?? - "0x0538907b56f07ef4f90e6f2da26a099ccfbc64e1cc4d03ff1e627fa7c2eb78ac", - account - ); + // socialPayBob = await prepareAndConnectContract( + // accountBob?.contract_address ?? // uncomment if you recreate a contract + // ACCOUNT_TEST_PROFILE?.bob?.contract ?? + // "0x0538907b56f07ef4f90e6f2da26a099ccfbc64e1cc4d03ff1e627fa7c2eb78ac", + // account + // ); - let accountAlice = await createSocialContract(alicePublicKey); - /** uncomment to use social account deploy when SNIP-6 finish */ - // let accountAlice = await createSocialAccount( - // alicePublicKey, - // AAprivateKeyAlice, - // AAstarkKeyPubAlice - // ); - // console.log("accountAlice", accountAlice?.contract_address); + // let accountAlice = await createSocialContract(alicePublicKey); + // /** uncomment to use social account deploy when SNIP-6 finish */ + // // let accountAlice = await createSocialAccount( + // // alicePublicKey, + // // AAprivateKeyAlice, + // // AAstarkKeyPubAlice + // // ); + // // console.log("accountAlice", accountAlice?.contract_address); - socialAlice = await prepareAndConnectContract( - accountAlice?.contract_address ?? - ACCOUNT_TEST_PROFILE?.alice?.contract ?? - "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", - account - ); - } else { - socialPayBob = await prepareAndConnectContract( - ACCOUNT_TEST_PROFILE?.bob?.contract ?? - "0x0538907b56f07ef4f90e6f2da26a099ccfbc64e1cc4d03ff1e627fa7c2eb78ac", - account - ); + // socialAlice = await prepareAndConnectContract( + // accountAlice?.contract_address ?? + // ACCOUNT_TEST_PROFILE?.alice?.contract ?? + // "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", + // account + // ); + // } else { + // socialPayBob = await prepareAndConnectContract( + // ACCOUNT_TEST_PROFILE?.bob?.contract ?? + // "0x0538907b56f07ef4f90e6f2da26a099ccfbc64e1cc4d03ff1e627fa7c2eb78ac", + // account + // ); - socialAlice = await prepareAndConnectContract( - ACCOUNT_TEST_PROFILE?.alice?.contract ?? - "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", - account - ); - } + // socialAlice = await prepareAndConnectContract( + // ACCOUNT_TEST_PROFILE?.alice?.contract ?? + // "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", + // account + // ); + // } - let pkBobAccount = await socialPayBob?.get_public_key(); - console.log("public key Bob account", pkBobAccount); - // let token = await createToken() + // let pkBobAccount = await socialPayBob?.get_public_key(); + // console.log("public key Bob account", pkBobAccount); + // // let token = await createToken() - // let TOKEN_TEST=token?.address - // console.log("TOKEN_TEST",TOKEN_TEST) - // await transferToken( - // account, - // socialPayBob?.address, - // token?.address, // TOKENS_ADDRESS.SEPOLIA.TEST, - // 10 - // ); + // // let TOKEN_TEST=token?.address + // // console.log("TOKEN_TEST",TOKEN_TEST) + // // await transferToken( + // // account, + // // socialPayBob?.address, + // // token?.address, // TOKENS_ADDRESS.SEPOLIA.TEST, + // // 10 + // // ); - // const AAprivateKeyAlice = stark.randomAddress(); - // console.log("New account:\nprivateKey=", AAprivateKeyAlice); - // const AAstarkKeyPubAlice = ec.starkCurve.getStarkKey(AAprivateKeyAlice); - // console.log("publicKey=", AAstarkKeyPubAlice); - /** @description Uncomment to create your social account or comment and change your old contract in the constant ACCOUNT_TEST_PROFILE or direcly below***/ + // // const AAprivateKeyAlice = stark.randomAddress(); + // // console.log("New account:\nprivateKey=", AAprivateKeyAlice); + // // const AAstarkKeyPubAlice = ec.starkCurve.getStarkKey(AAprivateKeyAlice); + // // console.log("publicKey=", AAstarkKeyPubAlice); + // /** @description Uncomment to create your social account or comment and change your old contract in the constant ACCOUNT_TEST_PROFILE or direcly below***/ - let pkAliceAccount = await socialAlice?.get_public_key(); - console.log("public key Alice Account", pkAliceAccount); + // let pkAliceAccount = await socialAlice?.get_public_key(); + // console.log("public key Alice Account", pkAliceAccount); - /** Send a note */ - let amount: number = 1; - let strkToken = await prepareAndConnectContract( - TOKENS_ADDRESS?.SEPOLIA?.TEST, - account - ); + // /** Send a note */ + // let amount: number = 1; + // let strkToken = await prepareAndConnectContract( + // TOKENS_ADDRESS?.SEPOLIA?.TEST, + // account + // ); - // let balanceAlice = await strkToken?.balanceOf( - // ACCOUNT_TEST_PROFILE?.alice?.contract - // ); + // // let balanceAlice = await strkToken?.balanceOf( + // // ACCOUNT_TEST_PROFILE?.alice?.contract + // // ); - // console.log("Alice balance STRK", balanceAlice); - // expect(balanceAlice).to.eq(BigInt(0)); + // // console.log("Alice balance STRK", balanceAlice); + // // expect(balanceAlice).to.eq(BigInt(0)); - /** @TODO - * utils to fetch token address - * Format socialRequest and input*/ - // Handle transfer with SocialPay A.A+ - let profileBob = { - pubkey: bobPublicKey, - relays: ["wss://relay.joyboy.community.com"], - }; - let nprofileBob = nip19.nprofileEncode(profileBob); - console.log("nprofileBob", nprofileBob); - let nprofileAlice = nip19.nprofileEncode({ - pubkey: alicePublicKey, - relays: [], - }); - console.log("nprofileAlice", nprofileAlice); - // let symbol = await strkToken.symbol(); - let symbol = "BIG_TOKEN"; - console.log("symbol", symbol); - let timestamp = new Date().getTime(); - let content = `${nprofileBob} send ${amount} ${symbol} to ${nprofileAlice}`; - console.log("content", content); - const finalizedFunctionParametersEvent = finalizeEvent( - { - kind: 1, - tags: [], - content: content, - created_at: timestamp, - }, - pkBob - ); - const signature = finalizedFunctionParametersEvent.sig; - const signatureR = signature.slice(0, signature.length / 2); - const signatureS = signature.slice(signature.length / 2); - if (signature) { - const functionParametersDummy = { - public_key: uint256.bnToUint256(BigInt("0x" + bobPublicKey)), - created_at: timestamp, - kind: 1, - tags: byteArray.byteArrayFromString("[]"), // tags - content: { - amount: uint256.bnToUint256(BigInt(amount)), // amount - // amount:cairo.uint256(amount), - token: byteArray.byteArrayFromString(symbol), // token symbol - token_address: TOKENS_ADDRESS?.SEPOLIA?.TEST, // token address - joyboy: { - public_key: uint256.bnToUint256(BigInt("0x" + bobPublicKey)), - // [], - relays: ["wss://relay.joyboy.community.com"], - }, - recipient: { - public_key: uint256.bnToUint256(BigInt("0x" + alicePublicKey)), - realys: [], - }, - }, + // /** @TODO + // * utils to fetch token address + // * Format socialRequest and input*/ + // // Handle transfer with SocialPay A.A+ + // let profileBob = { + // pubkey: bobPublicKey, + // relays: ["wss://relay.joyboy.community.com"], + // }; + // let nprofileBob = nip19.nprofileEncode(profileBob); + // console.log("nprofileBob", nprofileBob); + // let nprofileAlice = nip19.nprofileEncode({ + // pubkey: alicePublicKey, + // relays: [], + // }); + // console.log("nprofileAlice", nprofileAlice); + // // let symbol = await strkToken.symbol(); + // let symbol = "BIG_TOKEN"; + // console.log("symbol", symbol); + // let timestamp = new Date().getTime(); + // let content = `${nprofileBob} send ${amount} ${symbol} to ${nprofileAlice}`; + // console.log("content", content); + // const finalizedFunctionParametersEvent = finalizeEvent( + // { + // kind: 1, + // tags: [], + // content: content, + // created_at: timestamp, + // }, + // pkBob + // ); + // const signature = finalizedFunctionParametersEvent.sig; + // const signatureR = signature.slice(0, signature.length / 2); + // const signatureS = signature.slice(signature.length / 2); + // if (signature) { + // const functionParametersDummy = { + // public_key: uint256.bnToUint256(BigInt("0x" + bobPublicKey)), + // created_at: timestamp, + // kind: 1, + // tags: byteArray.byteArrayFromString("[]"), // tags + // content: { + // amount: uint256.bnToUint256(BigInt(amount)), // amount + // // amount:cairo.uint256(amount), + // token: byteArray.byteArrayFromString(symbol), // token symbol + // token_address: TOKENS_ADDRESS?.SEPOLIA?.TEST, // token address + // joyboy: { + // public_key: uint256.bnToUint256(BigInt("0x" + bobPublicKey)), + // // [], + // relays: ["wss://relay.joyboy.community.com"], + // }, + // recipient: { + // public_key: uint256.bnToUint256(BigInt("0x" + alicePublicKey)), + // realys: [], + // }, + // }, - recipient_address: - ACCOUNT_TEST_PROFILE?.alice?.contract ?? - "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", - signature: { - r: cairo.uint256(BigInt("0x" + signatureR)), - s: cairo.uint256(BigInt("0x" + signatureS)), - // r: uint256.bnToUint256(BigInt("0x" + signatureR)), - // s: uint256.bnToUint256(BigInt("0x" + signatureS)), - }, - }; + // recipient_address: + // ACCOUNT_TEST_PROFILE?.alice?.contract ?? + // "0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1", + // signature: { + // r: cairo.uint256(BigInt("0x" + signatureR)), + // s: cairo.uint256(BigInt("0x" + signatureS)), + // // r: uint256.bnToUint256(BigInt("0x" + signatureR)), + // // s: uint256.bnToUint256(BigInt("0x" + signatureS)), + // }, + // }; - console.log("before tx handle transfer"); - console.log( - "finalizedFunctionParametersEvent", - finalizedFunctionParametersEvent - ); - console.log( - "signature", - signature - ); + // console.log("before tx handle transfer"); + // console.log( + // "finalizedFunctionParametersEvent", + // finalizedFunctionParametersEvent + // ); + // console.log( + // "signature", + // signature + // ); - console.log("functionParametersDummy", functionParametersDummy); - // console.log("account address", account?.address); - // console.log(" socialPayBob?.address", socialPayBob?.address); - const tx = await account.execute({ - contractAddress: socialPayBob?.address, - calldata: functionParametersDummy, - entrypoint: "handle_transfer", - }); + // console.log("functionParametersDummy", functionParametersDummy); + // // console.log("account address", account?.address); + // // console.log(" socialPayBob?.address", socialPayBob?.address); + // const tx = await account.execute({ + // contractAddress: socialPayBob?.address, + // calldata: functionParametersDummy, + // entrypoint: "handle_transfer", + // }); - console.log("tx handle transfer", tx); - } + // console.log("tx handle transfer", tx); + // } - // console.log("Alice balance STRK", balanceAlice); - // expect(balanceAlice).to.eq(cairo.uint256(amount)); - }); + // // console.log("Alice balance STRK", balanceAlice); + // // expect(balanceAlice).to.eq(cairo.uint256(amount)); + // }); }); diff --git a/prototype/utils/escrow.ts b/prototype/utils/escrow.ts new file mode 100644 index 00000000..43334059 --- /dev/null +++ b/prototype/utils/escrow.ts @@ -0,0 +1,109 @@ +import { + Account, + json, + hash, + CallData, + Contract, + cairo, + uint256, +} from "starknet"; +import fs from "fs"; +import dotenv from "dotenv"; +import { nostrPubkeyToUint256 } from "./format"; +import { provider } from "./starknet"; +import { transferToken } from "./token"; +import path from "path"; + +dotenv.config(); +const PATH_SOCIAL_ACCOUNT = path.resolve( + __dirname, + "../../onchain/target/dev/joyboy_DepositEscrow.contract_class.json" +); +const PATH_SOCIAL_ACCOUNT_COMPILED = path.resolve( + __dirname, + "../../onchain/target/dev/joyboy_DepositEscrow.compiled_contract_class.json" +); + +/** @TODO spec need to be discuss. This function serve as an exemple */ +export const createEscrowAccount = async () => { + try { + // initialize existing predeployed account 0 of Devnet + const privateKey0 = process.env.DEV_PK as string; + const accountAddress0 = process.env.DEV_PUBLIC_KEY as string; + + // Devnet or Sepolia account + const account0 = new Account(provider, accountAddress0, privateKey0, "1"); + let EscrowClassHash = process.env.ESCROW_CLASS_HASH as string; + + const compiledSierraAAaccount = json.parse( + fs.readFileSync(PATH_SOCIAL_ACCOUNT).toString("ascii") + ); + const compiledAACasm = json.parse( + fs.readFileSync(PATH_SOCIAL_ACCOUNT_COMPILED).toString("ascii") + ); + /** Get class hash account */ + + // const ch = hash.computeSierraContractClassHash(compiledSierraAAaccount); + // const compCH = hash.computeCompiledClassHash(compiledAACasm); + // let pubkeyUint = pubkeyToUint256(nostrPublicKey); + + //Devnet + // // fund account address before account creation + // const { data: answer } = await axios.post( + // "http://127.0.0.1:5050/mint", + // { + // address: AAcontractAddress, + // amount: 50_000_000_000_000_000_000, + // lite: true, + // }, + // { headers: { "Content-Type": "application/json" } } + // ); + // console.log("Answer mint =", answer); + + // deploy account + + // const AAaccount = new Account(provider, AAcontractAddress, AAprivateKey); + /** @description uncomment this to declare your accout */ + // console.log("declare account"); + console.log("try declare account"); + const declareResponse = await account0.declare({ + contract: compiledSierraAAaccount, + casm: compiledAACasm, + }); + console.log("Declare deploy", declareResponse?.transaction_hash); + await provider.waitForTransaction(declareResponse?.transaction_hash); + const contractClassHash = declareResponse.class_hash; + EscrowClassHash = contractClassHash; + + + const nonce = await account0?.getNonce(); + console.log("nonce", nonce); + + const { transaction_hash, contract_address } = + await account0.deployContract({ + classHash: EscrowClassHash, + constructorCalldata: [], + }); + + console.log("transaction_hash", transaction_hash); + console.log("contract_address", contract_address); + let tx = await account0?.waitForTransaction(transaction_hash); + + console.log("Tx deploy", tx); + await provider.waitForTransaction(transaction_hash); + console.log( + "✅ New contract Social created.\n address =", + contract_address + ); + + // const contract = new Contract(compiledSierraAAaccount, contract_address, account0) + return { + contract_address, + tx, + // contract + }; + } catch (error) { + console.log("Error createEscrowAccount= ", error); + } +}; +