@cantonconnect/core 0.2.0 → 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 +255 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# @cantonconnect/core
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
**Core types, errors, and abstractions for CantonConnect**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@cantonconnect/core)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
`@cantonconnect/core` provides the foundational types, error classes, and abstractions used by the CantonConnect SDK ecosystem. This package is primarily intended for:
|
|
18
|
+
|
|
19
|
+
- **Wallet adapter developers** building custom adapters
|
|
20
|
+
- **SDK contributors** working on the core SDK
|
|
21
|
+
- **Advanced users** who need direct access to types and utilities
|
|
22
|
+
|
|
23
|
+
> **Note**: Most dApp developers should use `@cantonconnect/sdk` instead, which re-exports the necessary types from this package.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @cantonconnect/core
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## What's Included
|
|
36
|
+
|
|
37
|
+
### Type Definitions
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import type {
|
|
41
|
+
// Branded types
|
|
42
|
+
WalletId,
|
|
43
|
+
PartyId,
|
|
44
|
+
SessionId,
|
|
45
|
+
NetworkId,
|
|
46
|
+
CapabilityKey,
|
|
47
|
+
Signature,
|
|
48
|
+
TransactionHash,
|
|
49
|
+
|
|
50
|
+
// Core types
|
|
51
|
+
WalletInfo,
|
|
52
|
+
Session,
|
|
53
|
+
SignedMessage,
|
|
54
|
+
SignedTransaction,
|
|
55
|
+
TxReceipt,
|
|
56
|
+
TransactionStatus,
|
|
57
|
+
|
|
58
|
+
// Adapter types
|
|
59
|
+
WalletAdapter,
|
|
60
|
+
AdapterContext,
|
|
61
|
+
AdapterDetectResult,
|
|
62
|
+
AdapterConnectResult,
|
|
63
|
+
SignMessageParams,
|
|
64
|
+
SignTransactionParams,
|
|
65
|
+
SubmitTransactionParams,
|
|
66
|
+
|
|
67
|
+
// Service adapters
|
|
68
|
+
StorageAdapter,
|
|
69
|
+
CryptoAdapter,
|
|
70
|
+
LoggerAdapter,
|
|
71
|
+
TelemetryAdapter,
|
|
72
|
+
} from '@cantonconnect/core';
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Error Classes
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import {
|
|
79
|
+
CantonConnectError,
|
|
80
|
+
WalletNotFoundError,
|
|
81
|
+
WalletNotInstalledError,
|
|
82
|
+
UserRejectedError,
|
|
83
|
+
OriginNotAllowedError,
|
|
84
|
+
SessionExpiredError,
|
|
85
|
+
CapabilityNotSupportedError,
|
|
86
|
+
TransportError,
|
|
87
|
+
RegistryFetchFailedError,
|
|
88
|
+
RegistryVerificationFailedError,
|
|
89
|
+
RegistrySchemaInvalidError,
|
|
90
|
+
InternalError,
|
|
91
|
+
TimeoutError,
|
|
92
|
+
} from '@cantonconnect/core';
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Transport Classes
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import {
|
|
99
|
+
PostMessageTransport,
|
|
100
|
+
DeepLinkTransport,
|
|
101
|
+
MockTransport,
|
|
102
|
+
} from '@cantonconnect/core';
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Utilities
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import {
|
|
109
|
+
// Type constructors
|
|
110
|
+
toWalletId,
|
|
111
|
+
toPartyId,
|
|
112
|
+
toSessionId,
|
|
113
|
+
toNetworkId,
|
|
114
|
+
toSignature,
|
|
115
|
+
toTransactionHash,
|
|
116
|
+
|
|
117
|
+
// Guards
|
|
118
|
+
capabilityGuard,
|
|
119
|
+
installGuard,
|
|
120
|
+
|
|
121
|
+
// Error mapping
|
|
122
|
+
mapUnknownErrorToCantonConnectError,
|
|
123
|
+
} from '@cantonconnect/core';
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Building a Wallet Adapter
|
|
129
|
+
|
|
130
|
+
If you're building a custom wallet adapter, implement the `WalletAdapter` interface:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import type {
|
|
134
|
+
WalletAdapter,
|
|
135
|
+
AdapterContext,
|
|
136
|
+
AdapterDetectResult,
|
|
137
|
+
AdapterConnectResult,
|
|
138
|
+
Session,
|
|
139
|
+
SignedMessage,
|
|
140
|
+
SignedTransaction,
|
|
141
|
+
SignMessageParams,
|
|
142
|
+
SignTransactionParams,
|
|
143
|
+
} from '@cantonconnect/core';
|
|
144
|
+
import { toWalletId, toSignature } from '@cantonconnect/core';
|
|
145
|
+
|
|
146
|
+
export class MyWalletAdapter implements WalletAdapter {
|
|
147
|
+
readonly walletId = toWalletId('my-wallet');
|
|
148
|
+
readonly name = 'My Wallet';
|
|
149
|
+
|
|
150
|
+
getCapabilities() {
|
|
151
|
+
return ['signMessage', 'signTransaction'] as const;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async detectInstalled(): Promise<AdapterDetectResult> {
|
|
155
|
+
const installed = typeof window !== 'undefined' &&
|
|
156
|
+
window.myWallet !== undefined;
|
|
157
|
+
return { installed };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async connect(
|
|
161
|
+
ctx: AdapterContext,
|
|
162
|
+
options: { timeoutMs: number }
|
|
163
|
+
): Promise<AdapterConnectResult> {
|
|
164
|
+
// Implement connection logic
|
|
165
|
+
const result = await window.myWallet.connect();
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
partyId: toPartyId(result.partyId),
|
|
169
|
+
capabilities: this.getCapabilities(),
|
|
170
|
+
session: {
|
|
171
|
+
expiresAt: Date.now() + 24 * 60 * 60 * 1000, // 24 hours
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async disconnect(ctx: AdapterContext, session: Session): Promise<void> {
|
|
177
|
+
await window.myWallet.disconnect();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async signMessage(
|
|
181
|
+
ctx: AdapterContext,
|
|
182
|
+
session: Session,
|
|
183
|
+
params: SignMessageParams
|
|
184
|
+
): Promise<SignedMessage> {
|
|
185
|
+
const sig = await window.myWallet.signMessage(params.message);
|
|
186
|
+
return {
|
|
187
|
+
message: params.message,
|
|
188
|
+
signature: toSignature(sig),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Implement other methods...
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## Error Handling
|
|
199
|
+
|
|
200
|
+
All errors extend `CantonConnectError` and include an error code:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import {
|
|
204
|
+
CantonConnectError,
|
|
205
|
+
WalletNotInstalledError
|
|
206
|
+
} from '@cantonconnect/core';
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
// ... wallet operation
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (error instanceof CantonConnectError) {
|
|
212
|
+
console.log('Error code:', error.code);
|
|
213
|
+
console.log('Message:', error.message);
|
|
214
|
+
console.log('Cause:', error.cause);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Error Codes
|
|
220
|
+
|
|
221
|
+
| Error Class | Code |
|
|
222
|
+
|-------------|------|
|
|
223
|
+
| `WalletNotFoundError` | `WALLET_NOT_FOUND` |
|
|
224
|
+
| `WalletNotInstalledError` | `WALLET_NOT_INSTALLED` |
|
|
225
|
+
| `UserRejectedError` | `USER_REJECTED` |
|
|
226
|
+
| `OriginNotAllowedError` | `ORIGIN_NOT_ALLOWED` |
|
|
227
|
+
| `SessionExpiredError` | `SESSION_EXPIRED` |
|
|
228
|
+
| `CapabilityNotSupportedError` | `CAPABILITY_NOT_SUPPORTED` |
|
|
229
|
+
| `TransportError` | `TRANSPORT_ERROR` |
|
|
230
|
+
| `TimeoutError` | `TIMEOUT` |
|
|
231
|
+
| `InternalError` | `INTERNAL_ERROR` |
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## Related Packages
|
|
236
|
+
|
|
237
|
+
| Package | Description |
|
|
238
|
+
|---------|-------------|
|
|
239
|
+
| [@cantonconnect/sdk](https://www.npmjs.com/package/@cantonconnect/sdk) | Main SDK for dApps |
|
|
240
|
+
| [@cantonconnect/react](https://www.npmjs.com/package/@cantonconnect/react) | React integration |
|
|
241
|
+
| [@cantonconnect/adapter-starter](https://www.npmjs.com/package/@cantonconnect/adapter-starter) | Template for new adapters |
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Links
|
|
246
|
+
|
|
247
|
+
- [GitHub Repository](https://github.com/cantonconnect/wallet-sdk)
|
|
248
|
+
- [Wallet Provider Guide](https://github.com/cantonconnect/wallet-sdk/blob/main/docs/wallet-provider-guide.md)
|
|
249
|
+
- [Report Issues](https://github.com/cantonconnect/wallet-sdk/issues)
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|