Skip to content

Commit f0d9525

Browse files
committed
feat: extend makeInteractiveSigner and make it exportable
- 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
1 parent 7c365a1 commit f0d9525

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

packages/web-components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export { makeAgoricKeplrConnection } from './src/keplr-connection/KeplrConnection.js';
44
export { makeAgoricWalletConnection } from './src/wallet-connection/walletConnection.js';
5+
export { makeInteractiveSigner } from './src/wallet-connection/makeInteractiveSigner.js';
56
export { suggestChain } from './src/wallet-connection/suggestChain.js';
67
export { Errors as AgoricKeplrConnectionErrors } from './src/errors.js';
78
export {

packages/web-components/src/wallet-connection/makeInteractiveSigner.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ const SwingsetMsgs = {
5555

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

58-
const SwingsetRegistry = new Registry([
59-
...defaultRegistryTypes,
60-
// XXX should this list be "upstreamed" to @agoric/cosmic-proto?
61-
[SwingsetMsgs.MsgWalletSpendAction.typeUrl, MsgWalletSpendAction],
62-
[SwingsetMsgs.MsgProvision.typeUrl, MsgProvision],
63-
]);
64-
6558
/**
6659
* @returns {StdFee}
6760
*/
@@ -127,19 +120,28 @@ const SwingsetConverters = {
127120
},
128121
};
129122

123+
/**
124+
* @typedef {object} MakeInteractiveSignerOpts
125+
* @property {Array<[string, import('@cosmjs/proto-signing').GeneratedType]>} registryTypes
126+
* @property {import('@cosmjs/stargate').SigningStargateClientOptions['gasPrice']} gasPrice
127+
* @property {AminoConverters} converters
128+
*/
129+
130130
/**
131131
* Use Keplr to sign offers and delegate object messaging to local storage key.
132132
* @param {string} chainId
133133
* @param {string} rpc
134134
* @param {Keplr} keplr
135135
* @param {typeof import('@cosmjs/stargate').SigningStargateClient.connectWithSigner} connectWithSigner
136+
* @param {MakeInteractiveSignerOpts} [opts]
136137
* Ref: https://docs.keplr.app/api/
137138
*/
138139
export const makeInteractiveSigner = async (
139140
chainId,
140141
rpc,
141142
keplr,
142143
connectWithSigner,
144+
opts,
143145
) => {
144146
await null;
145147
try {
@@ -163,10 +165,19 @@ export const makeInteractiveSigner = async (
163165
...SwingsetConverters,
164166
...createBankAminoConverters(),
165167
...createAuthzAminoConverters(),
168+
...(opts?.converters || {}),
166169
};
170+
const SwingsetRegistry = new Registry([
171+
...defaultRegistryTypes,
172+
// XXX should this list be "upstreamed" to @agoric/cosmic-proto?
173+
[SwingsetMsgs.MsgWalletSpendAction.typeUrl, MsgWalletSpendAction],
174+
[SwingsetMsgs.MsgProvision.typeUrl, MsgProvision],
175+
...(opts?.registryTypes || []),
176+
]);
167177
const signingClient = await connectWithSigner(rpc, offlineSigner, {
168178
aminoTypes: new AminoTypes(converters),
169179
registry: SwingsetRegistry,
180+
...(opts?.gasPrice ? { gasPrice: opts.gasPrice } : {}),
170181
});
171182
console.debug('InteractiveSigner', { signingClient });
172183

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

254+
return tx;
255+
},
256+
/**
257+
* Sign and broadcast any Action
258+
*
259+
* @param {import('@cosmjs/proto-signing').EncodeObject} msg encoded msg
260+
* @throws if account does not exist on chain, user cancels,
261+
* RPC connection fails, RPC service fails to broadcast (
262+
* for example, if signature verification fails)
263+
*/
264+
signAndBroadcast: async msg => {
265+
const { accountNumber, sequence } = await signingClient.getSequence(
266+
address,
267+
);
268+
console.debug({ accountNumber, sequence });
269+
270+
const msgs = [msg];
271+
console.debug('sign msg', { address, msgs, fee });
272+
273+
const tx = await signingClient.signAndBroadcast(address, msgs, fee);
274+
console.debug('msg result tx', tx);
275+
assertIsDeliverTxSuccess(tx);
276+
243277
return tx;
244278
},
245279
});

0 commit comments

Comments
 (0)