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

feat: extend makeInteractiveSigner and make it exportable #54

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/web-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export { makeAgoricKeplrConnection } from './src/keplr-connection/KeplrConnection.js';
export { makeAgoricWalletConnection } from './src/wallet-connection/walletConnection.js';
export { makeInteractiveSigner } from './src/wallet-connection/makeInteractiveSigner.js';
export { suggestChain } from './src/wallet-connection/suggestChain.js';
export { Errors as AgoricKeplrConnectionErrors } from './src/errors.js';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ const SwingsetMsgs = {

/** @typedef {{owner: string, spendAction: string}} WalletSpendAction */

const SwingsetRegistry = new Registry([
...defaultRegistryTypes,
// XXX should this list be "upstreamed" to @agoric/cosmic-proto?
[SwingsetMsgs.MsgWalletSpendAction.typeUrl, MsgWalletSpendAction],
[SwingsetMsgs.MsgProvision.typeUrl, MsgProvision],
]);

/**
* @returns {StdFee}
*/
Expand Down Expand Up @@ -127,19 +120,28 @@ const SwingsetConverters = {
},
};

/**
* @typedef {object} MakeInteractiveSignerOpts
* @property {Array<[string, import('@cosmjs/proto-signing').GeneratedType]>} registryTypes
* @property {import('@cosmjs/stargate').SigningStargateClientOptions['gasPrice']} gasPrice
* @property {AminoConverters} converters
*/

/**
* Use Keplr to sign offers and delegate object messaging to local storage key.
* @param {string} chainId
* @param {string} rpc
* @param {Keplr} keplr
* @param {typeof import('@cosmjs/stargate').SigningStargateClient.connectWithSigner} connectWithSigner
* @param {MakeInteractiveSignerOpts} [opts]
* Ref: https://docs.keplr.app/api/
*/
export const makeInteractiveSigner = async (
chainId,
rpc,
keplr,
connectWithSigner,
opts,
) => {
await null;
try {
Expand All @@ -163,10 +165,19 @@ export const makeInteractiveSigner = async (
...SwingsetConverters,
...createBankAminoConverters(),
...createAuthzAminoConverters(),
...(opts?.converters || {}),
};
const SwingsetRegistry = new Registry([
...defaultRegistryTypes,
// XXX should this list be "upstreamed" to @agoric/cosmic-proto?
[SwingsetMsgs.MsgWalletSpendAction.typeUrl, MsgWalletSpendAction],
[SwingsetMsgs.MsgProvision.typeUrl, MsgProvision],
...(opts?.registryTypes || []),
]);
const signingClient = await connectWithSigner(rpc, offlineSigner, {
aminoTypes: new AminoTypes(converters),
registry: SwingsetRegistry,
...(opts?.gasPrice ? { gasPrice: opts.gasPrice } : {}),
});
console.debug('InteractiveSigner', { signingClient });

Expand Down Expand Up @@ -240,6 +251,29 @@ export const makeInteractiveSigner = async (
console.debug('spend action result tx', tx);
assertIsDeliverTxSuccess(tx);

return tx;
},
/**
* Sign and broadcast any Action
*
* @param {import('@cosmjs/proto-signing').EncodeObject} msg encoded msg
* @throws if account does not exist on chain, user cancels,
* RPC connection fails, RPC service fails to broadcast (
* for example, if signature verification fails)
*/
signAndBroadcast: async msg => {
const { accountNumber, sequence } = await signingClient.getSequence(
address,
);
console.debug({ accountNumber, sequence });

const msgs = [msg];
console.debug('sign msg', { address, msgs, fee });

const tx = await signingClient.signAndBroadcast(address, msgs, fee);
console.debug('msg result tx', tx);
assertIsDeliverTxSuccess(tx);

return tx;
},
});
Expand Down