@thru/passkey-manager 0.2.1

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.
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # @thru/passkey-manager
2
+
3
+ Platform-agnostic TypeScript library for interacting with the on-chain `passkey_manager` program. It provides ABI-generated instruction builders, P-256 cryptographic utilities, and helpers for constructing passkey-authenticated transactions. Works in browsers, React Native, and Node.js.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @thru/passkey-manager
9
+ ```
10
+
11
+ ## Basic Usage
12
+
13
+ ```ts
14
+ import {
15
+ encodeCreateInstruction,
16
+ encodeValidateInstruction,
17
+ encodeTransferInstruction,
18
+ concatenateInstructions,
19
+ createWalletSeed,
20
+ deriveWalletAddress,
21
+ buildAccountContext,
22
+ createValidateChallenge,
23
+ parseDerSignature,
24
+ normalizeLowS,
25
+ PASSKEY_MANAGER_PROGRAM_ADDRESS,
26
+ } from '@thru/passkey-manager';
27
+
28
+ // Derive a wallet address from a passkey's public key
29
+ const seed = await createWalletSeed('my-wallet', pubkeyX, pubkeyY);
30
+ const walletAddress = await deriveWalletAddress(seed, PASSKEY_MANAGER_PROGRAM_ADDRESS);
31
+
32
+ // Build account context for the transaction
33
+ const ctx = buildAccountContext({
34
+ walletAddress: 'taWalletAddress...',
35
+ readWriteAccounts: [recipientBytes],
36
+ readOnlyAccounts: [],
37
+ });
38
+
39
+ // Encode instructions using ABI-generated builders
40
+ const create = encodeCreateInstruction({
41
+ walletAccountIdx: ctx.walletAccountIdx,
42
+ authority: { tag: 1, pubkeyX, pubkeyY },
43
+ seed,
44
+ stateProof,
45
+ });
46
+
47
+ const transfer = encodeTransferInstruction({
48
+ walletAccountIdx: ctx.walletAccountIdx,
49
+ toAccountIdx: ctx.getAccountIndex(recipientBytes),
50
+ amount: 1_000_000n,
51
+ });
52
+
53
+ // Combine multiple instructions into a single payload
54
+ const payload = concatenateInstructions([create, transfer]);
55
+
56
+ // Build the challenge that the passkey must sign
57
+ const challenge = await createValidateChallenge(nonce, ctx.accountAddresses, payload);
58
+ ```
59
+
60
+ ### Processing a Passkey Signature
61
+
62
+ ```ts
63
+ import {
64
+ parseDerSignature,
65
+ normalizeLowS,
66
+ encodeValidateInstruction,
67
+ } from '@thru/passkey-manager';
68
+
69
+ // Parse the DER-encoded signature from WebAuthn
70
+ const { r, s } = parseDerSignature(derSignatureBytes);
71
+ const normalizedS = normalizeLowS(s);
72
+
73
+ // Encode the validate instruction with the parsed signature
74
+ const validate = encodeValidateInstruction({
75
+ walletAccountIdx: ctx.walletAccountIdx,
76
+ authIdx: 0,
77
+ signatureR: r,
78
+ signatureS: normalizedS,
79
+ authenticatorData,
80
+ clientDataJSON,
81
+ });
82
+ ```
83
+
84
+ ## Instruction Builders
85
+
86
+ Each instruction maps to an on-chain `passkey_manager` program handler. All builders use ABI-generated types for type-safe serialization.
87
+
88
+ | Function | Description |
89
+ | --- | --- |
90
+ | `encodeCreateInstruction` | Create a new passkey-managed wallet account |
91
+ | `encodeValidateInstruction` | Submit a passkey signature for transaction authorization |
92
+ | `encodeTransferInstruction` | Transfer lamports from a managed wallet |
93
+ | `encodeInvokeInstruction` | Invoke a cross-program instruction from a managed wallet |
94
+ | `encodeAddAuthorityInstruction` | Add a passkey or pubkey authority to a wallet |
95
+ | `encodeRemoveAuthorityInstruction` | Remove an authority from a wallet |
96
+ | `concatenateInstructions` | Combine multiple encoded instructions into one payload |
97
+
98
+ ## Cryptographic Utilities
99
+
100
+ Platform-agnostic P-256 / ECDSA helpers that do not depend on any native crypto library beyond `crypto.subtle`:
101
+
102
+ - `parseDerSignature` -- extract r and s components from a DER-encoded ECDSA signature
103
+ - `normalizeLowS` -- enforce low-S form (BIP-62 / SEC1 compliance)
104
+ - `normalizeSignatureComponent` -- pad or trim a signature component to exactly 32 bytes
105
+ - `bytesToBigIntBE` / `bigIntToBytesBE` -- big-endian bigint conversion
106
+ - `P256_N` / `P256_HALF_N` -- P-256 curve order constants
107
+
108
+ ## Encoding Utilities
109
+
110
+ Zero-dependency byte manipulation functions:
111
+
112
+ - `arrayBufferToBase64Url` / `base64UrlToArrayBuffer` -- ArrayBuffer base64url conversion
113
+ - `bytesToBase64Url` / `base64UrlToBytes` -- Uint8Array base64url conversion
114
+ - `bytesToHex` / `hexToBytes` -- hex string conversion
115
+ - `bytesEqual` / `compareBytes` -- byte array comparison
116
+ - `uniqueAccounts` -- deduplicate account byte arrays
117
+
118
+ ## Account and Seed Helpers
119
+
120
+ - `createWalletSeed(walletName, pubkeyX, pubkeyY)` -- derive a deterministic 32-byte seed from a wallet name and passkey public key coordinates via SHA-256
121
+ - `deriveWalletAddress(seed, programAddress)` -- derive the on-chain PDA for a managed wallet
122
+ - `buildAccountContext(params)` -- build a sorted, deduplicated account context with index lookup for transaction construction
123
+ - `parseWalletNonce(data)` / `fetchWalletNonce(sdk, address)` -- read the current nonce from on-chain wallet account data
124
+
125
+ ## Types
126
+
127
+ ```ts
128
+ import type {
129
+ PasskeyMetadata,
130
+ PasskeyRegistrationResult,
131
+ PasskeySigningResult,
132
+ PasskeyDiscoverableSigningResult,
133
+ Authority,
134
+ AccountContext,
135
+ CreateInstructionParams,
136
+ ValidateInstructionParams,
137
+ TransferInstructionParams,
138
+ WalletSigner,
139
+ TransactionExecutionSummary,
140
+ } from '@thru/passkey-manager';
141
+ ```
142
+
143
+ | Type | Description |
144
+ | --- | --- |
145
+ | `PasskeyMetadata` | Local metadata for a registered passkey (credential ID, public key, RP ID) |
146
+ | `PasskeyRegistrationResult` | Result of WebAuthn credential creation |
147
+ | `PasskeySigningResult` | Parsed WebAuthn assertion with raw r/s components |
148
+ | `PasskeyDiscoverableSigningResult` | Signing result that includes the discovered credential ID |
149
+ | `Authority` | Tagged union for passkey (P-256) or pubkey (Ed25519) authorities |
150
+ | `AccountContext` | Sorted account list with index lookup for transaction building |