Skip to content

Commit 390deee

Browse files
committed
Better requested permission resolution
1 parent 74ea956 commit 390deee

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

src/lib/wharf/plugins/multisig.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,47 @@ export class WalletPluginMultiSig extends AbstractWalletPlugin implements Wallet
9797
);
9898
}
9999

100-
async getSigners(signer: PermissionLevel, context: TransactContext): Promise<PermissionLevel[]> {
101-
const account = await context.client.v1.chain.get_account(signer.actor);
102-
const permission = account.permissions.find((p) => p.perm_name.equals(signer.permission));
103-
if (!permission) {
104-
throw new Error('Requested permission not found');
105-
}
100+
async resolveSigners(
101+
auth: PermissionLevel,
102+
context: TransactContext,
103+
seen: Set<string>
104+
): Promise<PermissionLevel[]> {
105+
const key = String(auth);
106+
if (seen.has(key)) return [];
107+
seen.add(key);
108+
const account = await context.client.v1.chain.get_account(auth.actor);
109+
const permission = account.permissions.find((p) => p.perm_name.equals(auth.permission));
110+
if (!permission) return [auth];
106111
const accountAuths = permission.required_auth.accounts.map((a) => a.permission);
112+
if (accountAuths.length === 1 && permission.required_auth.threshold.equals(1)) {
113+
return this.resolveSigners(accountAuths[0], context, seen);
114+
}
107115
if (accountAuths.length > 0) {
108116
return accountAuths;
109117
}
110-
return [signer];
118+
return [auth];
119+
}
120+
121+
async getRequestedSigners(
122+
transaction: Transaction,
123+
context: TransactContext
124+
): Promise<PermissionLevel[]> {
125+
const requested: PermissionLevel[] = [];
126+
const seen = new Set<string>();
127+
for (const action of transaction.actions) {
128+
for (const auth of action.authorization) {
129+
const resolved = await this.resolveSigners(auth, context, seen);
130+
requested.push(...resolved);
131+
}
132+
}
133+
return requested;
111134
}
112135

113136
async propose(
114137
resolved: ResolvedSigningRequest,
115138
context: TransactContext
116139
): Promise<WalletPluginSignResponse> {
117-
const requested = await this.getSigners(resolved.signer, context);
140+
const requested = await this.getRequestedSigners(resolved.transaction, context);
118141
const session = this.getSession(context);
119142
const msig = new MsigContract({ client: context.client });
120143
const eosntime = new TimeContract({ client: context.client });

0 commit comments

Comments
 (0)