react-native-wallet-keystore 0.1.0

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.
Files changed (39) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +376 -0
  3. package/WalletKeystore.podspec +29 -0
  4. package/android/build.gradle +62 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/com/walletkeystore/Secp256k1.kt +158 -0
  7. package/android/src/main/java/com/walletkeystore/WalletKeystoreCrypto.kt +233 -0
  8. package/android/src/main/java/com/walletkeystore/WalletKeystoreModule.kt +627 -0
  9. package/android/src/main/java/com/walletkeystore/WalletKeystorePackage.kt +31 -0
  10. package/ios/WalletKeystore.h +5 -0
  11. package/ios/WalletKeystore.mm +895 -0
  12. package/lib/module/NativeWalletKeystore.js +15 -0
  13. package/lib/module/NativeWalletKeystore.js.map +1 -0
  14. package/lib/module/errors.js +57 -0
  15. package/lib/module/errors.js.map +1 -0
  16. package/lib/module/index.js +9 -0
  17. package/lib/module/index.js.map +1 -0
  18. package/lib/module/keystore.js +220 -0
  19. package/lib/module/keystore.js.map +1 -0
  20. package/lib/module/package.json +1 -0
  21. package/lib/module/viem.js +65 -0
  22. package/lib/module/viem.js.map +1 -0
  23. package/lib/typescript/package.json +1 -0
  24. package/lib/typescript/src/NativeWalletKeystore.d.ts +26 -0
  25. package/lib/typescript/src/NativeWalletKeystore.d.ts.map +1 -0
  26. package/lib/typescript/src/errors.d.ts +29 -0
  27. package/lib/typescript/src/errors.d.ts.map +1 -0
  28. package/lib/typescript/src/index.d.ts +3 -0
  29. package/lib/typescript/src/index.d.ts.map +1 -0
  30. package/lib/typescript/src/keystore.d.ts +100 -0
  31. package/lib/typescript/src/keystore.d.ts.map +1 -0
  32. package/lib/typescript/src/viem.d.ts +24 -0
  33. package/lib/typescript/src/viem.d.ts.map +1 -0
  34. package/package.json +210 -0
  35. package/src/NativeWalletKeystore.ts +43 -0
  36. package/src/errors.ts +94 -0
  37. package/src/index.tsx +6 -0
  38. package/src/keystore.ts +317 -0
  39. package/src/viem.ts +99 -0
package/src/viem.ts ADDED
@@ -0,0 +1,99 @@
1
+ import {
2
+ hashMessage,
3
+ hashTypedData,
4
+ keccak256,
5
+ serializeTransaction,
6
+ type Hex,
7
+ type LocalAccount,
8
+ type SerializeTransactionFn,
9
+ type SignableMessage,
10
+ type TransactionSerializable,
11
+ type TypedData,
12
+ type TypedDataDefinition,
13
+ } from 'viem';
14
+ import { publicKeyToAddress, toAccount } from 'viem/accounts';
15
+
16
+ import { getPublicKey, signDigest } from './keystore';
17
+
18
+ export type KeystoreAccountOptions = {
19
+ /** Shown in the authentication prompt. */
20
+ reason?: string;
21
+ /** Skips a native round-trip. Must belong to `keyId`. */
22
+ publicKey?: Hex;
23
+ };
24
+
25
+ const DEFAULT_REASON = 'Sign with your wallet key';
26
+
27
+ /**
28
+ * Adapts a hardware-wrapped key into a viem {@link LocalAccount}.
29
+ *
30
+ * Every path hashes in JS and sends only a 32-byte digest to native, leaving
31
+ * EIP-191, EIP-712 and transaction serialization to viem.
32
+ *
33
+ * ```ts
34
+ * const account = await toKeystoreAccount('wallet-1');
35
+ * const client = createWalletClient({ account, chain: mainnet, transport: http() });
36
+ * await client.sendTransaction({ to: '0x…', value: 1n });
37
+ * ```
38
+ *
39
+ * Each signature raises its own authentication prompt, so batching several will
40
+ * prompt several times.
41
+ */
42
+ export async function toKeystoreAccount(
43
+ keyId: string,
44
+ options: KeystoreAccountOptions = {}
45
+ ): Promise<LocalAccount> {
46
+ const reason = options.reason ?? DEFAULT_REASON;
47
+ const publicKey = options.publicKey ?? (await getPublicKey(keyId));
48
+
49
+ // Derived here rather than natively: the address is a pure function of the
50
+ // public key, and keccak has no business in the native layer.
51
+ const address = publicKeyToAddress(publicKey);
52
+
53
+ const sign = (digest: Hex) =>
54
+ signDigest(keyId, digest, reason) as Promise<Hex>;
55
+
56
+ const account = toAccount({
57
+ address,
58
+
59
+ async sign({ hash }: { hash: Hex }) {
60
+ return sign(hash);
61
+ },
62
+
63
+ async signMessage({ message }: { message: SignableMessage }) {
64
+ return sign(hashMessage(message));
65
+ },
66
+
67
+ async signTypedData<
68
+ const typedData extends TypedData | Record<string, unknown>,
69
+ primaryType extends keyof typedData | 'EIP712Domain' = keyof typedData,
70
+ >(typedData: TypedDataDefinition<typedData, primaryType>) {
71
+ return sign(hashTypedData(typedData as TypedDataDefinition));
72
+ },
73
+
74
+ async signTransaction(
75
+ transaction: TransactionSerializable,
76
+ args?: {
77
+ serializer?:
78
+ SerializeTransactionFn<TransactionSerializable> | undefined;
79
+ }
80
+ ) {
81
+ const serializer = args?.serializer ?? serializeTransaction;
82
+
83
+ // Sign the hash of the unsigned serialization, then re-serialize with the
84
+ // signature attached; viem's serializer owns the EIP-155 and typed-tx
85
+ // rules for every transaction type.
86
+ const unsigned = (await serializer(transaction)) as Hex;
87
+ const signature = await sign(keccak256(unsigned));
88
+
89
+ return (await serializer(transaction, {
90
+ r: `0x${signature.slice(2, 66)}` as Hex,
91
+ s: `0x${signature.slice(66, 130)}` as Hex,
92
+ v: BigInt(parseInt(signature.slice(130, 132), 16)),
93
+ })) as Hex;
94
+ },
95
+ });
96
+
97
+ // toAccount's return type is a union; this pins it to the local branch.
98
+ return account as LocalAccount;
99
+ }