@0xkey-io/wallet-stamper 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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.md +232 -0
- package/dist/_kernel/stamping.d.ts +6 -0
- package/dist/_kernel/stamping.d.ts.map +1 -0
- package/dist/_kernel/stamping.js +10 -0
- package/dist/_kernel/stamping.js.map +1 -0
- package/dist/_kernel/stamping.mjs +7 -0
- package/dist/_kernel/stamping.mjs.map +1 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -0
- package/dist/constants.mjs +5 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/errors.mjs +10 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/ethereum.d.ts +71 -0
- package/dist/ethereum.d.ts.map +1 -0
- package/dist/ethereum.js +107 -0
- package/dist/ethereum.js.map +1 -0
- package/dist/ethereum.mjs +103 -0
- package/dist/ethereum.mjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/dist/stamper.d.ts +7 -0
- package/dist/stamper.d.ts.map +1 -0
- package/dist/stamper.js +64 -0
- package/dist/stamper.js.map +1 -0
- package/dist/stamper.mjs +62 -0
- package/dist/stamper.mjs.map +1 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/types.js.map +1 -0
- package/dist/types.mjs +11 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +66 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 0xkey
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# ZeroXKey Wallet Stamper
|
|
2
|
+
|
|
3
|
+
The `@0xkey-io/wallet-stamper` package provides a mechanism to stamp requests using a wallet public key and signature. This package supports both Solana and Ethereum wallets, allowing for seamless integration with various blockchain applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Before you start using the ZeroXKey Wallet Stamper, make sure to install the necessary packages in your project. This guide assumes you have a Node.js environment ready for development.
|
|
8
|
+
|
|
9
|
+
Install the required packages using NPM, Yarn, or PNPM:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @0xkey-io/wallet-stamper @0xkey-io/http
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Prerequisites
|
|
18
|
+
|
|
19
|
+
#### Add Wallet Public Key as Authenticator
|
|
20
|
+
|
|
21
|
+
To use the wallet stamper, you must add the user's wallet public key as an API key authenticator.
|
|
22
|
+
This can be achieved either by creating a sub-organization activity or by generating a new API key and passing in the wallet's public key.
|
|
23
|
+
|
|
24
|
+
The API key stamper is necessary because we need to use the parent organization's API key to add the authenticator initially.
|
|
25
|
+
Once this is done, the wallet can stamp activity requests independently without needing the parent organization's API private and public keys.
|
|
26
|
+
|
|
27
|
+
Below are the steps to add a Solana public key as a wallet authenticator:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
const apiKeyStamper = new ApiKeyStamper({
|
|
31
|
+
apiPublicKey: process.env.API_PUBLIC_KEY ?? "",
|
|
32
|
+
apiPrivateKey: process.env.API_PRIVATE_KEY ?? "",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const client = new ZeroXKeyClient({ baseUrl: BASE_URL }, apiKeyStamper);
|
|
36
|
+
|
|
37
|
+
const activityPoller = createActivityPoller({
|
|
38
|
+
client,
|
|
39
|
+
requestFn: client.createApiKeys,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// See "Example: Signing with a Solana Wallet" below for how to implement the SolanaWallet interface
|
|
43
|
+
const mockWallet = new MockSolanaWallet();
|
|
44
|
+
|
|
45
|
+
// This is the public key of the wallet that will be added as an API key and used to stamp future requests
|
|
46
|
+
const publicKey = mockWallet.recoverPublicKey();
|
|
47
|
+
|
|
48
|
+
// The userId of the user that we will add the wallet public key as an authenticator
|
|
49
|
+
const userId = "f4a5e6b4-3b9c-4f69-b7f6-9c2f456a4d23";
|
|
50
|
+
|
|
51
|
+
// We set the curve type to 'API_KEY_CURVE_ED25519' for solana wallets
|
|
52
|
+
// If using an Ethereum wallet, set the curve type to 'API_KEY_CURVE_SECP256K1'
|
|
53
|
+
const curveType = "API_KEY_CURVE_ED25519";
|
|
54
|
+
|
|
55
|
+
const result = activityPoller({
|
|
56
|
+
type: "ACTIVITY_TYPE_CREATE_API_KEYS_V2",
|
|
57
|
+
timestampMs: new Date().getTime().toString(),
|
|
58
|
+
organizationId: "acd0bc97-2af5-475b-bc34-0fa7ca3bdc75",
|
|
59
|
+
parameters: {
|
|
60
|
+
apiKeys: [
|
|
61
|
+
{
|
|
62
|
+
apiKeyName: "solana-wallet",
|
|
63
|
+
publicKey,
|
|
64
|
+
curveType,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
userId,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### Using the Wallet Stamper
|
|
73
|
+
|
|
74
|
+
To use the `@0xkey-io/wallet-stamper` package, follow these steps:
|
|
75
|
+
|
|
76
|
+
1. **Choose Your Wallet Type**: Decide whether you will use an EVM-based wallet (e.g., Ethereum) or a Solana-based wallet.
|
|
77
|
+
|
|
78
|
+
2. **Implement the Wallet Interface**: Depending on your chosen blockchain, implement the wallet interface. This involves creating methods to sign messages and recover public keys.
|
|
79
|
+
|
|
80
|
+
> Note: We've provided a default implementation for Ethereum wallets via the `EthereumWallet` class. For custom implementations, you may implement the `WalletInterface` yourself.
|
|
81
|
+
|
|
82
|
+
3. **Instantiate the WalletStamper**: Create an instance of the `WalletStamper` using the wallet interface.
|
|
83
|
+
|
|
84
|
+
4. **Instantiate the ZeroXKeyClient**: Create an instance of the `ZeroXKeyClient` with the `WalletStamper` instance.
|
|
85
|
+
|
|
86
|
+
5. **Stamp Requests**: Now when making request using the `ZeroXKeyClient`, the wallet stamper will automatically stamp the request with the user's wallet public key and signature.
|
|
87
|
+
|
|
88
|
+
### Example: Signing with a Solana Wallet
|
|
89
|
+
|
|
90
|
+
In this example, we are using a local Solana wallet.
|
|
91
|
+
For information on using an injected Solana wallet such as Solflare, please refer to the [`with-wallet-stamper`](../../examples/with-wallet-stamper) example.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { Keypair } from "@solana/web3.js";
|
|
95
|
+
import { decodeUTF8 } from "tweetnacl-util";
|
|
96
|
+
import nacl from "tweetnacl";
|
|
97
|
+
import { ZeroXKeyClient } from "@0xkey-io/http";
|
|
98
|
+
import { WalletStamper, SolanaWalletInterface } from "@0xkey-io/wallet-stamper";
|
|
99
|
+
|
|
100
|
+
class SolanaWallet implements SolanaWalletInterface {
|
|
101
|
+
keypair = Keypair.fromSecretKey(SOLANA_PRIVATE_KEY);
|
|
102
|
+
type = "solana" as const;
|
|
103
|
+
|
|
104
|
+
async signMessage(message: string): Promise<string> {
|
|
105
|
+
const messageBytes = decodeUTF8(message);
|
|
106
|
+
const signature = nacl.sign.detached(messageBytes, this.keypair.secretKey);
|
|
107
|
+
return Buffer.from(signature).toString("hex");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async getPublicKey(): Promise<string> {
|
|
111
|
+
// Convert the base24 encoded Solana wallet public key (the one displayed in the wallet)
|
|
112
|
+
// into the ed25519 decoded public key
|
|
113
|
+
const ed25519PublicKey = Buffer.from(
|
|
114
|
+
this.keypair.publicKey.toBuffer(),
|
|
115
|
+
).toString("hex");
|
|
116
|
+
return ed25519PublicKey;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Instantiate the WalletStamper with the SolanaWallet
|
|
121
|
+
const walletStamper = new WalletStamper(new SolanaWallet());
|
|
122
|
+
|
|
123
|
+
// Instantiate the ZeroXKeyClient with the WalletStamper
|
|
124
|
+
const client = new ZeroXKeyClient({ baseUrl: BASE_URL }, walletStamper);
|
|
125
|
+
|
|
126
|
+
// Call getWhoami to get the sub org's organizationId and userId passing in the parent org id
|
|
127
|
+
// whoami { organizationId: string; organizationName: string; userId: string; username: string; }
|
|
128
|
+
const whoami = await client.getWhoami({
|
|
129
|
+
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Now that we have the sub organization id, we can make requests using that sub org id
|
|
133
|
+
|
|
134
|
+
// Get the wallets for this sub organization
|
|
135
|
+
const wallets = await client.getWallets({
|
|
136
|
+
organizationId: whoami.organizationId,
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Example: Signing with an Ethereum Wallet
|
|
141
|
+
|
|
142
|
+
The main distinction between signing with an Ethereum Wallet and a Solana Wallet lies in how the public key is obtained.
|
|
143
|
+
For Solana, the public key can be directly derived from the wallet.
|
|
144
|
+
In contrast, with Ethereum, the secp256k1 public key isn't directly accessible.
|
|
145
|
+
Instead, you need to first obtain a signature from the user and then recover the public key from that signature.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import {
|
|
149
|
+
createWalletClient,
|
|
150
|
+
custom,
|
|
151
|
+
recoverPublicKey,
|
|
152
|
+
hashMessage,
|
|
153
|
+
} from "viem";
|
|
154
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
155
|
+
import { mainnet } from "viem/chains";
|
|
156
|
+
|
|
157
|
+
import { WalletStamper, EthereumWallet } from "@0xkey-io/wallet-stamper";
|
|
158
|
+
|
|
159
|
+
// Instantiate the WalletStamper with the EthereumWallet
|
|
160
|
+
const walletStamper = new WalletStamper(new EthereumWallet());
|
|
161
|
+
|
|
162
|
+
// Instantiate the ZeroXKeyClient with the WalletStamper
|
|
163
|
+
const client = new ZeroXKeyClient({ baseUrl: BASE_URL }, walletStamper);
|
|
164
|
+
|
|
165
|
+
// Call getWhoami to get the sub-org's organizationId and userId passing in the parent org id
|
|
166
|
+
// whoami { organizationId: string; organizationName: string; userId: string; username: string; }
|
|
167
|
+
const whoami = await client.getWhoami({
|
|
168
|
+
organizationId: process.env.ORGANIZATION_ID,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
let subOrganizationId = whoami?.organizationId;
|
|
172
|
+
|
|
173
|
+
// User does not yet have a sub-organization, so we need to create one
|
|
174
|
+
if (!subOrganizationId) {
|
|
175
|
+
// We'll need to use the parent org's API keys to create the sub-org on behalf of the user
|
|
176
|
+
const { ApiKeyStamper } = await import("@0xkey-io/api-key-stamper");
|
|
177
|
+
|
|
178
|
+
// Instantiate the ZeroXKeyClient with the ApiKeyStamper
|
|
179
|
+
const parentOrgClient = new ZeroXKeyClient(
|
|
180
|
+
{ baseUrl: BASE_URL },
|
|
181
|
+
new ApiKeyStamper({
|
|
182
|
+
// In practice we'll want to ensure these keys do not get exposed to the client
|
|
183
|
+
apiPublicKey: process.env.API_PUBLIC_KEY ?? "",
|
|
184
|
+
apiPrivateKey: process.env.API_PRIVATE_KEY ?? "",
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
const apiKeys = [
|
|
189
|
+
{
|
|
190
|
+
apiKeyName: "Wallet Auth - Embedded Wallet",
|
|
191
|
+
// The public key of the wallet that will be added as an API key and used to stamp future requests
|
|
192
|
+
publicKey,
|
|
193
|
+
// We set the curve type to 'API_KEY_CURVE_ED25519' for solana wallets
|
|
194
|
+
// If using an Ethereum wallet, set the curve type to 'API_KEY_CURVE_SECP256K1'
|
|
195
|
+
curveType,
|
|
196
|
+
},
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
const subOrg = await parentOrgClient.createSubOrganization({
|
|
200
|
+
organizationId: process.env.ORGANIZATION_ID,
|
|
201
|
+
subOrganizationName: `Sub Org - ${publicKey}`,
|
|
202
|
+
rootUsers: [
|
|
203
|
+
{
|
|
204
|
+
// Replace with user provided values
|
|
205
|
+
userName: "New User",
|
|
206
|
+
userEmail: "wallet@domain.com",
|
|
207
|
+
apiKeys,
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
rootQuorumThreshold: 1,
|
|
211
|
+
wallet: {
|
|
212
|
+
walletName: "Default Wallet",
|
|
213
|
+
accounts: DEFAULT_ETHEREUM_ACCOUNTS,
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
subOrganizationId = subOrg.subOrganizationId;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Get the wallets for this sub-organization
|
|
221
|
+
const wallets = await client.getWallets({
|
|
222
|
+
organizationId: subOrganizationId,
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Contributing
|
|
227
|
+
|
|
228
|
+
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
The `@0xkey-io/wallet-stamper` package is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamping.d.ts","sourceRoot":"","sources":["../../src/_kernel/stamping.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,YAAY,CAAC;AAE9C,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,eAAe,EAAE,CAAC,EAClB,gBAAgB,EAAE,MAAM,GACvB;IAAE,eAAe,EAAE,CAAC,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAA;CAAE,CAElD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const STAMP_HEADER_API_KEY = "X-Stamp";
|
|
4
|
+
function createStampResult(stampHeaderName, stampHeaderValue) {
|
|
5
|
+
return { stampHeaderName, stampHeaderValue };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
exports.STAMP_HEADER_API_KEY = STAMP_HEADER_API_KEY;
|
|
9
|
+
exports.createStampResult = createStampResult;
|
|
10
|
+
//# sourceMappingURL=stamping.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamping.js","sources":["../../src/_kernel/stamping.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAO,MAAM,oBAAoB,GAAG;AAE9B,SAAU,iBAAiB,CAC/B,eAAkB,EAClB,gBAAwB,EAAA;AAExB,IAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE;AAC9C;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamping.mjs","sources":["../../src/_kernel/stamping.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAO,MAAM,oBAAoB,GAAG;AAE9B,SAAU,iBAAiB,CAC/B,eAAkB,EAClB,gBAAwB,EAAA;AAExB,IAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE;AAC9C;;;;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 = "SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191";
|
|
2
|
+
export declare const SIGNATURE_SCHEME_TK_API_ED25519 = "SIGNATURE_SCHEME_TK_API_ED25519";
|
|
3
|
+
export { STAMP_HEADER_API_KEY as STAMP_HEADER_NAME } from "./_kernel/stamping";
|
|
4
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,wCAAwC,6CACT,CAAC;AAC7C,eAAO,MAAM,+BAA+B,oCACT,CAAC;AAEpC,OAAO,EAAE,oBAAoB,IAAI,iBAAiB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 = "SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191";
|
|
4
|
+
const SIGNATURE_SCHEME_TK_API_ED25519 = "SIGNATURE_SCHEME_TK_API_ED25519";
|
|
5
|
+
|
|
6
|
+
exports.SIGNATURE_SCHEME_TK_API_ED25519 = SIGNATURE_SCHEME_TK_API_ED25519;
|
|
7
|
+
exports.SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 = SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191;
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sources":["../src/constants.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAO,MAAM,wCAAwC,GACnD;AACK,MAAM,+BAA+B,GAC1C;;;;;"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 = "SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191";
|
|
2
|
+
const SIGNATURE_SCHEME_TK_API_ED25519 = "SIGNATURE_SCHEME_TK_API_ED25519";
|
|
3
|
+
|
|
4
|
+
export { SIGNATURE_SCHEME_TK_API_ED25519, SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 };
|
|
5
|
+
//# sourceMappingURL=constants.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","sources":["../src/constants.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAO,MAAM,wCAAwC,GACnD;AACK,MAAM,+BAA+B,GAC1C;;;;"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,kBAAmB,SAAQ,KAAK;IAGlC,aAAa,EAAE,GAAG;gBADzB,OAAO,EAAE,MAAM,EACR,aAAa,GAAE,GAAU;CAOnC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class WalletStamperError extends Error {
|
|
4
|
+
constructor(message, originalError = null) {
|
|
5
|
+
super(`${message}${originalError ? ` - error: ${originalError.message}` : ""}`);
|
|
6
|
+
this.originalError = originalError;
|
|
7
|
+
this.name = "WalletStamperError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
exports.WalletStamperError = WalletStamperError;
|
|
12
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sources":["../src/errors.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAM,MAAO,kBAAmB,SAAQ,KAAK,CAAA;IAC3C,WAAA,CACE,OAAe,EACR,aAAA,GAAqB,IAAI,EAAA;AAEhC,QAAA,KAAK,CACH,CAAA,EAAG,OAAO,GAAG,aAAa,GAAG,CAAA,UAAA,EAAa,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,CAAA,CAAE,CACzE;QAJM,IAAA,CAAA,aAAa,GAAb,aAAa;AAKpB,QAAA,IAAI,CAAC,IAAI,GAAG,oBAAoB;IAClC;AACD;;;;"}
|
package/dist/errors.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class WalletStamperError extends Error {
|
|
2
|
+
constructor(message, originalError = null) {
|
|
3
|
+
super(`${message}${originalError ? ` - error: ${originalError.message}` : ""}`);
|
|
4
|
+
this.originalError = originalError;
|
|
5
|
+
this.name = "WalletStamperError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { WalletStamperError };
|
|
10
|
+
//# sourceMappingURL=errors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.mjs","sources":["../src/errors.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAM,MAAO,kBAAmB,SAAQ,KAAK,CAAA;IAC3C,WAAA,CACE,OAAe,EACR,aAAA,GAAqB,IAAI,EAAA;AAEhC,QAAA,KAAK,CACH,CAAA,EAAG,OAAO,GAAG,aAAa,GAAG,CAAA,UAAA,EAAa,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,CAAA,CAAE,CACzE;QAJM,IAAA,CAAA,aAAa,GAAb,aAAa;AAKpB,QAAA,IAAI,CAAC,IAAI,GAAG,oBAAoB;IAClC;AACD;;;;"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { EthereumWalletInterface, WalletType } from "./types";
|
|
2
|
+
import { Hex } from "viem";
|
|
3
|
+
import "viem/window";
|
|
4
|
+
/**
|
|
5
|
+
* Abstract class representing a base Ethereum wallet.
|
|
6
|
+
* This class is used for stamping requests with an Ethereum wallet.
|
|
7
|
+
*
|
|
8
|
+
* To use this class, extend it and implement the `signMessage` method
|
|
9
|
+
* to provide a custom signing function. The `signMessage` method should
|
|
10
|
+
* return a promise that resolves to a hexadecimal string representing
|
|
11
|
+
* the signature of the provided message.
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class BaseEthereumWallet implements EthereumWalletInterface {
|
|
14
|
+
type: WalletType.Ethereum;
|
|
15
|
+
/**
|
|
16
|
+
* Abstract method to sign a message.
|
|
17
|
+
* Must be implemented by subclasses to provide a custom signing function.
|
|
18
|
+
*
|
|
19
|
+
* @param message - The message to be signed, either as a string or a Hex.
|
|
20
|
+
* @returns A promise that resolves to a Hex string representing the signature.
|
|
21
|
+
*/
|
|
22
|
+
abstract signMessage(message: string | Hex): Promise<Hex>;
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the public key associated with the wallet.
|
|
25
|
+
*
|
|
26
|
+
* @returns A promise that resolves to a string representing the compressed public key.
|
|
27
|
+
*/
|
|
28
|
+
getPublicKey(): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* EthereumWallet class extends the BaseEthereumWallet to provide
|
|
32
|
+
* specific implementations for Ethereum-based wallets.
|
|
33
|
+
*
|
|
34
|
+
* This class is responsible for signing messages using the Ethereum
|
|
35
|
+
* provider available in the browser (e.g., MetaMask). It interacts
|
|
36
|
+
* with the Ethereum provider to request account access and sign
|
|
37
|
+
* messages.
|
|
38
|
+
*/
|
|
39
|
+
export declare class EthereumWallet extends BaseEthereumWallet {
|
|
40
|
+
/**
|
|
41
|
+
* Signs a message using the Ethereum provider.
|
|
42
|
+
*
|
|
43
|
+
* @param message - The message to be signed, either as a string or a Hex.
|
|
44
|
+
* @returns A promise that resolves to a Hex string representing the signature.
|
|
45
|
+
*
|
|
46
|
+
* This method uses the 'personal_sign' method of the Ethereum provider
|
|
47
|
+
* to sign the message with the user's account.
|
|
48
|
+
*/
|
|
49
|
+
signMessage(message: string | Hex): Promise<`0x${string}`>;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves the Ethereum provider from the window object.
|
|
52
|
+
*
|
|
53
|
+
* @returns The EIP1193Provider instance.
|
|
54
|
+
*
|
|
55
|
+
* This method checks if the Ethereum provider is available in the
|
|
56
|
+
* window object and throws an error if not found.
|
|
57
|
+
*/
|
|
58
|
+
private getProvider;
|
|
59
|
+
/**
|
|
60
|
+
* Requests the user's Ethereum account from the provider.
|
|
61
|
+
*
|
|
62
|
+
* @returns A promise that resolves to the user's Ethereum address.
|
|
63
|
+
*
|
|
64
|
+
* This method uses the 'eth_requestAccounts' method of the Ethereum
|
|
65
|
+
* provider to request access to the user's account. It throws an error
|
|
66
|
+
* if no account is connected.
|
|
67
|
+
*/
|
|
68
|
+
private getAccount;
|
|
69
|
+
}
|
|
70
|
+
export declare const getCompressedPublicKey: (signature: Hex, message: string) => Promise<string>;
|
|
71
|
+
//# sourceMappingURL=ethereum.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ethereum.d.ts","sourceRoot":"","sources":["../src/ethereum.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAGL,GAAG,EAGJ,MAAM,MAAM,CAAC;AACd,OAAO,aAAa,CAAC;AAKrB;;;;;;;;GAQG;AACH,8BAAsB,kBAAmB,YAAW,uBAAuB;IACzE,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAuB;IAEhD;;;;;;OAMG;IACH,QAAQ,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAEzD;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;CAKtC;AAED;;;;;;;;GAQG;AACH,qBAAa,cAAe,SAAQ,kBAAkB;IACpD;;;;;;;;OAQG;IACG,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;IAWvC;;;;;;;OAOG;IACH,OAAO,CAAC,WAAW;IAQnB;;;;;;;;OAQG;YACW,UAAU;CAazB;AAED,eAAO,MAAM,sBAAsB,cACtB,GAAG,WACL,MAAM,oBAShB,CAAC"}
|
package/dist/ethereum.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var types = require('./types.js');
|
|
4
|
+
var viem = require('viem');
|
|
5
|
+
require('viem/window');
|
|
6
|
+
var errors = require('./errors.js');
|
|
7
|
+
var crypto = require('@0xkey-io/crypto');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Abstract class representing a base Ethereum wallet.
|
|
11
|
+
* This class is used for stamping requests with an Ethereum wallet.
|
|
12
|
+
*
|
|
13
|
+
* To use this class, extend it and implement the `signMessage` method
|
|
14
|
+
* to provide a custom signing function. The `signMessage` method should
|
|
15
|
+
* return a promise that resolves to a hexadecimal string representing
|
|
16
|
+
* the signature of the provided message.
|
|
17
|
+
*/
|
|
18
|
+
class BaseEthereumWallet {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.type = types.WalletType.Ethereum;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the public key associated with the wallet.
|
|
24
|
+
*
|
|
25
|
+
* @returns A promise that resolves to a string representing the compressed public key.
|
|
26
|
+
*/
|
|
27
|
+
async getPublicKey() {
|
|
28
|
+
const message = "GET_PUBLIC_KEY";
|
|
29
|
+
const signature = await this.signMessage(message);
|
|
30
|
+
return getCompressedPublicKey(signature, message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* EthereumWallet class extends the BaseEthereumWallet to provide
|
|
35
|
+
* specific implementations for Ethereum-based wallets.
|
|
36
|
+
*
|
|
37
|
+
* This class is responsible for signing messages using the Ethereum
|
|
38
|
+
* provider available in the browser (e.g., MetaMask). It interacts
|
|
39
|
+
* with the Ethereum provider to request account access and sign
|
|
40
|
+
* messages.
|
|
41
|
+
*/
|
|
42
|
+
class EthereumWallet extends BaseEthereumWallet {
|
|
43
|
+
/**
|
|
44
|
+
* Signs a message using the Ethereum provider.
|
|
45
|
+
*
|
|
46
|
+
* @param message - The message to be signed, either as a string or a Hex.
|
|
47
|
+
* @returns A promise that resolves to a Hex string representing the signature.
|
|
48
|
+
*
|
|
49
|
+
* This method uses the 'personal_sign' method of the Ethereum provider
|
|
50
|
+
* to sign the message with the user's account.
|
|
51
|
+
*/
|
|
52
|
+
async signMessage(message) {
|
|
53
|
+
const account = await this.getAccount();
|
|
54
|
+
const signature = await this.getProvider().request({
|
|
55
|
+
method: "personal_sign",
|
|
56
|
+
params: [message, account],
|
|
57
|
+
});
|
|
58
|
+
return signature;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the Ethereum provider from the window object.
|
|
62
|
+
*
|
|
63
|
+
* @returns The EIP1193Provider instance.
|
|
64
|
+
*
|
|
65
|
+
* This method checks if the Ethereum provider is available in the
|
|
66
|
+
* window object and throws an error if not found.
|
|
67
|
+
*/
|
|
68
|
+
getProvider() {
|
|
69
|
+
if (!window?.ethereum) {
|
|
70
|
+
throw new errors.WalletStamperError("No ethereum provider found");
|
|
71
|
+
}
|
|
72
|
+
return window.ethereum;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Requests the user's Ethereum account from the provider.
|
|
76
|
+
*
|
|
77
|
+
* @returns A promise that resolves to the user's Ethereum address.
|
|
78
|
+
*
|
|
79
|
+
* This method uses the 'eth_requestAccounts' method of the Ethereum
|
|
80
|
+
* provider to request access to the user's account. It throws an error
|
|
81
|
+
* if no account is connected.
|
|
82
|
+
*/
|
|
83
|
+
async getAccount() {
|
|
84
|
+
const provider = this.getProvider();
|
|
85
|
+
const [connectedAccount] = await provider.request({
|
|
86
|
+
method: "eth_requestAccounts",
|
|
87
|
+
});
|
|
88
|
+
if (!connectedAccount) {
|
|
89
|
+
throw new errors.WalletStamperError("No connected account found");
|
|
90
|
+
}
|
|
91
|
+
return connectedAccount;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const getCompressedPublicKey = async (signature, message) => {
|
|
95
|
+
const secp256k1PublicKey = await viem.recoverPublicKey({
|
|
96
|
+
hash: viem.hashMessage(message),
|
|
97
|
+
signature: signature,
|
|
98
|
+
});
|
|
99
|
+
const publicKey = secp256k1PublicKey.replace("0x", "");
|
|
100
|
+
const publicKeyBytes = Uint8Array.from(Buffer.from(publicKey, "hex"));
|
|
101
|
+
return Buffer.from(crypto.compressRawPublicKey(publicKeyBytes)).toString("hex");
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
exports.BaseEthereumWallet = BaseEthereumWallet;
|
|
105
|
+
exports.EthereumWallet = EthereumWallet;
|
|
106
|
+
exports.getCompressedPublicKey = getCompressedPublicKey;
|
|
107
|
+
//# sourceMappingURL=ethereum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ethereum.js","sources":["../src/ethereum.ts"],"sourcesContent":[null],"names":["WalletType","WalletStamperError","recoverPublicKey","hashMessage","compressRawPublicKey"],"mappings":";;;;;;;;AAaA;;;;;;;;AAQG;MACmB,kBAAkB,CAAA;AAAxC,IAAA,WAAA,GAAA;AACE,QAAA,IAAA,CAAA,IAAI,GAAwBA,gBAAU,CAAC,QAAQ;IAqBjD;AAVE;;;;AAIG;AACH,IAAA,MAAM,YAAY,GAAA;QAChB,MAAM,OAAO,GAAG,gBAAgB;QAChC,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC;AACjD,QAAA,OAAO,sBAAsB,CAAC,SAAS,EAAE,OAAO,CAAC;IACnD;AACD;AAED;;;;;;;;AAQG;AACG,MAAO,cAAe,SAAQ,kBAAkB,CAAA;AACpD;;;;;;;;AAQG;IACH,MAAM,WAAW,CAAC,OAAqB,EAAA;AACrC,QAAA,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE;QAEvC,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC;AACjD,YAAA,MAAM,EAAE,eAAwB;AAChC,YAAA,MAAM,EAAE,CAAC,OAAc,EAAE,OAAO,CAAC;AAClC,SAAA,CAAC;AAEF,QAAA,OAAO,SAAS;IAClB;AAEA;;;;;;;AAOG;IACK,WAAW,GAAA;AACjB,QAAA,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;AACrB,YAAA,MAAM,IAAIC,yBAAkB,CAAC,4BAA4B,CAAC;QAC5D;QAEA,OAAO,MAAM,CAAC,QAAQ;IACxB;AAEA;;;;;;;;AAQG;AACK,IAAA,MAAM,UAAU,GAAA;AACtB,QAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE;QAEnC,MAAM,CAAC,gBAAgB,CAAC,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC;AAChD,YAAA,MAAM,EAAE,qBAAqB;AAC9B,SAAA,CAAC;QAEF,IAAI,CAAC,gBAAgB,EAAE;AACrB,YAAA,MAAM,IAAIA,yBAAkB,CAAC,4BAA4B,CAAC;QAC5D;AAEA,QAAA,OAAO,gBAAgB;IACzB;AACD;AAEM,MAAM,sBAAsB,GAAG,OACpC,SAAc,EACd,OAAe,KACb;AACF,IAAA,MAAM,kBAAkB,GAAG,MAAMC,qBAAgB,CAAC;AAChD,QAAA,IAAI,EAAEC,gBAAW,CAAC,OAAO,CAAC;AAC1B,QAAA,SAAS,EAAE,SAAgB;AAC5B,KAAA,CAAC;IACF,MAAM,SAAS,GAAG,kBAAkB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;AACtD,IAAA,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AACrE,IAAA,OAAO,MAAM,CAAC,IAAI,CAACC,2BAAoB,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;AAC1E;;;;;;"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { WalletType } from './types.mjs';
|
|
2
|
+
import { recoverPublicKey, hashMessage } from 'viem';
|
|
3
|
+
import 'viem/window';
|
|
4
|
+
import { WalletStamperError } from './errors.mjs';
|
|
5
|
+
import { compressRawPublicKey } from '@0xkey-io/crypto';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Abstract class representing a base Ethereum wallet.
|
|
9
|
+
* This class is used for stamping requests with an Ethereum wallet.
|
|
10
|
+
*
|
|
11
|
+
* To use this class, extend it and implement the `signMessage` method
|
|
12
|
+
* to provide a custom signing function. The `signMessage` method should
|
|
13
|
+
* return a promise that resolves to a hexadecimal string representing
|
|
14
|
+
* the signature of the provided message.
|
|
15
|
+
*/
|
|
16
|
+
class BaseEthereumWallet {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.type = WalletType.Ethereum;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves the public key associated with the wallet.
|
|
22
|
+
*
|
|
23
|
+
* @returns A promise that resolves to a string representing the compressed public key.
|
|
24
|
+
*/
|
|
25
|
+
async getPublicKey() {
|
|
26
|
+
const message = "GET_PUBLIC_KEY";
|
|
27
|
+
const signature = await this.signMessage(message);
|
|
28
|
+
return getCompressedPublicKey(signature, message);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* EthereumWallet class extends the BaseEthereumWallet to provide
|
|
33
|
+
* specific implementations for Ethereum-based wallets.
|
|
34
|
+
*
|
|
35
|
+
* This class is responsible for signing messages using the Ethereum
|
|
36
|
+
* provider available in the browser (e.g., MetaMask). It interacts
|
|
37
|
+
* with the Ethereum provider to request account access and sign
|
|
38
|
+
* messages.
|
|
39
|
+
*/
|
|
40
|
+
class EthereumWallet extends BaseEthereumWallet {
|
|
41
|
+
/**
|
|
42
|
+
* Signs a message using the Ethereum provider.
|
|
43
|
+
*
|
|
44
|
+
* @param message - The message to be signed, either as a string or a Hex.
|
|
45
|
+
* @returns A promise that resolves to a Hex string representing the signature.
|
|
46
|
+
*
|
|
47
|
+
* This method uses the 'personal_sign' method of the Ethereum provider
|
|
48
|
+
* to sign the message with the user's account.
|
|
49
|
+
*/
|
|
50
|
+
async signMessage(message) {
|
|
51
|
+
const account = await this.getAccount();
|
|
52
|
+
const signature = await this.getProvider().request({
|
|
53
|
+
method: "personal_sign",
|
|
54
|
+
params: [message, account],
|
|
55
|
+
});
|
|
56
|
+
return signature;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Retrieves the Ethereum provider from the window object.
|
|
60
|
+
*
|
|
61
|
+
* @returns The EIP1193Provider instance.
|
|
62
|
+
*
|
|
63
|
+
* This method checks if the Ethereum provider is available in the
|
|
64
|
+
* window object and throws an error if not found.
|
|
65
|
+
*/
|
|
66
|
+
getProvider() {
|
|
67
|
+
if (!window?.ethereum) {
|
|
68
|
+
throw new WalletStamperError("No ethereum provider found");
|
|
69
|
+
}
|
|
70
|
+
return window.ethereum;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Requests the user's Ethereum account from the provider.
|
|
74
|
+
*
|
|
75
|
+
* @returns A promise that resolves to the user's Ethereum address.
|
|
76
|
+
*
|
|
77
|
+
* This method uses the 'eth_requestAccounts' method of the Ethereum
|
|
78
|
+
* provider to request access to the user's account. It throws an error
|
|
79
|
+
* if no account is connected.
|
|
80
|
+
*/
|
|
81
|
+
async getAccount() {
|
|
82
|
+
const provider = this.getProvider();
|
|
83
|
+
const [connectedAccount] = await provider.request({
|
|
84
|
+
method: "eth_requestAccounts",
|
|
85
|
+
});
|
|
86
|
+
if (!connectedAccount) {
|
|
87
|
+
throw new WalletStamperError("No connected account found");
|
|
88
|
+
}
|
|
89
|
+
return connectedAccount;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const getCompressedPublicKey = async (signature, message) => {
|
|
93
|
+
const secp256k1PublicKey = await recoverPublicKey({
|
|
94
|
+
hash: hashMessage(message),
|
|
95
|
+
signature: signature,
|
|
96
|
+
});
|
|
97
|
+
const publicKey = secp256k1PublicKey.replace("0x", "");
|
|
98
|
+
const publicKeyBytes = Uint8Array.from(Buffer.from(publicKey, "hex"));
|
|
99
|
+
return Buffer.from(compressRawPublicKey(publicKeyBytes)).toString("hex");
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { BaseEthereumWallet, EthereumWallet, getCompressedPublicKey };
|
|
103
|
+
//# sourceMappingURL=ethereum.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ethereum.mjs","sources":["../src/ethereum.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;AAaA;;;;;;;;AAQG;MACmB,kBAAkB,CAAA;AAAxC,IAAA,WAAA,GAAA;AACE,QAAA,IAAA,CAAA,IAAI,GAAwB,UAAU,CAAC,QAAQ;IAqBjD;AAVE;;;;AAIG;AACH,IAAA,MAAM,YAAY,GAAA;QAChB,MAAM,OAAO,GAAG,gBAAgB;QAChC,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC;AACjD,QAAA,OAAO,sBAAsB,CAAC,SAAS,EAAE,OAAO,CAAC;IACnD;AACD;AAED;;;;;;;;AAQG;AACG,MAAO,cAAe,SAAQ,kBAAkB,CAAA;AACpD;;;;;;;;AAQG;IACH,MAAM,WAAW,CAAC,OAAqB,EAAA;AACrC,QAAA,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE;QAEvC,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC;AACjD,YAAA,MAAM,EAAE,eAAwB;AAChC,YAAA,MAAM,EAAE,CAAC,OAAc,EAAE,OAAO,CAAC;AAClC,SAAA,CAAC;AAEF,QAAA,OAAO,SAAS;IAClB;AAEA;;;;;;;AAOG;IACK,WAAW,GAAA;AACjB,QAAA,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;AACrB,YAAA,MAAM,IAAI,kBAAkB,CAAC,4BAA4B,CAAC;QAC5D;QAEA,OAAO,MAAM,CAAC,QAAQ;IACxB;AAEA;;;;;;;;AAQG;AACK,IAAA,MAAM,UAAU,GAAA;AACtB,QAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE;QAEnC,MAAM,CAAC,gBAAgB,CAAC,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC;AAChD,YAAA,MAAM,EAAE,qBAAqB;AAC9B,SAAA,CAAC;QAEF,IAAI,CAAC,gBAAgB,EAAE;AACrB,YAAA,MAAM,IAAI,kBAAkB,CAAC,4BAA4B,CAAC;QAC5D;AAEA,QAAA,OAAO,gBAAgB;IACzB;AACD;AAEM,MAAM,sBAAsB,GAAG,OACpC,SAAc,EACd,OAAe,KACb;AACF,IAAA,MAAM,kBAAkB,GAAG,MAAM,gBAAgB,CAAC;AAChD,QAAA,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC;AAC1B,QAAA,SAAS,EAAE,SAAgB;AAC5B,KAAA,CAAC;IACF,MAAM,SAAS,GAAG,kBAAkB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;AACtD,IAAA,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AACrE,IAAA,OAAO,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;AAC1E;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants.js');
|
|
4
|
+
var errors = require('./errors.js');
|
|
5
|
+
var types = require('./types.js');
|
|
6
|
+
var stamper = require('./stamper.js');
|
|
7
|
+
var ethereum = require('./ethereum.js');
|
|
8
|
+
var stamping = require('./_kernel/stamping.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
exports.SIGNATURE_SCHEME_TK_API_ED25519 = constants.SIGNATURE_SCHEME_TK_API_ED25519;
|
|
13
|
+
exports.SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 = constants.SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191;
|
|
14
|
+
exports.WalletStamperError = errors.WalletStamperError;
|
|
15
|
+
Object.defineProperty(exports, "WalletType", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return types.WalletType; }
|
|
18
|
+
});
|
|
19
|
+
exports.WalletStamper = stamper.WalletStamper;
|
|
20
|
+
exports.BaseEthereumWallet = ethereum.BaseEthereumWallet;
|
|
21
|
+
exports.EthereumWallet = ethereum.EthereumWallet;
|
|
22
|
+
exports.getCompressedPublicKey = ethereum.getCompressedPublicKey;
|
|
23
|
+
exports.STAMP_HEADER_NAME = stamping.STAMP_HEADER_API_KEY;
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { SIGNATURE_SCHEME_TK_API_ED25519, SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 } from './constants.mjs';
|
|
2
|
+
export { WalletStamperError } from './errors.mjs';
|
|
3
|
+
export { WalletType } from './types.mjs';
|
|
4
|
+
export { WalletStamper } from './stamper.mjs';
|
|
5
|
+
export { BaseEthereumWallet, EthereumWallet, getCompressedPublicKey } from './ethereum.mjs';
|
|
6
|
+
export { STAMP_HEADER_API_KEY as STAMP_HEADER_NAME } from './_kernel/stamping.mjs';
|
|
7
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type TStamper, type WalletInterface, type TStamp } from "./types";
|
|
2
|
+
export declare class WalletStamper implements TStamper {
|
|
3
|
+
private wallet;
|
|
4
|
+
constructor(wallet: WalletInterface);
|
|
5
|
+
stamp(payload: string): Promise<TStamp>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=stamper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamper.d.ts","sourceRoot":"","sources":["../src/stamper.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,KAAK,MAAM,EAEZ,MAAM,SAAS,CAAC;AAWjB,qBAAa,aAAc,YAAW,QAAQ;IAC5C,OAAO,CAAC,MAAM,CAAkB;gBAEpB,MAAM,EAAE,eAAe;IAI7B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAyD9C"}
|
package/dist/stamper.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var encoding = require('@0xkey-io/encoding');
|
|
4
|
+
var errors = require('./errors.js');
|
|
5
|
+
var types = require('./types.js');
|
|
6
|
+
var constants = require('./constants.js');
|
|
7
|
+
var stamping = require('./_kernel/stamping.js');
|
|
8
|
+
|
|
9
|
+
// WalletStamper class implements the TStamper interface to use wallet's signature and public key
|
|
10
|
+
// to authenticate requests to ZeroXKey.
|
|
11
|
+
class WalletStamper {
|
|
12
|
+
constructor(wallet) {
|
|
13
|
+
this.wallet = wallet;
|
|
14
|
+
}
|
|
15
|
+
async stamp(payload) {
|
|
16
|
+
let signature;
|
|
17
|
+
try {
|
|
18
|
+
signature = await this.wallet.signMessage(payload);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new errors.WalletStamperError("Failed to sign the message", error);
|
|
22
|
+
}
|
|
23
|
+
// Determine the signature scheme based on the wallet type.
|
|
24
|
+
const scheme = this.wallet.type === types.WalletType.Solana
|
|
25
|
+
? constants.SIGNATURE_SCHEME_TK_API_ED25519
|
|
26
|
+
: constants.SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191;
|
|
27
|
+
let publicKey;
|
|
28
|
+
try {
|
|
29
|
+
// For Ethereum, we need to recover the public key from the signature over the payload.
|
|
30
|
+
// This is because recovering the SECP256K1 public key requires a signed message.
|
|
31
|
+
// This avoids an additional call to the wallet to get the public key.
|
|
32
|
+
if (this.wallet.type === types.WalletType.Ethereum) {
|
|
33
|
+
const { recoverPublicKey, hashMessage } = await import('viem');
|
|
34
|
+
const { compressRawPublicKey, toDerSignature } = await import('@0xkey-io/crypto');
|
|
35
|
+
const secp256k1PublicKey = await recoverPublicKey({
|
|
36
|
+
hash: hashMessage(payload),
|
|
37
|
+
signature: signature,
|
|
38
|
+
});
|
|
39
|
+
publicKey = secp256k1PublicKey.replace("0x", "");
|
|
40
|
+
const publicKeyBytes = Uint8Array.from(Buffer.from(publicKey, "hex"));
|
|
41
|
+
publicKey = Buffer.from(compressRawPublicKey(publicKeyBytes)).toString("hex");
|
|
42
|
+
// Convert the signature to DER format which is required by the ZeroXKey API.
|
|
43
|
+
signature = toDerSignature(signature.replace("0x", ""));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// For Solana, we can directly use the public key.
|
|
47
|
+
publicKey = await this.wallet.getPublicKey();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new errors.WalletStamperError("Failed to recover public key", error);
|
|
52
|
+
}
|
|
53
|
+
const stamp = {
|
|
54
|
+
publicKey,
|
|
55
|
+
scheme,
|
|
56
|
+
signature,
|
|
57
|
+
};
|
|
58
|
+
// Return the stamp as a base64url encoded JSON string in the header format.
|
|
59
|
+
return stamping.createStampResult(stamping.STAMP_HEADER_API_KEY, encoding.stringToBase64urlString(JSON.stringify(stamp)));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.WalletStamper = WalletStamper;
|
|
64
|
+
//# sourceMappingURL=stamper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamper.js","sources":["../src/stamper.ts"],"sourcesContent":[null],"names":["WalletStamperError","WalletType","SIGNATURE_SCHEME_TK_API_ED25519","SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191","createStampResult","STAMP_HEADER_NAME","stringToBase64urlString"],"mappings":";;;;;;;;AAgBA;AACA;MACa,aAAa,CAAA;AAGxB,IAAA,WAAA,CAAY,MAAuB,EAAA;AACjC,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM;IACtB;IAEA,MAAM,KAAK,CAAC,OAAe,EAAA;AACzB,QAAA,IAAI,SAAiB;AACrB,QAAA,IAAI;YACF,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;QACpD;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,MAAM,IAAIA,yBAAkB,CAAC,4BAA4B,EAAE,KAAK,CAAC;QACnE;;QAGA,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,IAAI,KAAKC,gBAAU,CAAC;AAC9B,cAAEC;cACAC,kDAAwC;AAE9C,QAAA,IAAI,SAAiB;AACrB,QAAA,IAAI;;;;YAIF,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAKF,gBAAU,CAAC,QAAQ,EAAE;gBAC5C,MAAM,EAAE,gBAAgB,EAAE,WAAW,EAAE,GAAG,MAAM,OAAO,MAAM,CAAC;gBAC9D,MAAM,EAAE,oBAAoB,EAAE,cAAc,EAAE,GAAG,MAAM,OACrD,kBAAkB,CACnB;AAED,gBAAA,MAAM,kBAAkB,GAAG,MAAM,gBAAgB,CAAC;AAChD,oBAAA,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC;AAC1B,oBAAA,SAAS,EAAE,SAAgB;AAC5B,iBAAA,CAAC;gBACF,SAAS,GAAG,kBAAkB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;AAChD,gBAAA,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AACrE,gBAAA,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,CACpE,KAAK,CACN;;AAGD,gBAAA,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACzD;iBAAO;;gBAEL,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE;YAC9C;QACF;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,MAAM,IAAID,yBAAkB,CAAC,8BAA8B,EAAE,KAAK,CAAC;QACrE;AAEA,QAAA,MAAM,KAAK,GAAG;YACZ,SAAS;YACT,MAAM;YACN,SAAS;SACV;;AAGD,QAAA,OAAOI,0BAAiB,CACtBC,6BAAiB,EACjBC,gCAAuB,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAC/C;IACH;AACD;;;;"}
|
package/dist/stamper.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { stringToBase64urlString } from '@0xkey-io/encoding';
|
|
2
|
+
import { WalletStamperError } from './errors.mjs';
|
|
3
|
+
import { WalletType } from './types.mjs';
|
|
4
|
+
import { SIGNATURE_SCHEME_TK_API_ED25519, SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191 } from './constants.mjs';
|
|
5
|
+
import { createStampResult, STAMP_HEADER_API_KEY } from './_kernel/stamping.mjs';
|
|
6
|
+
|
|
7
|
+
// WalletStamper class implements the TStamper interface to use wallet's signature and public key
|
|
8
|
+
// to authenticate requests to ZeroXKey.
|
|
9
|
+
class WalletStamper {
|
|
10
|
+
constructor(wallet) {
|
|
11
|
+
this.wallet = wallet;
|
|
12
|
+
}
|
|
13
|
+
async stamp(payload) {
|
|
14
|
+
let signature;
|
|
15
|
+
try {
|
|
16
|
+
signature = await this.wallet.signMessage(payload);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
throw new WalletStamperError("Failed to sign the message", error);
|
|
20
|
+
}
|
|
21
|
+
// Determine the signature scheme based on the wallet type.
|
|
22
|
+
const scheme = this.wallet.type === WalletType.Solana
|
|
23
|
+
? SIGNATURE_SCHEME_TK_API_ED25519
|
|
24
|
+
: SIGNATURE_SCHEME_TK_API_SECP256K1_EIP191;
|
|
25
|
+
let publicKey;
|
|
26
|
+
try {
|
|
27
|
+
// For Ethereum, we need to recover the public key from the signature over the payload.
|
|
28
|
+
// This is because recovering the SECP256K1 public key requires a signed message.
|
|
29
|
+
// This avoids an additional call to the wallet to get the public key.
|
|
30
|
+
if (this.wallet.type === WalletType.Ethereum) {
|
|
31
|
+
const { recoverPublicKey, hashMessage } = await import('viem');
|
|
32
|
+
const { compressRawPublicKey, toDerSignature } = await import('@0xkey-io/crypto');
|
|
33
|
+
const secp256k1PublicKey = await recoverPublicKey({
|
|
34
|
+
hash: hashMessage(payload),
|
|
35
|
+
signature: signature,
|
|
36
|
+
});
|
|
37
|
+
publicKey = secp256k1PublicKey.replace("0x", "");
|
|
38
|
+
const publicKeyBytes = Uint8Array.from(Buffer.from(publicKey, "hex"));
|
|
39
|
+
publicKey = Buffer.from(compressRawPublicKey(publicKeyBytes)).toString("hex");
|
|
40
|
+
// Convert the signature to DER format which is required by the ZeroXKey API.
|
|
41
|
+
signature = toDerSignature(signature.replace("0x", ""));
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// For Solana, we can directly use the public key.
|
|
45
|
+
publicKey = await this.wallet.getPublicKey();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new WalletStamperError("Failed to recover public key", error);
|
|
50
|
+
}
|
|
51
|
+
const stamp = {
|
|
52
|
+
publicKey,
|
|
53
|
+
scheme,
|
|
54
|
+
signature,
|
|
55
|
+
};
|
|
56
|
+
// Return the stamp as a base64url encoded JSON string in the header format.
|
|
57
|
+
return createStampResult(STAMP_HEADER_API_KEY, stringToBase64urlString(JSON.stringify(stamp)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { WalletStamper };
|
|
62
|
+
//# sourceMappingURL=stamper.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamper.mjs","sources":["../src/stamper.ts"],"sourcesContent":[null],"names":["STAMP_HEADER_NAME"],"mappings":";;;;;;AAgBA;AACA;MACa,aAAa,CAAA;AAGxB,IAAA,WAAA,CAAY,MAAuB,EAAA;AACjC,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM;IACtB;IAEA,MAAM,KAAK,CAAC,OAAe,EAAA;AACzB,QAAA,IAAI,SAAiB;AACrB,QAAA,IAAI;YACF,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;QACpD;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,MAAM,IAAI,kBAAkB,CAAC,4BAA4B,EAAE,KAAK,CAAC;QACnE;;QAGA,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC;AAC9B,cAAE;cACA,wCAAwC;AAE9C,QAAA,IAAI,SAAiB;AACrB,QAAA,IAAI;;;;YAIF,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,QAAQ,EAAE;gBAC5C,MAAM,EAAE,gBAAgB,EAAE,WAAW,EAAE,GAAG,MAAM,OAAO,MAAM,CAAC;gBAC9D,MAAM,EAAE,oBAAoB,EAAE,cAAc,EAAE,GAAG,MAAM,OACrD,kBAAkB,CACnB;AAED,gBAAA,MAAM,kBAAkB,GAAG,MAAM,gBAAgB,CAAC;AAChD,oBAAA,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC;AAC1B,oBAAA,SAAS,EAAE,SAAgB;AAC5B,iBAAA,CAAC;gBACF,SAAS,GAAG,kBAAkB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;AAChD,gBAAA,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AACrE,gBAAA,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,CACpE,KAAK,CACN;;AAGD,gBAAA,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACzD;iBAAO;;gBAEL,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE;YAC9C;QACF;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,MAAM,IAAI,kBAAkB,CAAC,8BAA8B,EAAE,KAAK,CAAC;QACrE;AAEA,QAAA,MAAM,KAAK,GAAG;YACZ,SAAS;YACT,MAAM;YACN,SAAS;SACV;;AAGD,QAAA,OAAO,iBAAiB,CACtBA,oBAAiB,EACjB,uBAAuB,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAC/C;IACH;AACD;;;;"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} TStamp
|
|
3
|
+
* @property {'X-Stamp'} stampHeaderName - The name of the stamp header.
|
|
4
|
+
* @property {string} stampHeaderValue - The value of the stamp header.
|
|
5
|
+
*/
|
|
6
|
+
export type TStamp = {
|
|
7
|
+
stampHeaderName: "X-Stamp";
|
|
8
|
+
stampHeaderValue: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* @interface TStamper
|
|
12
|
+
* @property {function(string): Promise<TStamp>} stamp - Function to stamp an input string.
|
|
13
|
+
*/
|
|
14
|
+
export interface TStamper {
|
|
15
|
+
stamp: (input: string) => Promise<TStamp>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Base interface for wallet functionalities common across different blockchain chains.
|
|
19
|
+
* @interface BaseWalletInterface
|
|
20
|
+
* @property {function(string): Promise<string>} signMessage - Signs a message and returns the hex signature as a string.
|
|
21
|
+
* @property {function(): Promise<string>} getPublicKey - Retrieves the public key as a string.
|
|
22
|
+
*/
|
|
23
|
+
export interface BaseWalletInterface {
|
|
24
|
+
type: WalletType.Ethereum | WalletType.Solana;
|
|
25
|
+
signMessage: (message: string) => Promise<string>;
|
|
26
|
+
getPublicKey: () => Promise<string>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Solana wallets can directly access the public key without needing a signed message.
|
|
30
|
+
* @interface SolanaWalletInterface
|
|
31
|
+
* @extends BaseWalletInterface
|
|
32
|
+
* @property {function(): string} getPublicKey - Returns the public key, which is the ED25519 hex encoded public key from your Solana wallet public key.
|
|
33
|
+
* @property {'solana'} type - The type of the wallet.
|
|
34
|
+
*/
|
|
35
|
+
export interface SolanaWalletInterface extends BaseWalletInterface {
|
|
36
|
+
type: WalletType.Solana;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Ethereum wallets require a signed message to derive the public key.
|
|
40
|
+
*
|
|
41
|
+
* @remarks This is the SECP256K1 public key of the Ethereum wallet, not the address.
|
|
42
|
+
* This requires that the wallet signs a message in order to derive the public key.
|
|
43
|
+
*
|
|
44
|
+
* @interface EthereumWalletInterface
|
|
45
|
+
* @extends BaseWalletInterface
|
|
46
|
+
* @property {function(): Promise<string>} getPublicKey - Returns the public key, which is the SECP256K1 hex encoded public key from your Ethereum wallet.
|
|
47
|
+
* @property {'ethereum'} type - The type of the wallet.
|
|
48
|
+
*/
|
|
49
|
+
export interface EthereumWalletInterface extends BaseWalletInterface {
|
|
50
|
+
type: WalletType.Ethereum;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Union type for wallet interfaces, supporting both Solana and Ethereum wallets.
|
|
54
|
+
* @typedef {SolanaWalletInterface | EthereumWalletInterface} WalletInterface
|
|
55
|
+
*/
|
|
56
|
+
export type WalletInterface = SolanaWalletInterface | EthereumWalletInterface;
|
|
57
|
+
/**
|
|
58
|
+
* Enum representing the type of wallet the user is stamping with.
|
|
59
|
+
*/
|
|
60
|
+
export declare enum WalletType {
|
|
61
|
+
Ethereum = "ethereum",
|
|
62
|
+
Solana = "solana"
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,eAAe,EAAE,SAAS,CAAC;IAC3B,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC3C;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC;IAC9C,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAClD,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CACrC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,qBAAsB,SAAQ,mBAAmB;IAChE,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC;CACzB;AAED;;;;;;;;;;GAUG;AACH,MAAM,WAAW,uBAAwB,SAAQ,mBAAmB;IAClE,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,qBAAqB,GAAG,uBAAuB,CAAC;AAE9E;;GAEG;AACH,oBAAY,UAAU;IACpB,QAAQ,aAAa;IACrB,MAAM,WAAW;CAClB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Enum representing the type of wallet the user is stamping with.
|
|
5
|
+
*/
|
|
6
|
+
exports.WalletType = void 0;
|
|
7
|
+
(function (WalletType) {
|
|
8
|
+
WalletType["Ethereum"] = "ethereum";
|
|
9
|
+
WalletType["Solana"] = "solana";
|
|
10
|
+
})(exports.WalletType || (exports.WalletType = {}));
|
|
11
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sources":["../src/types.ts"],"sourcesContent":[null],"names":["WalletType"],"mappings":";;AA8DA;;AAEG;AACSA;AAAZ,CAAA,UAAY,UAAU,EAAA;AACpB,IAAA,UAAA,CAAA,UAAA,CAAA,GAAA,UAAqB;AACrB,IAAA,UAAA,CAAA,QAAA,CAAA,GAAA,QAAiB;AACnB,CAAC,EAHWA,kBAAU,KAAVA,kBAAU,GAAA,EAAA,CAAA,CAAA;;"}
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum representing the type of wallet the user is stamping with.
|
|
3
|
+
*/
|
|
4
|
+
var WalletType;
|
|
5
|
+
(function (WalletType) {
|
|
6
|
+
WalletType["Ethereum"] = "ethereum";
|
|
7
|
+
WalletType["Solana"] = "solana";
|
|
8
|
+
})(WalletType || (WalletType = {}));
|
|
9
|
+
|
|
10
|
+
export { WalletType };
|
|
11
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sources":["../src/types.ts"],"sourcesContent":[null],"names":[],"mappings":"AA8DA;;AAEG;IACS;AAAZ,CAAA,UAAY,UAAU,EAAA;AACpB,IAAA,UAAA,CAAA,UAAA,CAAA,GAAA,UAAqB;AACrB,IAAA,UAAA,CAAA,QAAA,CAAA,GAAA,QAAiB;AACnB,CAAC,EAHW,UAAU,KAAV,UAAU,GAAA,EAAA,CAAA,CAAA;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0xkey-io/wallet-stamper",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"description": "Wallet stamper for @0xkey-io/http",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"0xkey",
|
|
19
|
+
"http",
|
|
20
|
+
"stamper",
|
|
21
|
+
"wallet authentication"
|
|
22
|
+
],
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "0xkey",
|
|
25
|
+
"url": "https://0xkey.com/"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/0xkey-io/sdk-js/tree/main/packages/wallet-stamper",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/0xkey-io/sdk-js/issues"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/0xkey-io/sdk-js.git",
|
|
34
|
+
"directory": "packages/wallet-stamper"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist/",
|
|
38
|
+
"CHANGELOG.md",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@0xkey-io/encoding": "0.1.0",
|
|
46
|
+
"@0xkey-io/crypto": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"optionalDependencies": {
|
|
49
|
+
"viem": "^2.21.35"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@solana/web3.js": "^1.95.8",
|
|
53
|
+
"dotenv": "^16.0.3",
|
|
54
|
+
"tweetnacl": "^1.0.3",
|
|
55
|
+
"tweetnacl-util": "^0.15.1",
|
|
56
|
+
"typescript": "5.4.3",
|
|
57
|
+
"@0xkey-io/http": "0.1.0",
|
|
58
|
+
"@0xkey-io/api-key-stamper": "0.1.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "rollup -c",
|
|
62
|
+
"clean": "rimraf ./dist ./.cache",
|
|
63
|
+
"test": "jest",
|
|
64
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
65
|
+
}
|
|
66
|
+
}
|