@thru/passkey-manager 0.2.1 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import type { RegisterCredentialInstructionParams } from '../types';
2
+ import {
3
+ RegisterCredentialArgsBuilder,
4
+ PasskeyInstructionBuilder,
5
+ } from '../abi/thru/program/passkey_manager/types';
6
+
7
+ export function encodeRegisterCredentialInstruction(
8
+ params: RegisterCredentialInstructionParams
9
+ ): Uint8Array {
10
+ const { walletAccountIdx, lookupAccountIdx, seed, stateProof } = params;
11
+
12
+ if (seed.length !== 32) throw new Error('seed must be 32 bytes');
13
+ if (walletAccountIdx < 0 || walletAccountIdx > 0xffff) {
14
+ throw new Error('walletAccountIdx must be 0-65535');
15
+ }
16
+ if (lookupAccountIdx < 0 || lookupAccountIdx > 0xffff) {
17
+ throw new Error('lookupAccountIdx must be 0-65535');
18
+ }
19
+
20
+ const argsPayload = new RegisterCredentialArgsBuilder()
21
+ .set_wallet_account_idx(walletAccountIdx)
22
+ .set_lookup_account_idx(lookupAccountIdx)
23
+ .set_seed(seed)
24
+ .set_state_proof(stateProof)
25
+ .build();
26
+
27
+ return new PasskeyInstructionBuilder()
28
+ .payload()
29
+ .select('register_credential')
30
+ .writePayload(argsPayload)
31
+ .finish()
32
+ .build();
33
+ }
package/src/seeds.ts CHANGED
@@ -45,3 +45,36 @@ export async function deriveWalletAddress(
45
45
  const hashBuffer = await crypto.subtle.digest('SHA-256', preimage);
46
46
  return new Uint8Array(hashBuffer);
47
47
  }
48
+
49
+ /**
50
+ * Create a 32-byte seed for a credential lookup PDA.
51
+ * SHA-256(credentialId || walletName)
52
+ *
53
+ * Including walletName makes the lookup unique per (credential, wallet) pair,
54
+ * so one passkey can be an authority on multiple wallets with separate lookups.
55
+ */
56
+ export async function createCredentialLookupSeed(
57
+ credentialId: Uint8Array,
58
+ walletName: string
59
+ ): Promise<Uint8Array> {
60
+ const nameBytes = new TextEncoder().encode(walletName);
61
+ const data = new Uint8Array(credentialId.length + nameBytes.length);
62
+ data.set(credentialId, 0);
63
+ data.set(nameBytes, credentialId.length);
64
+
65
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
66
+ return new Uint8Array(hashBuffer);
67
+ }
68
+
69
+ /**
70
+ * Derive credential lookup PDA address from a credential ID and wallet name.
71
+ * Convenience wrapper: deriveWalletAddress(SHA-256(credentialId || walletName), programAddress)
72
+ */
73
+ export async function deriveCredentialLookupAddress(
74
+ credentialId: Uint8Array,
75
+ walletName: string,
76
+ programAddress: string
77
+ ): Promise<Uint8Array> {
78
+ const seed = await createCredentialLookupSeed(credentialId, walletName);
79
+ return deriveWalletAddress(seed, programAddress);
80
+ }
package/src/types.ts CHANGED
@@ -81,6 +81,13 @@ export interface AccountContext {
81
81
  getAccountIndex: (pubkey: Uint8Array) => number;
82
82
  }
83
83
 
84
+ export interface RegisterCredentialInstructionParams {
85
+ walletAccountIdx: number;
86
+ lookupAccountIdx: number;
87
+ seed: Uint8Array; // SHA-256(credentialId), 32 bytes
88
+ stateProof: Uint8Array;
89
+ }
90
+
84
91
  export type WalletSigner = {
85
92
  signTransaction: (payloadBase64: string) => Promise<string>;
86
93
  };