Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing inmemory #610

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/modules/lite/creator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,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 @@ -408,8 +408,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
13 changes: 10 additions & 3 deletions src/modules/lite/explorer/pages/CreateProposal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useToken } from "../../hooks/useToken"
import { ResponsiveDialog } from "modules/explorer/components/ResponsiveDialog"
import { useDAO } from "services/services/dao/hooks/useDAO"
import { useDAOID } from "modules/explorer/pages/DAO/router"
import { EnvKey, getEnv } from "services/config"
dayjs.extend(duration)

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

export const ProposalCreator: React.FC<{ id?: string }> = 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 @@ -569,8 +570,14 @@ export const ProposalCreator: React.FC<{ id?: string }> = props => {
data.startTime = String(dayjs().valueOf())
data.endTime = calculateEndTime(values.endTimeDays!, values.endTimeHours!, values.endTimeMinutes!)

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
14 changes: 11 additions & 3 deletions src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useSinglePoll } from "../../hooks/usePoll"
import { ProposalStatus } from "../../components/ProposalTableRowStatusBadge"
import { BackButton } from "modules/lite/components/BackButton"
import { voteOnLiteProposal } from "services/services/lite/lite-services"
import { EnvKey, getEnv } from "services/config"

const PageContainer = styled("div")({
marginBottom: 50,
Expand Down Expand Up @@ -53,7 +54,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
const isMobileSmall = useMediaQuery(theme.breakpoints.down("sm"))
const navigate = useHistory()
const { state } = useLocation<{ poll: Poll; choices: Choice[] }>()
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 @@ -83,8 +84,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 All @@ -111,6 +118,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
return
}
} catch (error) {
console.log("error: ", error)
openNotification({
message: `Something went wrong!!`,
autoHideDuration: 3000,
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
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
}

await saveLiteCommunity(signature, publicKey, payloadBytes)

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 @@ -190,7 +192,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 @@ -207,8 +215,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 @@ -182,7 +184,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 @@ -199,8 +207,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 }
}
Loading