Skip to content

Commit 3ac6370

Browse files
authored
Merge pull request #69 from cometh-hq/develop
Fix/bitwarden (#68)
2 parents 1da33e3 + 19f392d commit 3ac6370

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

bun.lockb

0 Bytes
Binary file not shown.

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "yoanslvy"
77
}
88
],
9-
"version": "1.0.5",
9+
"version": "1.0.6",
1010
"description": "SDK Cometh Connect 4337",
1111
"repository": "https://github.com/cometh-hq/connect-sdk-4337.git",
1212
"keywords": [

packages/sdk/src/core/signers/passkeys/passkeyService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const sign = async ({
197197
[
198198
toHex(new Uint8Array(assertion.response.authenticatorData)),
199199
extractClientDataFields(assertion.response) as Hex,
200-
extractSignature(assertion.response),
200+
extractSignature(assertion.response.signature),
201201
]
202202
);
203203

@@ -510,4 +510,4 @@ export {
510510
sign,
511511
retrieveSmartAccountAddressFromPasskey,
512512
retrieveSmartAccountAddressFromPasskeyId,
513-
};
513+
};

packages/sdk/src/core/signers/passkeys/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ export function extractClientDataFields(
195195
* - <https://en.wikipedia.org/wiki/X.690#BER_encoding>
196196
*/
197197
export function extractSignature(
198-
response: AuthenticatorAssertionResponse
198+
signature: ArrayBuffer | Uint8Array
199199
): [bigint, bigint] {
200200
const check = (x: boolean): void => {
201201
if (!x) {
202202
throw new Error("invalid signature encoding");
203203
}
204204
};
205205

206-
// Decode the DER signature. Note that we assume that all lengths fit into 8-bit integers,
207-
// which is true for the kinds of signatures we are decoding but generally false. I.e. this
208-
// code should not be used in any serious application.
209-
const view = new DataView(response.signature);
206+
// Convert Uint8Array to ArrayBuffer if needed
207+
const buffer =
208+
signature instanceof Uint8Array ? signature.buffer : signature;
209+
const view = new DataView(buffer);
210210

211211
// check that the sequence header is valid
212212
check(view.getUint8(0) === 0x30);
@@ -218,7 +218,7 @@ export function extractSignature(
218218
const len = view.getUint8(offset + 1);
219219
const start = offset + 2;
220220
const end = start + len;
221-
const n = BigInt(toHex(new Uint8Array(view.buffer.slice(start, end))));
221+
const n = BigInt(toHex(new Uint8Array(buffer.slice(start, end))));
222222
check(n < maxUint256);
223223
return [n, end] as const;
224224
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { createWalletClient, custom } from "viem";
2+
3+
import type {
4+
Account,
5+
Chain,
6+
EIP1193Provider,
7+
Hex,
8+
SignableMessage,
9+
Transport,
10+
TypedData,
11+
TypedDataDefinition,
12+
WalletClient,
13+
} from "viem";
14+
15+
import { signTypedData } from "viem/actions";
16+
17+
export function walletClientToSmartAccountSigner<
18+
TChain extends Chain | undefined = Chain | undefined,
19+
>(walletClient: WalletClient<Transport, TChain, Account>) {
20+
return {
21+
address: walletClient.account.address,
22+
type: "local",
23+
source: "custom",
24+
publicKey: walletClient.account.address,
25+
signMessage: async ({
26+
message,
27+
}: { message: SignableMessage }): Promise<Hex> => {
28+
return walletClient.signMessage({ message });
29+
},
30+
async signTypedData<
31+
const TTypedData extends TypedData | Record<string, unknown>,
32+
TPrimaryType extends
33+
| keyof TTypedData
34+
| "EIP712Domain" = keyof TTypedData,
35+
>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>) {
36+
return signTypedData<TTypedData, TPrimaryType, TChain, Account>(
37+
walletClient,
38+
{
39+
account: walletClient.account,
40+
...typedData,
41+
}
42+
);
43+
},
44+
};
45+
}
46+
47+
export const providerToSmartAccountSigner = async (
48+
provider: EIP1193Provider,
49+
params?: {
50+
signerAddress: Hex;
51+
}
52+
) => {
53+
let account: Hex;
54+
if (!params) {
55+
try {
56+
[account] = await provider.request({
57+
method: "eth_requestAccounts",
58+
});
59+
} catch {
60+
[account] = await provider.request({
61+
method: "eth_accounts",
62+
});
63+
}
64+
} else {
65+
account = params.signerAddress;
66+
}
67+
const walletClient = createWalletClient({
68+
account: account as Hex,
69+
transport: custom(provider),
70+
});
71+
return walletClientToSmartAccountSigner(walletClient);
72+
};

packages/sdk/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import type {
4949
CreateSessionDataParams,
5050
GrantPermissionResponse,
5151
} from "./core/modules/sessionKey/types";
52+
import { providerToSmartAccountSigner } from "./core/signers/utils";
5253

5354
export {
5455
createSigner,
@@ -67,6 +68,7 @@ export {
6768
erc7579Actions,
6869
smartSessionActions,
6970
toSmartSessionsSigner,
71+
providerToSmartAccountSigner,
7072
ENTRYPOINT_ADDRESS_V07,
7173
customChains,
7274
SmartSessionMode,

0 commit comments

Comments
 (0)