Skip to content

Commit

Permalink
Merge in memory testing changes & resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
benefacto committed Jan 9, 2024
2 parents 2216e85 + 8d3d35f commit e3238f7
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ REACT_APP_MIXPANEL_DEBUG_ENABLED=false
REACT_APP_MIXPANEL_TOKEN=#YOUR_MIXPANEL_TOKEN_HERE
REACT_APP_NETWORK=ghostnet
REACT_APP_URL=http://localhost:3000
REACT_APP_V2_URL=http://localhost:3000
REACT_APP_V2_URL=http://localhost:3000
REACT_APP_IS_NOT_TESTING=false
13 changes: 10 additions & 3 deletions src/modules/lite/creator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ const CommunityForm = ({ submitForm, values, setFieldValue, errors, touched, set

export const CommunityCreator: React.FC = () => {
const navigate = useHistory()
const { network, account, wallet } = useTezos()
const { network, account, wallet, tezos } = useTezos()
const openNotification = useNotification()

const initialState: Community = {
Expand Down Expand Up @@ -489,8 +489,15 @@ export const CommunityCreator: React.FC = () => {
values.members.push(account)

try {
const { signature, payloadBytes } = await getSignature(account, wallet, JSON.stringify(values))
const publicKey = (await wallet?.client.getActiveAccount())?.publicKey
const { signature, payloadBytes } = await getSignature(account, wallet, network, JSON.stringify(values), tezos)
let publicKey

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true") {
publicKey = await tezos.signer.publicKey()
} else {
publicKey = (await wallet?.client.getActiveAccount())?.publicKey
}

if (!signature) {
openNotification({
message: `Issue with Signature`,
Expand Down
14 changes: 10 additions & 4 deletions src/modules/lite/explorer/pages/CreateProposal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import CodeOffIcon from "@mui/icons-material/CodeOff"
import { ProposalCodeEditorInput } from "modules/explorer/components/ProposalFormInput"
import Prism, { highlight } from "prismjs"
import "prism-themes/themes/prism-night-owl.css"

import { EnvKey, getEnv } from "services/config"
dayjs.extend(duration)

const ProposalContainer = styled(Grid)(({ theme }) => ({
Expand Down Expand Up @@ -662,7 +662,7 @@ const calculateEndTime = (days: number, hours: number, minutes: number) => {

export const ProposalCreator: React.FC<{ id?: string; onClose?: any }> = props => {
const navigate = useHistory()
const { network, account, wallet } = useTezos()
const { network, account, wallet, tezos } = useTezos()
const openNotification = useNotification()
const [isLoading, setIsLoading] = useState(false)
const daoId = useDAOID()
Expand Down Expand Up @@ -699,8 +699,14 @@ export const ProposalCreator: React.FC<{ id?: string; onClose?: any }> = props =
data.endTime = calculateEndTime(values.endTimeDays!, values.endTimeHours!, values.endTimeMinutes!)
data.author = account

const { signature, payloadBytes } = await getSignature(account, wallet, JSON.stringify(data))
const publicKey = (await wallet?.client.getActiveAccount())?.publicKey
const { signature, payloadBytes } = await getSignature(account, wallet, network, JSON.stringify(data), tezos)
let publicKey
if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true") {
publicKey = await tezos.signer.publicKey()
} else {
publicKey = (await wallet?.client.getActiveAccount())?.publicKey
}

if (!signature) {
openNotification({
message: `Issue with Signature`,
Expand Down
13 changes: 10 additions & 3 deletions src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useDAO } from "services/services/dao/hooks/useDAO"
import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteWeight"
import BigNumber from "bignumber.js"
import { ArrowBackIosOutlined } from "@material-ui/icons"
import { EnvKey, getEnv } from "services/config"

const PageContainer = styled("div")({
marginBottom: 50,
Expand Down Expand Up @@ -57,7 +58,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
const { state } = useLocation<{ poll: Poll; choices: Choice[]; daoId: string }>()
const navigate = useHistory()
const { data: dao } = useDAO(state?.daoId)
const { account, wallet } = useTezos()
const { account, wallet, network, tezos } = useTezos()
const openNotification = useNotification()
const [refresh, setRefresh] = useState<number>()
const community = useCommunity(id)
Expand Down Expand Up @@ -91,8 +92,14 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
}

try {
const publicKey = (await wallet?.client.getActiveAccount())?.publicKey
const { signature, payloadBytes } = await getSignature(account, wallet, JSON.stringify(votesData))
const { signature, payloadBytes } = await getSignature(account, wallet, network, JSON.stringify(votesData), tezos)
let publicKey
if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true") {
publicKey = await tezos.signer.publicKey()
} else {
publicKey = (await wallet?.client.getActiveAccount())?.publicKey
}

if (!signature) {
openNotification({
message: `Issue with Signature`,
Expand Down
28 changes: 20 additions & 8 deletions src/services/beacon/context.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { createContext, useEffect, useReducer } from "react"
import mixpanel from "mixpanel-browser"
import { createTezos, createWallet, getTezosNetwork } from "./utils"
import { ALICE_PRIV_KEY, createTezos, createWallet, getTezosNetwork } from "./utils"
import { INITIAL_STATE, reducer, TezosState } from "./reducer"
import { TezosAction, TezosActionType } from "./actions"
import { InMemorySigner } from "@taquito/signer"
import { EnvKey, getEnv } from "services/config"
import { BeaconWallet } from "@taquito/beacon-wallet"

interface TezosProvider {
state: TezosState
Expand All @@ -19,20 +22,29 @@ const getSavedState = async (): Promise<TezosState> => {
try {
const network = getTezosNetwork()
const tezos = createTezos(network)
const wallet = createWallet(network)
const activeAccount = await wallet.client.getActiveAccount()

if (!activeAccount?.address) {
throw new Error("No wallet address found")
let wallet, account

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) === "true") {
wallet = createWallet(network)
account = await tezos.wallet.pkh()
tezos.setProvider({ wallet })
} else {
const signer = await InMemorySigner.fromSecretKey(ALICE_PRIV_KEY)
wallet = signer
account = await signer.publicKeyHash()
tezos.setProvider({ signer })
}

tezos.setProvider({ wallet })
if (!account) {
throw new Error("No wallet address found")
}

return {
network,
tezos,
wallet,
account: activeAccount.address
wallet: wallet as BeaconWallet,
account
}
} catch (error) {
return INITIAL_STATE
Expand Down
51 changes: 40 additions & 11 deletions src/services/beacon/hooks/useTezos.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useQueryClient } from "react-query"
import { useCallback, useContext } from "react"
import { MichelCodecPacker, TezosToolkit } from "@taquito/taquito"
import { connectWithBeacon, createTezos, Network, rpcNodes, TezosActionType } from "services/beacon"
import { ALICE_PRIV_KEY, connectWithBeacon, Network, rpcNodes, TezosActionType, createTezos } from "services/beacon"
import { TezosContext } from "services/beacon/context"
import { Tzip16Module } from "@taquito/tzip16"
import mixpanel from "mixpanel-browser"
import { BeaconWallet } from "@taquito/beacon-wallet"
import { EnvKey, getEnv } from "services/config"
import { InMemorySigner } from "@taquito/signer"

type WalletConnectReturn = {
tezos: TezosToolkit
Expand All @@ -17,6 +19,14 @@ type WalletConnectReturn = {
wallet: BeaconWallet | undefined
}

export const initTezosInstance = (network: Network) => {
const newTezos = new TezosToolkit(rpcNodes[network])
newTezos.setPackerProvider(new MichelCodecPacker())
newTezos.addExtension(new Tzip16Module())

return newTezos
}

export const useTezos = (): WalletConnectReturn => {
const {
state: { tezos, network, account, wallet },
Expand All @@ -27,20 +37,29 @@ export const useTezos = (): WalletConnectReturn => {

const connect = useCallback(
async (newNetwork?: Network) => {
const { wallet } = await connectWithBeacon(network)
const newTezos: TezosToolkit = initTezosInstance(network || newNetwork)

const newTezos: TezosToolkit = createTezos(network || newNetwork)
newTezos.setProvider({ wallet })
let wallet, account

const account = await newTezos.wallet.pkh()
if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) === "true") {
const { wallet: beaconWallet } = await connectWithBeacon(network)
wallet = beaconWallet
newTezos.setProvider({ wallet })
account = await newTezos.wallet.pkh()
} else {
const signer = await InMemorySigner.fromSecretKey(ALICE_PRIV_KEY)
wallet = signer
account = await signer.publicKeyHash()
newTezos.setProvider({ signer })
}

dispatch({
type: TezosActionType.UPDATE_TEZOS,
payload: {
network: newNetwork || network,
tezos: newTezos,
account,
wallet
wallet: wallet as BeaconWallet
}
})
mixpanel.identify(account)
Expand Down Expand Up @@ -79,17 +98,27 @@ export const useTezos = (): WalletConnectReturn => {
}
})
} else {
const { wallet } = await connectWithBeacon(newNetwork)
newTezos.setProvider({ wallet })
const newAccount = await newTezos.wallet.pkh()
let wallet, account

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) === "true") {
const { wallet: beaconWallet } = await connectWithBeacon(network)
wallet = beaconWallet
newTezos.setProvider({ wallet })
account = await newTezos.wallet.pkh()
} else {
const signer = await InMemorySigner.fromSecretKey(ALICE_PRIV_KEY)
wallet = signer
account = await signer.publicKeyHash()
newTezos.setProvider({ signer })
}

dispatch({
type: TezosActionType.UPDATE_TEZOS,
payload: {
network: newNetwork,
tezos: newTezos,
account: newAccount,
wallet
account,
wallet: wallet as BeaconWallet
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/beacon/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { EnvKey, getEnv } from "services/config"

export type Network = "mainnet" | "ghostnet"

export const ALICE_PRIV_KEY = "edsk3QoqBuvdamxouPhin7swCvkQNgq4jP5KZPbwWNnwdZpSpJiEbq"
export const ALICE_PRIV_KEY =
"edskS1q2utmfdhVsqFKvKbQHShFg7eme6xbVKoc5iRPeW1fX8a4M5iXdhzbyTca3r6CMfCfVQZAZ6yqiE5wqi9UyMr1VZ6S6dA"

export const rpcNodes: Record<Network, string> = {
mainnet: "https://mainnet.api.tez.ie",
Expand Down
1 change: 1 addition & 0 deletions src/services/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum EnvKey {
REACT_APP_LITE_API_URL = "REACT_APP_LITE_API_URL",
REACT_APP_API_URL = "REACT_APP_API_URL",
REACT_APP_BASE_URL = "REACT_APP_BASE_URL",
REACT_APP_IS_NOT_TESTING = "REACT_APP_IS_NOT_TESTING",
REACT_APP_DAO_DEPLOYER_API = "REACT_APP_DAO_DEPLOYER_API"
}

Expand Down
10 changes: 8 additions & 2 deletions src/services/contracts/baseDAO/hooks/useOriginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,14 @@ export const useOriginate = (template: DAOTemplate) => {
daoContract: contract.address,
tokenID: params.orgSettings.governanceToken.tokenId
}
const { signature, payloadBytes } = await getSignature(account, wallet, JSON.stringify(values))
const publicKey = (await wallet?.client.getActiveAccount())?.publicKey
const { signature, payloadBytes } = await getSignature(account, wallet, network, JSON.stringify(values), tezos)
let publicKey

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true") {
publicKey = await tezos.signer.publicKey()
} else {
publicKey = (await wallet?.client.getActiveAccount())?.publicKey
}

const resp = await saveLiteCommunity(signature, publicKey, payloadBytes)
const data = await resp.json()
Expand Down
22 changes: 19 additions & 3 deletions src/services/lite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { BeaconWallet } from "@taquito/beacon-wallet"
import { RequestSignPayloadInput, SigningType } from "@airgap/beacon-sdk"
import BigNumber from "bignumber.js"
import { Network } from "services/beacon"
import { TezosToolkit } from "@taquito/taquito"
import { EnvKey, getEnv } from "services/config"

export const getCurrentBlock = async (network: Network) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/head`
Expand Down Expand Up @@ -150,7 +152,13 @@ export const formatByDecimals = (value: string, decimals: string) => {
return nFormatter(new BigNumber(value).div(new BigNumber(10).pow(decimals)), 1)
}

export const getSignature = async (userAddress: string, wallet: BeaconWallet, data?: string) => {
export const getSignature = async (
userAddress: string,
wallet: BeaconWallet,
network: Network,
data?: string,
tezos?: TezosToolkit
) => {
const formattedInput: string = [
"Tezos Signed Message:",
process.env.REACT_APP_BASE_URL,
Expand All @@ -167,8 +175,16 @@ export const getSignature = async (userAddress: string, wallet: BeaconWallet, da
sourceAddress: userAddress
}

const signedPayload = await wallet?.client.requestSignPayload(payload)
const { signature } = signedPayload
let signature

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true" && tezos) {
const { sig: walletSign } = await tezos?.signer.sign(payloadBytes)
signature = walletSign
} else {
const signedPayload = await wallet?.client.requestSignPayload(payload)
const { signature: walletSign } = signedPayload
signature = walletSign
}

return { signature, payloadBytes }
}
22 changes: 19 additions & 3 deletions src/services/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { RequestSignPayloadInput, SigningType } from "@airgap/beacon-sdk"
import BigNumber from "bignumber.js"
import { Network } from "services/beacon"
import { networkNameMap } from "services/bakingBad"
import { TezosToolkit } from "@taquito/taquito"
import { EnvKey, getEnv } from "services/config"

export const getCurrentBlock = async (network: Network) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/head`
Expand Down Expand Up @@ -170,7 +172,13 @@ export const formatByDecimals = (value: string, decimals: string) => {
return nFormatter(new BigNumber(value).div(new BigNumber(10).pow(decimals)), 1)
}

export const getSignature = async (userAddress: string, wallet: BeaconWallet, data?: string) => {
export const getSignature = async (
userAddress: string,
wallet: BeaconWallet,
network: Network,
data?: string,
tezos?: TezosToolkit
) => {
const formattedInput: string = [
"Tezos Signed Message:",
process.env.REACT_APP_BASE_URL,
Expand All @@ -187,8 +195,16 @@ export const getSignature = async (userAddress: string, wallet: BeaconWallet, da
sourceAddress: userAddress
}

const signedPayload = await wallet?.client.requestSignPayload(payload)
const { signature } = signedPayload
let signature

if (getEnv(EnvKey.REACT_APP_IS_NOT_TESTING) !== "true" && tezos) {
const { sig: walletSign } = await tezos?.signer.sign(bytes)
signature = walletSign
} else {
const signedPayload = await wallet?.client.requestSignPayload(payload)
const { signature: walletSign } = signedPayload
signature = walletSign
}

return { signature, payloadBytes }
}

0 comments on commit e3238f7

Please sign in to comment.