Skip to content

Commit

Permalink
escrow script
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jun 14, 2024
1 parent ae35dc3 commit 8750b62
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 237 deletions.
2 changes: 2 additions & 0 deletions prototype/.env.exemple
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions prototype/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const ACCOUNT_TEST_PROFILE = {
contract:
"0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d",
},
escrow:{
contract:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48",
SEPOLIA:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48"
}
};
export const ERROR_MESSAGES = {
EVENT_NOTE_INVALID: {
Expand Down
216 changes: 216 additions & 0 deletions prototype/test/escrow.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
64 changes: 32 additions & 32 deletions prototype/test/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
Loading

0 comments on commit 8750b62

Please sign in to comment.