-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Cosmos Kit wallets and add experimental Browser tab (#1391)
- Loading branch information
Showing
11 changed files
with
550 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { StdSignDoc } from '@cosmjs/amino' | ||
import { DirectSignDoc } from '@cosmos-kit/core' | ||
import { useIframe } from '@cosmos-kit/react-lite' | ||
import { useEffect, useRef } from 'react' | ||
|
||
import { TxBody } from '@dao-dao/protobuf/codegen/cosmos/tx/v1beta1/tx' | ||
import { | ||
BrowserTab as StatelessBrowserTab, | ||
useDaoInfoContext, | ||
useDaoNavHelpers, | ||
} from '@dao-dao/stateless' | ||
import { ActionKey, CosmosMsgFor_Empty } from '@dao-dao/types' | ||
import { | ||
SITE_URL, | ||
aminoTypes, | ||
decodedStargateMsgToCw, | ||
getDaoProposalSinglePrefill, | ||
getFallbackImage, | ||
protobufToCwMsg, | ||
} from '@dao-dao/utils' | ||
export const BrowserTab = () => { | ||
const { | ||
name, | ||
imageUrl, | ||
chainId: currentChainId, | ||
coreAddress, | ||
polytoneProxies, | ||
} = useDaoInfoContext() | ||
const { getDaoProposalPath } = useDaoNavHelpers() | ||
|
||
const propose = (msgs: CosmosMsgFor_Empty[]) => | ||
window.open( | ||
getDaoProposalPath(coreAddress, 'create', { | ||
prefill: getDaoProposalSinglePrefill({ | ||
title: '', | ||
description: '', | ||
actions: [ | ||
{ | ||
actionKey: ActionKey.Custom, | ||
data: { | ||
message: JSON.stringify(msgs, undefined, 2), | ||
}, | ||
}, | ||
], | ||
}), | ||
}), | ||
'_blank' | ||
) | ||
|
||
const decodeDirect = (signDocBodyBytes: Uint8Array) => { | ||
const encodedMessages = TxBody.decode(signDocBodyBytes).messages | ||
const messages = encodedMessages.map((msg) => protobufToCwMsg(msg).msg) | ||
propose(messages) | ||
} | ||
const decodeAmino = (signDoc: StdSignDoc) => { | ||
const messages = signDoc.msgs.map( | ||
(msg) => decodedStargateMsgToCw(aminoTypes.fromAmino(msg)).msg | ||
) | ||
propose(messages) | ||
} | ||
|
||
const addressForChainId = (chainId: string) => | ||
(chainId === currentChainId ? coreAddress : polytoneProxies[chainId]) || '' | ||
|
||
const { wallet, iframeRef } = useIframe({ | ||
walletInfo: { | ||
prettyName: name, | ||
logo: imageUrl || SITE_URL + getFallbackImage(coreAddress), | ||
}, | ||
accountReplacement: (chainId) => ({ | ||
username: name, | ||
address: addressForChainId(chainId), | ||
}), | ||
walletClientOverrides: { | ||
// TODO(iframe): remove | ||
// @ts-ignore | ||
signAmino: (_chainId: string, _signer: string, signDoc: StdSignDoc) => { | ||
decodeAmino(signDoc) | ||
}, | ||
// TODO(iframe): remove | ||
// @ts-ignore | ||
signDirect: ( | ||
_chainId: string, | ||
_signer: string, | ||
signDoc: DirectSignDoc | ||
) => { | ||
if (!signDoc?.bodyBytes) { | ||
return { | ||
type: 'execute', | ||
} | ||
} | ||
|
||
decodeDirect(signDoc.bodyBytes) | ||
}, | ||
enable: (chainIds: string | string[]) => | ||
[chainIds].flat().every((chainId) => addressForChainId(chainId)) | ||
? { | ||
type: 'success', | ||
} | ||
: { | ||
type: 'error', | ||
error: 'Unsupported chain.', | ||
}, | ||
connect: (chainIds: string | string[]) => | ||
[chainIds].flat().every((chainId) => addressForChainId(chainId)) | ||
? { | ||
type: 'success', | ||
} | ||
: { | ||
type: 'error', | ||
error: 'Unsupported chain.', | ||
}, | ||
sign: () => ({ | ||
type: 'error', | ||
value: 'Unsupported.', | ||
}), | ||
signArbitrary: () => ({ | ||
type: 'error', | ||
value: 'Unsupported.', | ||
}), | ||
suggestToken: () => ({ | ||
type: 'success', | ||
}), | ||
addChain: () => ({ | ||
type: 'success', | ||
}), | ||
}, | ||
aminoSignerOverrides: { | ||
signAmino: (_signerAddress, signDoc) => { | ||
decodeAmino(signDoc) | ||
|
||
return { | ||
type: 'error', | ||
error: 'Handled by DAO browser.', | ||
} | ||
}, | ||
}, | ||
directSignerOverrides: { | ||
signDirect: (_signerAddress, signDoc) => { | ||
decodeDirect(signDoc.bodyBytes) | ||
|
||
return { | ||
type: 'error', | ||
error: 'Handled by DAO browser.', | ||
} | ||
}, | ||
}, | ||
}) | ||
|
||
// Connect to iframe wallet on load if disconnected. | ||
const connectingRef = useRef(false) | ||
useEffect(() => { | ||
if (wallet && !wallet.isWalletConnected && !connectingRef.current) { | ||
connectingRef.current = true | ||
try { | ||
wallet.connect() | ||
} finally { | ||
connectingRef.current = false | ||
} | ||
} | ||
}, [wallet]) | ||
|
||
return <StatelessBrowserTab iframeRef={iframeRef} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './BrowserTab' | ||
export * from './ProposalsTab' | ||
export * from './SubDaosTab' | ||
export * from './TreasuryAndNftsTab' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d34ba43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dapp-testnet – ./apps/dapp
dapp-testnet-da0da0.vercel.app
dao-dao-testnet.vercel.app
dapp-testnet-git-development-da0da0.vercel.app
testnet.daodao.zone
osmosis-testnet.daodao.zone
juno-testnet.daodao.zone
d34ba43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sda-testnet – ./apps/sda
dao-dao-ui-testnet-sdp.vercel.app
sda-testnet-da0da0.vercel.app
dao.testnet.daodao.zone
dao.osmosis-testnet.daodao.zone
sda-testnet-git-development-da0da0.vercel.app
dao.juno-testnet.daodao.zone