@coinbase/cdp-core 0.0.4 → 0.0.5

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 CHANGED
@@ -200,4 +200,56 @@ const { privateKey } = await exportEvmAccount({
200
200
 
201
201
  // WARNING: Handle private keys with extreme care!
202
202
  console.log("Private Key:", privateKey);
203
+ ```
204
+
205
+ ### EIP-1193 Provider
206
+
207
+ The core package includes an EIP-1193 compatible provider. This provider can be used to sign and send transactions.
208
+
209
+ The provider is created by calling `createCDPEmbeddedWallet`, which exposes a `.provider` attribute. `createCDPEmbeddedWallet` must be called with the desired chains to support as well as the transports for these chains.
210
+
211
+ The provider will initially connect to the first chain in the `chains` array. The transports are typically HTTP RPC endpoints, which are used internally for broadcasting non-Base transactions. For more information on transports, see [Wagmi's `createConfig` setup](https://wagmi.sh/react/api/createConfig).
212
+ ```typescript
213
+ import { base, mainnet } from "viem/chains";
214
+ import { http } from "viem"
215
+
216
+ // Basic usage with default configuration
217
+ const wallet = createCDPEmbeddedWallet({
218
+ chains:[base, mainnet],
219
+ transports: {
220
+ [base.id]: http(),
221
+ [mainnet.id]: http()
222
+ }
223
+ });
224
+ const provider = wallet.provider;
225
+
226
+ // Request account access
227
+ const accounts = await provider.request({
228
+ method: "eth_requestAccounts"
229
+ });
230
+
231
+ // Sign a message
232
+ const signature = await provider.request({
233
+ method: "personal_sign",
234
+ params: ["Hello, World!", accounts[0]]
235
+ });
236
+
237
+ // Send a transaction
238
+ const txHash = await provider.request({
239
+ method: "eth_sendTransaction",
240
+ params: [{
241
+ from: accounts[0],
242
+ to: "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
243
+ value: "0x1000000000000000000"
244
+ }]
245
+ });
246
+
247
+ // Listen for connection events
248
+ provider.on("connect", (connectInfo) => {
249
+ console.log("Connected to chain:", connectInfo.chainId);
250
+ });
251
+
252
+ provider.on("disconnect", () => {
253
+ console.log("Disconnected from wallet");
254
+ });
203
255
  ```
@@ -1,11 +1,18 @@
1
- import { SendEvmTransactionWithEndUserAccountBodyNetwork as s } from "@coinbase/cdp-api-client";
2
- const n = (r) => !r || typeof r != "object" ? r : Array.isArray(r) ? r.map(n) : Object.keys(r).sort().reduce(
3
- (e, t) => (e[t] = n(r[t]), e),
1
+ import { SendEvmTransactionWithEndUserAccountBodyNetwork as i } from "@coinbase/cdp-api-client";
2
+ const n = (e) => !e || typeof e != "object" ? e : Array.isArray(e) ? e.map(n) : Object.keys(e).sort().reduce(
3
+ (r, t) => (r[t] = n(e[t]), r),
4
4
  {}
5
- ), i = (r) => Object.values(s).includes(
6
- r
7
- );
5
+ ), o = (e) => Object.values(i).includes(
6
+ e
7
+ ), s = {
8
+ "base-sepolia": 84532,
9
+ base: 8453
10
+ }, c = Object.fromEntries(
11
+ Object.entries(s).map(([e, r]) => [r, e])
12
+ ), p = (e) => Object.values(s).includes(e);
8
13
  export {
9
- i as isChainSupportedForCDPSends,
14
+ c as chainIdToNameMapping,
15
+ p as isChainIdSupportedForCDPSends,
16
+ o as isChainSupportedForCDPSends,
10
17
  n as sortKeys
11
18
  };
@@ -1,107 +1,149 @@
1
- import { Provider as d, Hex as u } from "ox";
2
- import { signEvmTypedData as m, signEvmHash as h, getCurrentUser as c, signOut as g, sendEvmTransaction as l, signEvmMessage as v } from "./index2.js";
1
+ import { Provider as y, Hex as I } from "ox";
2
+ import { createWalletClient as E } from "viem";
3
+ import { signEvmTypedData as S, signEvmHash as T, getCurrentUser as f, signOut as P, sendEvmTransaction as R, signEvmTransaction as A, signEvmMessage as x } from "./index2.js";
3
4
  import "./index3.js";
4
- import { validateUserOwnsAddress as o, EIP1193ProviderError as s, STANDARD_ERROR_CODES as i, validateUserHasEvmAccount as f, validateUserConnected as w } from "./index4.js";
5
- import { baseSepolia as p } from "./index17.js";
6
- async function P() {
7
- return (await c())?.evmAccounts || [];
5
+ import { validateUserOwnsAddress as m, EIP1193ProviderError as l, STANDARD_ERROR_CODES as s, RPCRequestError as c, validateUserHasEvmAccount as C, validateUserConnected as D } from "./index4.js";
6
+ import { isChainIdSupportedForCDPSends as b, chainIdToNameMapping as j } from "./index12.js";
7
+ function g(e) {
8
+ return {
9
+ to: e.to,
10
+ data: e.data,
11
+ value: e.value ? BigInt(e.value) : void 0,
12
+ nonce: e.nonce ? Number(e.nonce) : void 0,
13
+ gas: e.gas ? BigInt(e.gas) : void 0,
14
+ maxFeePerGas: e.maxFeePerGas ? BigInt(e.maxFeePerGas) : void 0,
15
+ maxPriorityFeePerGas: e.maxPriorityFeePerGas ? BigInt(e.maxPriorityFeePerGas) : void 0,
16
+ type: "eip1559"
17
+ };
8
18
  }
9
- async function R(t, e) {
10
- const a = await w();
11
- return t.emit("connect", {
12
- chainId: u.fromNumber(e.getState().chainId)
13
- }), a.evmAccounts;
19
+ async function U() {
20
+ return (await f())?.evmAccounts || [];
14
21
  }
15
- async function D(t) {
16
- const [e, a] = t;
17
- await o(a);
22
+ async function M(e, a) {
23
+ const n = await D();
24
+ return e.emit("connect", {
25
+ chainId: I.fromNumber(a.getState().chainId)
26
+ }), n.evmAccounts;
27
+ }
28
+ async function _(e) {
29
+ const [a, n] = e;
30
+ await m(n);
31
+ const t = Buffer.from(a.slice(2), "hex").toString();
18
32
  try {
19
- return (await v({
20
- evmAccount: a,
21
- message: e
33
+ return (await x({
34
+ evmAccount: n,
35
+ message: t
22
36
  })).signature;
23
- } catch (n) {
24
- throw new s(
25
- i.provider.userRejectedRequest,
26
- n instanceof Error ? n.message : "Signing failed"
37
+ } catch (o) {
38
+ throw new l(
39
+ s.provider.userRejectedRequest,
40
+ o instanceof Error ? o.message : "Signing failed"
27
41
  );
28
42
  }
29
43
  }
30
- async function x(t) {
31
- const [e, a] = t;
32
- await o(a);
44
+ async function k(e) {
45
+ const [a, n] = e;
46
+ await m(n);
33
47
  try {
34
- const n = JSON.parse(e);
35
- return (await m({
36
- evmAccount: a,
37
- typedData: n
48
+ const t = JSON.parse(a);
49
+ return (await S({
50
+ evmAccount: n,
51
+ typedData: t
38
52
  })).signature;
39
- } catch (n) {
40
- throw new s(
41
- i.provider.userRejectedRequest,
42
- n instanceof Error ? n.message : "Signing failed"
53
+ } catch (t) {
54
+ throw new l(
55
+ s.provider.userRejectedRequest,
56
+ t instanceof Error ? t.message : "Signing failed"
43
57
  );
44
58
  }
45
59
  }
46
- async function T(t) {
47
- const [e] = t, n = (await f()).evmAccounts[0];
48
- try {
49
- return (await l({
50
- evmAccount: n,
51
- transaction: {
52
- to: e.to,
53
- value: BigInt(e.value),
54
- data: e.data,
55
- // Include data field for contract interactions
56
- nonce: e.nonce ? Number(e.nonce) : void 0,
57
- gas: e.gas ? BigInt(e.gas) : void 0,
58
- maxFeePerGas: e.maxFeePerGas ? BigInt(e.maxFeePerGas) : void 0,
59
- maxPriorityFeePerGas: e.maxPriorityFeePerGas ? BigInt(e.maxPriorityFeePerGas) : void 0,
60
- chainId: e.chainId ? Number(e.chainId) : p.id,
61
- type: "eip1559"
62
- },
63
- network: "base-sepolia"
64
- })).transactionHash;
65
- } catch (r) {
66
- throw console.log("Transaction failed", r), new s(
67
- i.provider.userRejectedRequest,
68
- r instanceof Error ? r.message : "Transaction failed"
60
+ async function z(e, a, n) {
61
+ const [t] = e;
62
+ if (!t || typeof t != "object")
63
+ throw new c(
64
+ s.rpc.invalidParams,
65
+ "Transaction parameter must be an object"
69
66
  );
70
- }
67
+ if (!t.to)
68
+ throw new c(
69
+ s.rpc.invalidParams,
70
+ "Transaction must include 'to' field"
71
+ );
72
+ const d = (await C()).evmAccounts[0], u = a.getState(), i = t.chainId ? Number(t.chainId) : u.chainId;
73
+ if (b(i))
74
+ try {
75
+ return (await R({
76
+ evmAccount: d,
77
+ transaction: { ...g(t), chainId: i },
78
+ network: j[i]
79
+ })).transactionHash;
80
+ } catch (r) {
81
+ throw console.log("Transaction failed", r), new c(
82
+ s.rpc.transactionRejected,
83
+ r instanceof Error ? r.message : "Transaction failed"
84
+ );
85
+ }
86
+ else
87
+ try {
88
+ const r = E({
89
+ // Safe as we check before calling this handler that chainId is configured
90
+ chain: u.chains.find((h) => h.id === i),
91
+ transport: n[i],
92
+ account: d
93
+ }), p = u.chains.find((h) => h.id === i), w = await r.prepareTransactionRequest({
94
+ ...g(t),
95
+ chain: p
96
+ }), v = await A({
97
+ evmAccount: d,
98
+ transaction: {
99
+ ...w,
100
+ type: "eip1559"
101
+ }
102
+ });
103
+ return await r.sendRawTransaction({
104
+ serializedTransaction: v.signedTransaction
105
+ });
106
+ } catch (r) {
107
+ throw new c(
108
+ s.rpc.transactionRejected,
109
+ r instanceof Error ? r.message : "Transaction failed"
110
+ );
111
+ }
71
112
  }
72
- async function b(t) {
73
- const [e, a] = t;
74
- await o(a);
113
+ async function J(e) {
114
+ const [a, n] = e;
115
+ await m(n);
75
116
  try {
76
- return (await h({
77
- evmAccount: a,
78
- hash: e
117
+ return (await T({
118
+ evmAccount: n,
119
+ hash: a
79
120
  })).signature;
80
- } catch (n) {
81
- throw new s(
82
- i.provider.userRejectedRequest,
83
- n instanceof Error ? n.message : "Signing failed"
121
+ } catch (t) {
122
+ throw new l(
123
+ s.provider.userRejectedRequest,
124
+ t instanceof Error ? t.message : "Signing failed"
84
125
  );
85
126
  }
86
127
  }
87
- async function C(t) {
88
- return `0x${t.getState().chainId.toString(16)}`;
128
+ async function W(e) {
129
+ return `0x${e.getState().chainId.toString(16)}`;
89
130
  }
90
- async function F(t) {
91
- await c() && await g(), t.emit("disconnect", new d.DisconnectedError());
131
+ async function $(e) {
132
+ await f() && await P(), e.emit("disconnect", new y.DisconnectedError());
92
133
  }
93
- function G(t, e) {
94
- const a = Number.parseInt(t[0].chainId, 16);
95
- e.getState().setChainId(a);
134
+ function K(e, a) {
135
+ const n = Number.parseInt(e[0].chainId, 16);
136
+ a.getState().setChainId(n);
96
137
  }
97
138
  export {
98
- P as handleAccounts,
99
- C as handleChainId,
100
- F as handleDisconnect,
101
- b as handleEthSign,
102
- D as handlePersonalSign,
103
- R as handleRequestAccounts,
104
- T as handleSendTransaction,
105
- x as handleSignTypedData,
106
- G as handleSwitchEthereumChain
139
+ U as handleAccounts,
140
+ W as handleChainId,
141
+ $ as handleDisconnect,
142
+ J as handleEthSign,
143
+ _ as handlePersonalSign,
144
+ M as handleRequestAccounts,
145
+ z as handleSendTransaction,
146
+ k as handleSignTypedData,
147
+ K as handleSwitchEthereumChain,
148
+ g as shimProviderTxRequest
107
149
  };
@@ -1,6 +1,6 @@
1
1
  import { chainConfig as a } from "./index77.js";
2
- import { defineChain as s } from "./index22.js";
3
- const e = 11155111, o = /* @__PURE__ */ s({
2
+ import { defineChain as t } from "./index22.js";
3
+ const e = 11155111, r = /* @__PURE__ */ t({
4
4
  ...a,
5
5
  id: 84532,
6
6
  network: "base-sepolia",
@@ -50,6 +50,9 @@ const e = 11155111, o = /* @__PURE__ */ s({
50
50
  testnet: !0,
51
51
  sourceId: e
52
52
  });
53
+ ({
54
+ ...r
55
+ });
53
56
  export {
54
- o as baseSepolia
57
+ r as baseSepolia
55
58
  };
@@ -1,91 +1,92 @@
1
- import { Provider as u } from "ox";
2
- import { http as m } from "viem";
3
- import { create as l } from "zustand";
4
- import { subscribeWithSelector as p, persist as f } from "./index14.js";
5
- import { validateRequest as g, EIP1193ProviderError as I, STANDARD_ERROR_CODES as S } from "./index4.js";
6
- import { EventEmitter as w } from "./index15.js";
7
- import { handleChainId as b, handleSignTypedData as A, handleEthSign as _, handleSwitchEthereumChain as C, handleDisconnect as v, handleSendTransaction as D, handlePersonalSign as P, handleRequestAccounts as R, handleAccounts as E } from "./index16.js";
8
- import { onAuthStateChange as T } from "./index2.js";
1
+ import { Provider as m } from "ox";
2
+ import { http as l } from "viem";
3
+ import { create as p } from "zustand";
4
+ import { subscribeWithSelector as f, persist as g } from "./index14.js";
5
+ import { validateRequest as I, EIP1193ProviderError as S, STANDARD_ERROR_CODES as w } from "./index4.js";
6
+ import { EventEmitter as b } from "./index15.js";
7
+ import { handleChainId as A, handleSignTypedData as C, handleEthSign as _, handleSwitchEthereumChain as v, handleDisconnect as D, handleSendTransaction as P, handlePersonalSign as R, handleRequestAccounts as E, handleAccounts as T } from "./index16.js";
8
+ import { onAuthStateChange as q } from "./index2.js";
9
9
  import { baseSepolia as h } from "./index17.js";
10
- function q(t, r, c) {
10
+ function y(e, r, a) {
11
11
  r.subscribe(
12
- (e) => e.chainId,
13
- (e, n) => {
14
- e !== n && t.emit("chainChanged", e);
12
+ (n) => n.chainId,
13
+ (n, t) => {
14
+ n !== t && e.emit("chainChanged", n);
15
15
  }
16
16
  ), r.subscribe(
17
- (e) => e.user,
18
- (e, n) => {
19
- if (!(e?.userId === n?.userId && e?.evmAccounts?.length === n?.evmAccounts?.length)) {
20
- if (!e && n) {
21
- t.emit("disconnect");
17
+ (n) => n.user,
18
+ (n, t) => {
19
+ if (!(n?.userId === t?.userId && n?.evmAccounts?.length === t?.evmAccounts?.length)) {
20
+ if (!n && t) {
21
+ e.emit("disconnect");
22
22
  return;
23
23
  }
24
- if (e && e.evmAccounts?.length && (!n || n.evmAccounts?.length === 0)) {
25
- const d = r.getState().chainId;
26
- t.emit("connect", { chainId: d });
24
+ if (n && n.evmAccounts?.length && (!t || t.evmAccounts?.length === 0)) {
25
+ const u = r.getState().chainId;
26
+ e.emit("connect", { chainId: u });
27
27
  return;
28
28
  }
29
29
  }
30
30
  }
31
31
  );
32
- const { chainId: o, setChainId: s } = r.getState(), a = o || c.chains[0].id;
33
- return o || s(a), u.from({
34
- request: async (e) => {
35
- g(e, c);
36
- const n = e;
37
- switch (n.method) {
32
+ const { chainId: s, setChainId: i } = r.getState(), c = s || a.chains[0].id;
33
+ return s || i(c), m.from({
34
+ request: async (n) => {
35
+ I(n, a);
36
+ const t = n;
37
+ switch (t.method) {
38
38
  case "eth_accounts":
39
- return E();
39
+ return T();
40
40
  case "eth_requestAccounts":
41
- return R(t, r);
41
+ return E(e, r);
42
42
  case "personal_sign":
43
- return P(n.params);
43
+ return R(t.params);
44
44
  case "eth_sendTransaction":
45
- return D(n.params);
45
+ return P(t.params, r, a.transports);
46
46
  case "wallet_disconnect":
47
- return v(t);
47
+ return D(e);
48
48
  case "wallet_switchEthereumChain":
49
- return C(n.params, r);
49
+ return v(t.params, r);
50
50
  case "eth_sign":
51
- return _(n.params);
51
+ return _(t.params);
52
52
  case "eth_signTypedData_v4":
53
- return A(n.params);
53
+ return C(t.params);
54
54
  case "eth_chainId":
55
- return b(r);
55
+ return A(r);
56
56
  // Catch-all for any unknown method
57
57
  default:
58
- throw new I(
59
- S.provider.unsupportedMethod,
60
- `Method ${n.method} not supported`
58
+ throw new S(
59
+ w.provider.unsupportedMethod,
60
+ `Method ${t.method} not supported`
61
61
  );
62
62
  }
63
63
  },
64
64
  // Event emitter methods - only the ones required by the interface
65
- on: t.on.bind(t),
66
- removeListener: t.removeListener.bind(t)
65
+ on: e.on.bind(e),
66
+ removeListener: e.removeListener.bind(e)
67
67
  });
68
68
  }
69
- const y = {
69
+ const d = {
70
70
  chains: [h],
71
71
  transports: {
72
- [h.id]: m()
72
+ [h.id]: l()
73
73
  }
74
74
  };
75
- function z(t) {
76
- if (t?.chains.length === 0)
75
+ function z(e) {
76
+ if (e?.chains.length === 0)
77
77
  throw new Error("At least one chain must be configured");
78
- const r = t?.chains ?? y.chains;
78
+ const r = e?.chains ?? d.chains, a = e?.transports ?? d.transports;
79
79
  if (r.length === 0)
80
80
  throw new Error("No chains found in configuration");
81
- const c = new w(), o = l()(
82
- p(
83
- f(
84
- (a) => ({
81
+ const s = new b(), i = p()(
82
+ f(
83
+ g(
84
+ (o) => ({
85
85
  chainId: r[0].id,
86
- setChainId: (i) => a({ chainId: i }),
86
+ setChainId: (n) => o({ chainId: n }),
87
+ chains: r,
87
88
  user: null,
88
- setUser: (i) => a({ user: i })
89
+ setUser: (n) => o({ user: n })
89
90
  }),
90
91
  {
91
92
  name: "cdp-provider-store"
@@ -94,11 +95,12 @@ function z(t) {
94
95
  )
95
96
  )
96
97
  );
97
- return T((a) => {
98
- o.getState().setUser(a);
98
+ return q((o) => {
99
+ i.getState().setUser(o);
99
100
  }), {
100
- provider: q(c, o, {
101
- chains: r
101
+ provider: y(s, i, {
102
+ chains: r,
103
+ transports: a
102
104
  })
103
105
  };
104
106
  }
@@ -117,7 +117,7 @@ async function w() {
117
117
  }
118
118
  export {
119
119
  o as EIP1193ProviderError,
120
- a as RPCInvalidParamsError,
120
+ a as RPCRequestError,
121
121
  n as STANDARD_ERROR_CODES,
122
122
  l as validateRequest,
123
123
  d as validateUserConnected,
@@ -1,4 +1,4 @@
1
- const o = "2.31.3";
1
+ const o = "2.33.0";
2
2
  export {
3
3
  o as version
4
4
  };
@@ -114,6 +114,7 @@ export declare type ProviderRequest = AccountsRequest | RequestAccountsRequest |
114
114
  export declare type ProviderState = {
115
115
  chainId: number;
116
116
  setChainId: (chainId: number) => void;
117
+ chains: readonly [Chain_2, ...Chain_2[]];
117
118
  user: User | null;
118
119
  setUser: (user: User | null) => void;
119
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coinbase/cdp-core",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**",
@@ -15,9 +15,9 @@
15
15
  "dependencies": {
16
16
  "jose": "^6.0.11",
17
17
  "ox": "0.8.1",
18
- "viem": "2.31.3",
18
+ "viem": "^2.33.0",
19
19
  "zustand": "^5.0.6",
20
- "@coinbase/cdp-api-client": "^0.0.4"
20
+ "@coinbase/cdp-api-client": "^0.0.5"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@size-limit/preset-big-lib": "^11.2.0",
@@ -30,7 +30,7 @@
30
30
  "name": "full-package",
31
31
  "path": "./dist/esm/index.js",
32
32
  "import": "*",
33
- "limit": "40 KB"
33
+ "limit": "60 KB"
34
34
  },
35
35
  {
36
36
  "name": "auth-only",
@@ -54,7 +54,7 @@
54
54
  "name": "provider-only",
55
55
  "path": "./dist/esm/index.js",
56
56
  "import": "{ createCDPEmbeddedWallet }",
57
- "limit": "35 KB"
57
+ "limit": "55 KB"
58
58
  }
59
59
  ],
60
60
  "scripts": {