Skip to content

Commit

Permalink
Fix signature and deployer
Browse files Browse the repository at this point in the history
Signed-off-by: Manank Patni <[email protected]>
  • Loading branch information
Man-Jain committed Aug 7, 2023
1 parent bacd3cd commit 3b40442
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 29 deletions.
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
9 changes: 5 additions & 4 deletions src/services/beacon/hooks/useTezos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,27 @@ export const useTezos = (): WalletConnectReturn => {
async (newNetwork?: Network) => {
const newTezos: TezosToolkit = initTezosInstance(network || newNetwork)

let wallet
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 })
}

const account = await newTezos.wallet.pkh()

dispatch({
type: TezosActionType.UPDATE_TEZOS,
payload: {
network: newNetwork || network,
tezos: newTezos,
account,
wallet
wallet: wallet as BeaconWallet
}
})
mixpanel.identify(account)
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 }
}

0 comments on commit 3b40442

Please sign in to comment.