tempo.ts 0.6.1 → 0.7.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 +134 -0
- package/README.md +6 -2
- package/dist/ox/Transaction.js +1 -1
- package/dist/ox/Transaction.js.map +1 -1
- package/dist/server/Handler.d.ts +346 -0
- package/dist/server/Handler.d.ts.map +1 -0
- package/dist/server/Handler.js +441 -0
- package/dist/server/Handler.js.map +1 -0
- package/dist/server/Kv.d.ts +16 -0
- package/dist/server/Kv.d.ts.map +1 -0
- package/dist/server/Kv.js +25 -0
- package/dist/server/Kv.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/internal/requestListener.d.ts +124 -0
- package/dist/server/internal/requestListener.d.ts.map +1 -0
- package/dist/server/internal/requestListener.js +174 -0
- package/dist/server/internal/requestListener.js.map +1 -0
- package/dist/viem/Actions/account.d.ts +40 -0
- package/dist/viem/Actions/account.d.ts.map +1 -0
- package/dist/viem/Actions/account.js +87 -0
- package/dist/viem/Actions/account.js.map +1 -0
- package/dist/viem/Actions/amm.d.ts +51 -1245
- package/dist/viem/Actions/amm.d.ts.map +1 -1
- package/dist/viem/Actions/amm.js +15 -478
- package/dist/viem/Actions/amm.js.map +1 -1
- package/dist/viem/Actions/index.d.ts +1 -0
- package/dist/viem/Actions/index.d.ts.map +1 -1
- package/dist/viem/Actions/index.js +1 -0
- package/dist/viem/Actions/index.js.map +1 -1
- package/dist/viem/Actions/reward.d.ts +0 -1067
- package/dist/viem/Actions/reward.d.ts.map +1 -1
- package/dist/viem/Actions/reward.js +4 -212
- package/dist/viem/Actions/reward.js.map +1 -1
- package/dist/viem/Decorator.d.ts +28 -263
- package/dist/viem/Decorator.d.ts.map +1 -1
- package/dist/viem/Decorator.js +2 -10
- package/dist/viem/Decorator.js.map +1 -1
- package/dist/viem/Storage.d.ts +23 -0
- package/dist/viem/Storage.d.ts.map +1 -0
- package/dist/viem/Storage.js +47 -0
- package/dist/viem/Storage.js.map +1 -0
- package/dist/viem/Transport.d.ts +10 -1
- package/dist/viem/Transport.d.ts.map +1 -1
- package/dist/viem/Transport.js +22 -3
- package/dist/viem/Transport.js.map +1 -1
- package/dist/viem/internal/utils.d.ts +6 -0
- package/dist/viem/internal/utils.d.ts.map +1 -1
- package/dist/viem/internal/utils.js +24 -0
- package/dist/viem/internal/utils.js.map +1 -1
- package/dist/wagmi/Actions/amm.d.ts +0 -225
- package/dist/wagmi/Actions/amm.d.ts.map +1 -1
- package/dist/wagmi/Actions/amm.js +0 -248
- package/dist/wagmi/Actions/amm.js.map +1 -1
- package/dist/wagmi/Actions/reward.d.ts +0 -110
- package/dist/wagmi/Actions/reward.d.ts.map +1 -1
- package/dist/wagmi/Actions/reward.js +0 -121
- package/dist/wagmi/Actions/reward.js.map +1 -1
- package/dist/wagmi/Connector.d.ts +6 -17
- package/dist/wagmi/Connector.d.ts.map +1 -1
- package/dist/wagmi/Connector.js +17 -43
- package/dist/wagmi/Connector.js.map +1 -1
- package/dist/wagmi/Hooks/amm.d.ts +0 -236
- package/dist/wagmi/Hooks/amm.d.ts.map +1 -1
- package/dist/wagmi/Hooks/amm.js +0 -285
- package/dist/wagmi/Hooks/amm.js.map +1 -1
- package/dist/wagmi/Hooks/reward.d.ts +0 -88
- package/dist/wagmi/Hooks/reward.d.ts.map +1 -1
- package/dist/wagmi/Hooks/reward.js +0 -103
- package/dist/wagmi/Hooks/reward.js.map +1 -1
- package/dist/wagmi/KeyManager.d.ts +57 -0
- package/dist/wagmi/KeyManager.d.ts.map +1 -0
- package/dist/wagmi/KeyManager.js +101 -0
- package/dist/wagmi/KeyManager.js.map +1 -0
- package/dist/wagmi/index.d.ts +1 -0
- package/dist/wagmi/index.d.ts.map +1 -1
- package/dist/wagmi/index.js +1 -0
- package/dist/wagmi/index.js.map +1 -1
- package/package.json +8 -2
- package/src/ox/Transaction.ts +1 -1
- package/src/ox/e2e.test.ts +7 -0
- package/src/server/Handler.test.ts +566 -0
- package/src/server/Handler.ts +577 -0
- package/src/server/Kv.ts +40 -0
- package/src/server/index.ts +2 -0
- package/src/server/internal/requestListener.ts +285 -0
- package/src/viem/Actions/account.test.ts +414 -0
- package/src/viem/Actions/account.ts +108 -0
- package/src/viem/Actions/amm.test.ts +10 -284
- package/src/viem/Actions/amm.ts +88 -768
- package/src/viem/Actions/index.ts +1 -0
- package/src/viem/Actions/reward.test.ts +4 -212
- package/src/viem/Actions/reward.ts +4 -291
- package/src/viem/Decorator.test.ts +1 -0
- package/src/viem/Decorator.ts +32 -294
- package/src/viem/Storage.ts +88 -0
- package/src/viem/Transport.ts +40 -2
- package/src/viem/e2e.test.ts +106 -3
- package/src/viem/internal/utils.ts +21 -0
- package/src/wagmi/Actions/amm.test.ts +7 -85
- package/src/wagmi/Actions/amm.ts +0 -346
- package/src/wagmi/Actions/reward.test.ts +0 -99
- package/src/wagmi/Actions/reward.ts +0 -203
- package/src/wagmi/Connector.test.ts +4 -1
- package/src/wagmi/Connector.ts +24 -58
- package/src/wagmi/Hooks/amm.test.ts +8 -200
- package/src/wagmi/Hooks/amm.ts +0 -443
- package/src/wagmi/Hooks/reward.test.ts +1 -142
- package/src/wagmi/Hooks/reward.ts +0 -196
- package/src/wagmi/KeyManager.ts +159 -0
- package/src/wagmi/index.ts +1 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Credit: https://github.com/mjackson/remix-the-web/blob/main/packages/node-fetch-server/src/lib/request-listener.ts
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a fetch handler in a Node.js request listener that can be used with:
|
|
5
|
+
*
|
|
6
|
+
* - [`http.createServer()`](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener)
|
|
7
|
+
* - [`https.createServer()`](https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener)
|
|
8
|
+
* - [`http2.createServer()`](https://nodejs.org/api/http2.html#http2createserveroptions-onrequesthandler)
|
|
9
|
+
* - [`http2.createSecureServer()`](https://nodejs.org/api/http2.html#http2createsecureserveroptions-onrequesthandler)
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import * as http from 'node:http';
|
|
15
|
+
* import { createRequestListener } from '@mjackson/node-fetch-server';
|
|
16
|
+
*
|
|
17
|
+
* async function handler(request) {
|
|
18
|
+
* return new Response('Hello, world!');
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* let server = http.createServer(
|
|
22
|
+
* createRequestListener(handler)
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* server.listen(3000);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param handler The fetch handler to use for processing incoming requests.
|
|
29
|
+
* @param options Request listener options.
|
|
30
|
+
* @returns A Node.js request listener function.
|
|
31
|
+
*/
|
|
32
|
+
export function fromFetchHandler(handler, options) {
|
|
33
|
+
const onError = options?.onError ?? defaultErrorHandler;
|
|
34
|
+
return async (req, res) => {
|
|
35
|
+
const request = createRequest(req, res, options);
|
|
36
|
+
const client = {
|
|
37
|
+
address: req.socket.remoteAddress,
|
|
38
|
+
family: req.socket.remoteFamily,
|
|
39
|
+
port: req.socket.remotePort,
|
|
40
|
+
};
|
|
41
|
+
let response;
|
|
42
|
+
try {
|
|
43
|
+
response = await handler(request, client);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
try {
|
|
47
|
+
response = (await onError(error)) ?? internalServerError();
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
console.error(`There was an error in the error handler: ${error}`);
|
|
51
|
+
response = internalServerError();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await sendResponse(res, response);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function defaultErrorHandler(error) {
|
|
58
|
+
console.error(error);
|
|
59
|
+
return internalServerError();
|
|
60
|
+
}
|
|
61
|
+
function internalServerError() {
|
|
62
|
+
return new Response(
|
|
63
|
+
// "Internal Server Error"
|
|
64
|
+
new Uint8Array([
|
|
65
|
+
73, 110, 116, 101, 114, 110, 97, 108, 32, 83, 101, 114, 118, 101, 114, 32,
|
|
66
|
+
69, 114, 114, 111, 114,
|
|
67
|
+
]), {
|
|
68
|
+
headers: {
|
|
69
|
+
'Content-Type': 'text/plain',
|
|
70
|
+
},
|
|
71
|
+
status: 500,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object from
|
|
76
|
+
*
|
|
77
|
+
* - a [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) pair
|
|
78
|
+
* - a [`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse) pair
|
|
79
|
+
*
|
|
80
|
+
* @param req The incoming request object.
|
|
81
|
+
* @param res The server response object.
|
|
82
|
+
* @param options
|
|
83
|
+
* @returns A request object.
|
|
84
|
+
*/
|
|
85
|
+
export function createRequest(req, res, options) {
|
|
86
|
+
const controller = new AbortController();
|
|
87
|
+
res.on('close', () => {
|
|
88
|
+
controller.abort();
|
|
89
|
+
});
|
|
90
|
+
const method = req.method ?? 'GET';
|
|
91
|
+
const headers = createHeaders(req);
|
|
92
|
+
const protocol = options?.protocol ??
|
|
93
|
+
('encrypted' in req.socket && req.socket.encrypted ? 'https:' : 'http:');
|
|
94
|
+
const host = options?.host ?? headers.get('Host') ?? 'localhost';
|
|
95
|
+
const url = new URL(req.url, `${protocol}//${host}`);
|
|
96
|
+
const init = { headers, method, signal: controller.signal };
|
|
97
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
98
|
+
init.body = new ReadableStream({
|
|
99
|
+
start(controller) {
|
|
100
|
+
req.on('data', (chunk) => {
|
|
101
|
+
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
102
|
+
});
|
|
103
|
+
req.on('end', () => {
|
|
104
|
+
controller.close();
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
init.duplex = 'half';
|
|
109
|
+
}
|
|
110
|
+
return new Request(url, init);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object from the headers in a Node.js
|
|
114
|
+
* [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest).
|
|
115
|
+
*
|
|
116
|
+
* @param req The incoming request object.
|
|
117
|
+
* @returns A headers object.
|
|
118
|
+
*/
|
|
119
|
+
export function createHeaders(req) {
|
|
120
|
+
const headers = new Headers();
|
|
121
|
+
const rawHeaders = req.rawHeaders;
|
|
122
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
123
|
+
if (rawHeaders[i]?.startsWith(':'))
|
|
124
|
+
continue;
|
|
125
|
+
headers.append(rawHeaders[i], rawHeaders[i + 1]);
|
|
126
|
+
}
|
|
127
|
+
return headers;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Sends a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the client using a Node.js
|
|
131
|
+
* [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse)
|
|
132
|
+
* object.
|
|
133
|
+
*
|
|
134
|
+
* @param res The server response object.
|
|
135
|
+
* @param response The response to send.
|
|
136
|
+
*/
|
|
137
|
+
export async function sendResponse(res, response) {
|
|
138
|
+
// Iterate over response.headers so we are sure to send multiple Set-Cookie headers correctly.
|
|
139
|
+
// These would incorrectly be merged into a single header if we tried to use
|
|
140
|
+
// `Object.fromEntries(response.headers.entries())`.
|
|
141
|
+
const headers = {};
|
|
142
|
+
for (const [key, value] of response.headers) {
|
|
143
|
+
if (key in headers) {
|
|
144
|
+
if (Array.isArray(headers[key])) {
|
|
145
|
+
headers[key].push(value);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
headers[key] = [headers[key], value];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
headers[key] = value;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
;
|
|
156
|
+
res.writeHead(response.status, headers);
|
|
157
|
+
if (response.body != null && res.req.method !== 'HEAD') {
|
|
158
|
+
for await (const chunk of readStream(response.body)) {
|
|
159
|
+
// @ts-expect-error - Node typings for http2 require a 2nd parameter to write but it's optional
|
|
160
|
+
res.write(chunk);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
res.end();
|
|
164
|
+
}
|
|
165
|
+
export async function* readStream(stream) {
|
|
166
|
+
const reader = stream.getReader();
|
|
167
|
+
while (true) {
|
|
168
|
+
const { done, value } = await reader.read();
|
|
169
|
+
if (done)
|
|
170
|
+
break;
|
|
171
|
+
yield value;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=requestListener.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestListener.js","sourceRoot":"","sources":["../../../src/server/internal/requestListener.ts"],"names":[],"mappings":"AAAA,cAAc;AACd,qHAAqH;AA+BrH;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,gBAAgB,CAC9B,OAAqB,EACrB,OAAgC;IAEhC,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,mBAAmB,CAAA;IAEvD,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QACxB,MAAM,OAAO,GAA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AO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAC1B,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAW,EAAE,KAAK,CAAC,CAAA;YAChD,CAAC;QACH,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QACtB,CAAC;IACH,CAAC;IAED,CAAC;IAAC,GAAW,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAEjD,IAAI,QAAQ,CAAC,IAAI,IAAI,IAAI,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;QACvD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACpD,+FAA+F;YAC/F,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QAClB,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAG,EAAE,CAAA;AACX,CAAC;AAED,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,UAAU,CAC/B,MAAkC;IAElC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAA;IAEjC,OAAO,IAAI,EAAE,CAAC;QACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;QAC3C,IAAI,IAAI;YAAE,MAAK;QACf,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Chain, Client, Transport } from 'viem';
|
|
2
|
+
import type { VerifyHashParameters, VerifyHashReturnType } from 'viem/actions';
|
|
3
|
+
import type { PartialBy } from '../../internal/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Verifies that a signature is valid for a given hash and address.
|
|
6
|
+
* Supports multiple signature types: Secp256k1, P256, WebCrypto P256, and WebAuthn P256.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { createClient, http } from 'viem'
|
|
11
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
12
|
+
* import { Actions, Account, P256 } from 'tempo.ts/viem'
|
|
13
|
+
*
|
|
14
|
+
* const client = createClient({
|
|
15
|
+
* chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
|
|
16
|
+
* transport: http(),
|
|
17
|
+
* })
|
|
18
|
+
*
|
|
19
|
+
* const privateKey = P256.randomPrivateKey()
|
|
20
|
+
* const account = Account.fromP256(privateKey)
|
|
21
|
+
*
|
|
22
|
+
* const hash = '0x...'
|
|
23
|
+
* const signature = await account.sign({ hash })
|
|
24
|
+
*
|
|
25
|
+
* const valid = await Actions.verifyHash(client, {
|
|
26
|
+
* hash,
|
|
27
|
+
* signature,
|
|
28
|
+
* })
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param client - Client.
|
|
32
|
+
* @param parameters - Parameters.
|
|
33
|
+
* @returns Whether the signature is valid.
|
|
34
|
+
*/
|
|
35
|
+
export declare function verifyHash<chain extends Chain | undefined>(client: Client<Transport, chain>, parameters: verifyHash.Parameters): Promise<verifyHash.ReturnValue>;
|
|
36
|
+
export declare namespace verifyHash {
|
|
37
|
+
type Parameters = PartialBy<VerifyHashParameters, 'address'>;
|
|
38
|
+
type ReturnValue = VerifyHashReturnType;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../../../src/viem/Actions/account.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AACpD,OAAO,KAAK,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAG9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,UAAU,CAAC,KAAK,SAAS,KAAK,GAAG,SAAS,EAC9D,MAAM,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,EAChC,UAAU,EAAE,UAAU,CAAC,UAAU,GAChC,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAwDjC;AAED,MAAM,CAAC,OAAO,WAAW,UAAU,CAAC;IAClC,KAAY,UAAU,GAAG,SAAS,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAAA;IAEnE,KAAY,WAAW,GAAG,oBAAoB,CAAA;CAC/C"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Secp256k1 } from 'ox';
|
|
2
|
+
import * as Hex from 'ox/Hex';
|
|
3
|
+
import * as P256 from 'ox/P256';
|
|
4
|
+
import * as WebAuthnP256 from 'ox/WebAuthnP256';
|
|
5
|
+
import { verifyHash as viem_verifyHash } from 'viem/actions';
|
|
6
|
+
import { getAction } from 'viem/utils';
|
|
7
|
+
import * as SignatureEnvelope from '../../ox/SignatureEnvelope.js';
|
|
8
|
+
/**
|
|
9
|
+
* Verifies that a signature is valid for a given hash and address.
|
|
10
|
+
* Supports multiple signature types: Secp256k1, P256, WebCrypto P256, and WebAuthn P256.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createClient, http } from 'viem'
|
|
15
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
16
|
+
* import { Actions, Account, P256 } from 'tempo.ts/viem'
|
|
17
|
+
*
|
|
18
|
+
* const client = createClient({
|
|
19
|
+
* chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
|
|
20
|
+
* transport: http(),
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* const privateKey = P256.randomPrivateKey()
|
|
24
|
+
* const account = Account.fromP256(privateKey)
|
|
25
|
+
*
|
|
26
|
+
* const hash = '0x...'
|
|
27
|
+
* const signature = await account.sign({ hash })
|
|
28
|
+
*
|
|
29
|
+
* const valid = await Actions.verifyHash(client, {
|
|
30
|
+
* hash,
|
|
31
|
+
* signature,
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @param client - Client.
|
|
36
|
+
* @param parameters - Parameters.
|
|
37
|
+
* @returns Whether the signature is valid.
|
|
38
|
+
*/
|
|
39
|
+
export async function verifyHash(client, parameters) {
|
|
40
|
+
const { hash } = parameters;
|
|
41
|
+
const signature = (() => {
|
|
42
|
+
const signature = parameters.signature;
|
|
43
|
+
if (Hex.validate(signature))
|
|
44
|
+
return signature;
|
|
45
|
+
if (typeof signature === 'object' && 'r' in signature && 's' in signature)
|
|
46
|
+
return SignatureEnvelope.serialize({
|
|
47
|
+
type: 'secp256k1',
|
|
48
|
+
signature: {
|
|
49
|
+
r: BigInt(signature.r),
|
|
50
|
+
s: BigInt(signature.s),
|
|
51
|
+
yParity: signature.yParity,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
return Hex.fromBytes(signature);
|
|
55
|
+
})();
|
|
56
|
+
const [envelope, userAddress] = (() => {
|
|
57
|
+
const envelope = SignatureEnvelope.from(signature);
|
|
58
|
+
// TODO: add once we have key auth
|
|
59
|
+
// if (envelope.type === 'keychain')
|
|
60
|
+
// return [envelope.inner, envelope.userAddress]
|
|
61
|
+
return [envelope, undefined];
|
|
62
|
+
})();
|
|
63
|
+
if (envelope.type === 'p256')
|
|
64
|
+
return P256.verify({
|
|
65
|
+
payload: hash,
|
|
66
|
+
publicKey: envelope.publicKey,
|
|
67
|
+
signature: envelope.signature,
|
|
68
|
+
hash: envelope.prehash,
|
|
69
|
+
});
|
|
70
|
+
if (envelope.type === 'webAuthn')
|
|
71
|
+
return WebAuthnP256.verify({
|
|
72
|
+
challenge: hash,
|
|
73
|
+
metadata: envelope.metadata,
|
|
74
|
+
publicKey: envelope.publicKey,
|
|
75
|
+
signature: envelope.signature,
|
|
76
|
+
});
|
|
77
|
+
// TODO: add once we have key auth
|
|
78
|
+
// if (envelope.type === 'keychain') throw new Error('not supported')
|
|
79
|
+
const address = parameters.address ??
|
|
80
|
+
userAddress ??
|
|
81
|
+
Secp256k1.recoverAddress({
|
|
82
|
+
payload: hash,
|
|
83
|
+
signature: envelope.signature,
|
|
84
|
+
});
|
|
85
|
+
return await getAction(client, viem_verifyHash, 'verifyHash')({ ...parameters, address });
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=account.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../../../src/viem/Actions/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAA;AAC9B,OAAO,KAAK,GAAG,MAAM,QAAQ,CAAA;AAC7B,OAAO,KAAK,IAAI,MAAM,SAAS,CAAA;AAC/B,OAAO,KAAK,YAAY,MAAM,iBAAiB,CAAA;AAG/C,OAAO,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,cAAc,CAAA;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtC,OAAO,KAAK,iBAAiB,MAAM,+BAA+B,CAAA;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,MAAgC,EAChC,UAAiC;IAEjC,MAAM,EAAE,IAAI,EAAE,GAAG,UAAU,CAAA;IAE3B,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE;QACtB,MAAM,SAAS,GAAG,UAAU,CAAC,SAAS,CAAA;QACtC,IAAI,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC;YAAE,OAAO,SAAS,CAAA;QAC7C,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,IAAI,SAAS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