Skip to content

Commit

Permalink
feat: extend makeInteractiveSigner and make it exportable
Browse files Browse the repository at this point in the history
- adds fifth optional argument `opts` to `makeInteractiveSigner`
- allows additional Registry types to be sent in `opts.registryTypes`
- allows additonal amino converters to be sent in `opts.converters`
- allows initial client gas price to be set with `opts.gasPrice`
- exposes `signAndBroadcast` method, that takes any EncodeObject as an argument
- exposes `makeInteractiveSigner` externally to consumers
  • Loading branch information
0xpatrickdev committed Oct 27, 2023
1 parent 7c365a1 commit f0d9525
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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

0 comments on commit f0d9525

Please sign in to comment.