@1001-digital/components 1.1.6 → 1.1.7
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1001-digital/components",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"*.css"
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
+
"@1001-digital/wagmi-in-app-wallet": "^0.1.0",
|
|
34
35
|
"@iconify/vue": "^5.0.0",
|
|
35
36
|
"@internationalized/date": "^3.8.0",
|
|
36
37
|
"@types/luxon": "^3.7.0",
|
|
@@ -1,221 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
createWalletClient,
|
|
7
|
-
custom,
|
|
8
|
-
getAddress,
|
|
9
|
-
hexToBigInt,
|
|
10
|
-
hexToNumber,
|
|
11
|
-
http,
|
|
12
|
-
numberToHex,
|
|
13
|
-
} from 'viem'
|
|
14
|
-
import { privateKeyToAccount, type PrivateKeyAccount } from 'viem/accounts'
|
|
15
|
-
|
|
16
|
-
const STORAGE_KEY = 'evm:in-app-wallet-pk'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Derive a private key from a BIP39 mnemonic and store it in localStorage.
|
|
20
|
-
* Call this before `connectAsync({ connector })`.
|
|
21
|
-
*/
|
|
22
|
-
export async function prepareInAppWallet(mnemonic: string): Promise<Address> {
|
|
23
|
-
const { mnemonicToAccount } = await import('viem/accounts')
|
|
24
|
-
const { bytesToHex } = await import('viem')
|
|
25
|
-
|
|
26
|
-
const normalized = mnemonic.trim().toLowerCase().replace(/\s+/g, ' ')
|
|
27
|
-
const hdAccount = mnemonicToAccount(normalized)
|
|
28
|
-
const hdKey = hdAccount.getHdKey()
|
|
29
|
-
const pk = bytesToHex(hdKey.privateKey!) as `0x${string}`
|
|
30
|
-
|
|
31
|
-
localStorage.setItem(STORAGE_KEY, pk)
|
|
32
|
-
return hdAccount.address
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type InAppWalletParameters = {
|
|
36
|
-
storageKey?: string
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
inAppWallet.type = 'inAppWallet' as const
|
|
40
|
-
|
|
41
|
-
export function inAppWallet(parameters: InAppWalletParameters = {}) {
|
|
42
|
-
const key = parameters.storageKey ?? STORAGE_KEY
|
|
43
|
-
|
|
44
|
-
type Provider =
|
|
45
|
-
ReturnType<typeof custom> extends (...args: infer A) => infer R ? R : never
|
|
46
|
-
|
|
47
|
-
// @ts-expect-error wagmi 0.4.x withCapabilities conditional return type
|
|
48
|
-
return createConnector<Provider>((config) => {
|
|
49
|
-
let account: PrivateKeyAccount | null = null
|
|
50
|
-
let currentChainId: number = config.chains[0].id
|
|
51
|
-
|
|
52
|
-
function loadAccount(): PrivateKeyAccount | null {
|
|
53
|
-
if (typeof window === 'undefined') return null
|
|
54
|
-
try {
|
|
55
|
-
const stored = localStorage.getItem(key)
|
|
56
|
-
if (stored?.startsWith('0x')) {
|
|
57
|
-
account = privateKeyToAccount(stored as `0x${string}`)
|
|
58
|
-
return account
|
|
59
|
-
}
|
|
60
|
-
} catch {}
|
|
61
|
-
return null
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function getChain(chainId?: number) {
|
|
65
|
-
return (
|
|
66
|
-
config.chains.find((c) => c.id === (chainId ?? currentChainId)) ??
|
|
67
|
-
config.chains[0]
|
|
68
|
-
)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
id: 'inAppWallet',
|
|
73
|
-
name: 'In App',
|
|
74
|
-
type: inAppWallet.type,
|
|
75
|
-
|
|
76
|
-
async connect({ chainId } = {}) {
|
|
77
|
-
const acct = account ?? loadAccount()
|
|
78
|
-
if (!acct) throw new Error('No in-app wallet key found in storage')
|
|
79
|
-
|
|
80
|
-
if (chainId) currentChainId = chainId
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
accounts: [getAddress(acct.address)],
|
|
84
|
-
chainId: currentChainId,
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
async disconnect() {
|
|
89
|
-
account = null
|
|
90
|
-
if (typeof window !== 'undefined') {
|
|
91
|
-
localStorage.removeItem(key)
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
async getAccounts() {
|
|
96
|
-
const acct = account ?? loadAccount()
|
|
97
|
-
return acct ? [getAddress(acct.address)] : []
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
async getChainId() {
|
|
101
|
-
return currentChainId
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
async getProvider() {
|
|
105
|
-
const chain = getChain()
|
|
106
|
-
const transport = config.transports?.[chain.id] ?? http()
|
|
107
|
-
|
|
108
|
-
const request = async ({
|
|
109
|
-
method,
|
|
110
|
-
params,
|
|
111
|
-
}: {
|
|
112
|
-
method: string
|
|
113
|
-
params?: unknown[]
|
|
114
|
-
}): Promise<unknown> => {
|
|
115
|
-
// Account methods
|
|
116
|
-
if (method === 'eth_accounts' || method === 'eth_requestAccounts') {
|
|
117
|
-
return account ? [account.address] : []
|
|
118
|
-
}
|
|
119
|
-
if (method === 'eth_chainId') {
|
|
120
|
-
return numberToHex(currentChainId)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Signing methods — handled locally
|
|
124
|
-
if (method === 'personal_sign') {
|
|
125
|
-
if (!account) throw new Error('Not connected')
|
|
126
|
-
const [data] = params as [Hex, Address]
|
|
127
|
-
return account.signMessage({ message: { raw: data } })
|
|
128
|
-
}
|
|
129
|
-
if (method === 'eth_signTypedData_v4') {
|
|
130
|
-
if (!account) throw new Error('Not connected')
|
|
131
|
-
const [, typedDataJson] = params as [Address, string]
|
|
132
|
-
const typedData = JSON.parse(typedDataJson)
|
|
133
|
-
return account.signTypedData(typedData)
|
|
134
|
-
}
|
|
135
|
-
if (method === 'eth_sign') {
|
|
136
|
-
if (!account) throw new Error('Not connected')
|
|
137
|
-
const [, data] = params as [Address, Hex]
|
|
138
|
-
return account.sign!({ hash: data })
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Send transaction — sign locally, broadcast via RPC
|
|
142
|
-
if (method === 'eth_sendTransaction') {
|
|
143
|
-
if (!account) throw new Error('Not connected')
|
|
144
|
-
const [tx] = params as [Record<string, string>]
|
|
145
|
-
const walletClient = createWalletClient({
|
|
146
|
-
account,
|
|
147
|
-
chain,
|
|
148
|
-
transport,
|
|
149
|
-
})
|
|
150
|
-
return walletClient.sendTransaction({
|
|
151
|
-
chain,
|
|
152
|
-
to: tx.to as Address,
|
|
153
|
-
data: tx.data as Hex | undefined,
|
|
154
|
-
value: tx.value ? hexToBigInt(tx.value as Hex) : undefined,
|
|
155
|
-
gas: tx.gas ? hexToBigInt(tx.gas as Hex) : undefined,
|
|
156
|
-
nonce:
|
|
157
|
-
tx.nonce != null ? hexToNumber(tx.nonce as Hex) : undefined,
|
|
158
|
-
})
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Chain switching
|
|
162
|
-
if (method === 'wallet_switchEthereumChain') {
|
|
163
|
-
const [{ chainId: hexChainId }] = params as [
|
|
164
|
-
{ chainId: `0x${string}` },
|
|
165
|
-
]
|
|
166
|
-
const newChainId = hexToNumber(hexChainId)
|
|
167
|
-
const chain = config.chains.find((c) => c.id === newChainId)
|
|
168
|
-
if (!chain) throw new Error('Chain not configured')
|
|
169
|
-
currentChainId = newChainId
|
|
170
|
-
config.emitter.emit('change', { chainId: newChainId })
|
|
171
|
-
return null
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Everything else — forward to RPC
|
|
175
|
-
const publicClient = createPublicClient({ chain, transport })
|
|
176
|
-
return (
|
|
177
|
-
publicClient as unknown as {
|
|
178
|
-
request: (args: {
|
|
179
|
-
method: string
|
|
180
|
-
params?: unknown[]
|
|
181
|
-
}) => Promise<unknown>
|
|
182
|
-
}
|
|
183
|
-
).request({ method, params: params as unknown[] })
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return custom({ request })({ retryCount: 0 })
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
async isAuthorized() {
|
|
190
|
-
const acct = account ?? loadAccount()
|
|
191
|
-
return !!acct
|
|
192
|
-
},
|
|
193
|
-
|
|
194
|
-
async switchChain({ chainId }) {
|
|
195
|
-
const chain = config.chains.find((c) => c.id === chainId)
|
|
196
|
-
if (!chain) throw new Error('Chain not configured')
|
|
197
|
-
currentChainId = chainId
|
|
198
|
-
config.emitter.emit('change', { chainId })
|
|
199
|
-
return chain
|
|
200
|
-
},
|
|
201
|
-
|
|
202
|
-
onAccountsChanged(accounts) {
|
|
203
|
-
if (accounts.length === 0) this.onDisconnect()
|
|
204
|
-
else
|
|
205
|
-
config.emitter.emit('change', {
|
|
206
|
-
accounts: accounts.map((a) => getAddress(a)),
|
|
207
|
-
})
|
|
208
|
-
},
|
|
209
|
-
|
|
210
|
-
onChainChanged(chain) {
|
|
211
|
-
const chainId = Number(chain)
|
|
212
|
-
config.emitter.emit('change', { chainId })
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
onDisconnect() {
|
|
216
|
-
config.emitter.emit('disconnect')
|
|
217
|
-
account = null
|
|
218
|
-
},
|
|
219
|
-
}
|
|
220
|
-
})
|
|
221
|
-
}
|
|
1
|
+
export {
|
|
2
|
+
inAppWallet,
|
|
3
|
+
prepareInAppWallet,
|
|
4
|
+
type InAppWalletParameters,
|
|
5
|
+
} from '@1001-digital/wagmi-in-app-wallet'
|