@sovereign-sdk/signers 0.2.1 → 0.3.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/dist/index.d.mts CHANGED
@@ -1,42 +1,8 @@
1
+ import { S as SignerOpt, a as Signer } from './signer-DID_aCoy.mjs';
1
2
  import { MetaMaskInpageProvider } from '@metamask/providers';
2
3
  import * as ed from '@noble/ed25519';
3
4
  import { PrivKey } from '@noble/secp256k1';
4
5
 
5
- /**
6
- * A signer interface that can be implemented to provide custom signers for use in Sovereign SDK applications.
7
- */
8
- interface Signer {
9
- /**
10
- * Sign a message.
11
- *
12
- * @param message - The message to sign
13
- * @returns A promise that resolves to the signature
14
- */
15
- sign(message: Uint8Array): Promise<Uint8Array>;
16
- /**
17
- * Get the public key.
18
- *
19
- * @returns A promise that resolves to the public key
20
- */
21
- publicKey(): Promise<Uint8Array>;
22
- }
23
- /**
24
- * Options for initializing a signer.
25
- */
26
- type SignerOpt = {
27
- /**
28
- * The rollup schema generated during building of the rollup.
29
- *
30
- * Can be used to serialize and deserialize types and provide human readable representation
31
- * of signing data.
32
- */
33
- schema: Record<string, unknown>;
34
- /**
35
- * The curve to use for signing.
36
- */
37
- curve: "ed25519" | "secp256k1";
38
- };
39
-
40
6
  /**
41
7
  * Options for initializing a MetaMask Snap signer.
42
8
  */
@@ -113,4 +79,4 @@ declare class PrivySigner implements Signer {
113
79
  private cachePublicKey;
114
80
  }
115
81
 
116
- export { Ed25519Signer, type EthereumProvider, type MetaMaskSnapSignerOpt, PrivySigner, PrivySignerError, Secp256k1Signer, type Signer, type SignerOpt, newMetaMaskSnapSigner };
82
+ export { Ed25519Signer, type EthereumProvider, type MetaMaskSnapSignerOpt, PrivySigner, PrivySignerError, Secp256k1Signer, Signer, SignerOpt, newMetaMaskSnapSigner };
package/dist/index.d.ts CHANGED
@@ -1,42 +1,8 @@
1
+ import { S as SignerOpt, a as Signer } from './signer-DID_aCoy.js';
1
2
  import { MetaMaskInpageProvider } from '@metamask/providers';
2
3
  import * as ed from '@noble/ed25519';
3
4
  import { PrivKey } from '@noble/secp256k1';
4
5
 
5
- /**
6
- * A signer interface that can be implemented to provide custom signers for use in Sovereign SDK applications.
7
- */
8
- interface Signer {
9
- /**
10
- * Sign a message.
11
- *
12
- * @param message - The message to sign
13
- * @returns A promise that resolves to the signature
14
- */
15
- sign(message: Uint8Array): Promise<Uint8Array>;
16
- /**
17
- * Get the public key.
18
- *
19
- * @returns A promise that resolves to the public key
20
- */
21
- publicKey(): Promise<Uint8Array>;
22
- }
23
- /**
24
- * Options for initializing a signer.
25
- */
26
- type SignerOpt = {
27
- /**
28
- * The rollup schema generated during building of the rollup.
29
- *
30
- * Can be used to serialize and deserialize types and provide human readable representation
31
- * of signing data.
32
- */
33
- schema: Record<string, unknown>;
34
- /**
35
- * The curve to use for signing.
36
- */
37
- curve: "ed25519" | "secp256k1";
38
- };
39
-
40
6
  /**
41
7
  * Options for initializing a MetaMask Snap signer.
42
8
  */
@@ -113,4 +79,4 @@ declare class PrivySigner implements Signer {
113
79
  private cachePublicKey;
114
80
  }
115
81
 
116
- export { Ed25519Signer, type EthereumProvider, type MetaMaskSnapSignerOpt, PrivySigner, PrivySignerError, Secp256k1Signer, type Signer, type SignerOpt, newMetaMaskSnapSigner };
82
+ export { Ed25519Signer, type EthereumProvider, type MetaMaskSnapSignerOpt, PrivySigner, PrivySignerError, Secp256k1Signer, Signer, SignerOpt, newMetaMaskSnapSigner };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A signer interface that can be implemented to provide custom signers for use in Sovereign SDK applications.
3
+ */
4
+ interface Signer {
5
+ /**
6
+ * Sign a message.
7
+ *
8
+ * @param message - The message to sign
9
+ * @returns A promise that resolves to the signature
10
+ */
11
+ sign(message: Uint8Array): Promise<Uint8Array>;
12
+ /**
13
+ * Get the public key.
14
+ *
15
+ * @returns A promise that resolves to the public key
16
+ */
17
+ publicKey(): Promise<Uint8Array>;
18
+ }
19
+ /**
20
+ * Options for initializing a signer.
21
+ */
22
+ type SignerOpt = {
23
+ /**
24
+ * The rollup schema generated during building of the rollup.
25
+ *
26
+ * Can be used to serialize and deserialize types and provide human readable representation
27
+ * of signing data.
28
+ */
29
+ schema: Record<string, unknown>;
30
+ /**
31
+ * The curve to use for signing.
32
+ */
33
+ curve: "ed25519" | "secp256k1";
34
+ };
35
+
36
+ export type { SignerOpt as S, Signer as a };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A signer interface that can be implemented to provide custom signers for use in Sovereign SDK applications.
3
+ */
4
+ interface Signer {
5
+ /**
6
+ * Sign a message.
7
+ *
8
+ * @param message - The message to sign
9
+ * @returns A promise that resolves to the signature
10
+ */
11
+ sign(message: Uint8Array): Promise<Uint8Array>;
12
+ /**
13
+ * Get the public key.
14
+ *
15
+ * @returns A promise that resolves to the public key
16
+ */
17
+ publicKey(): Promise<Uint8Array>;
18
+ }
19
+ /**
20
+ * Options for initializing a signer.
21
+ */
22
+ type SignerOpt = {
23
+ /**
24
+ * The rollup schema generated during building of the rollup.
25
+ *
26
+ * Can be used to serialize and deserialize types and provide human readable representation
27
+ * of signing data.
28
+ */
29
+ schema: Record<string, unknown>;
30
+ /**
31
+ * The curve to use for signing.
32
+ */
33
+ curve: "ed25519" | "secp256k1";
34
+ };
35
+
36
+ export type { SignerOpt as S, Signer as a };
@@ -0,0 +1,33 @@
1
+ import { a as Signer } from './signer-DID_aCoy.mjs';
2
+ import { MetaMaskInpageProvider } from '@metamask/providers';
3
+
4
+ /**
5
+ * EIP-712 signer implementation that uses MetaMask's eth_signTypedData_v4 method.
6
+ *
7
+ * This signer expects the message to be a valid UnsignedTransaction that can be
8
+ * parsed by the schema's eip712Json() method.
9
+ *
10
+ * The signer uses the provided address for signing operations.
11
+ */
12
+ declare class Eip712Signer implements Signer {
13
+ private readonly provider;
14
+ private readonly schema;
15
+ private readonly address;
16
+ private cachedPublicKey?;
17
+ private static readonly SIGNER_ID;
18
+ constructor(provider: MetaMaskInpageProvider, schemaJson: Record<string, unknown>, address: string);
19
+ /**
20
+ * Cache the public key recovered from a signature.
21
+ */
22
+ private cachePublicKey;
23
+ /**
24
+ * Get the public key. Requires sign() to be called first to recover the key from a signature.
25
+ */
26
+ publicKey(): Promise<Uint8Array>;
27
+ /**
28
+ * Sign an UnsignedTransaction using EIP-712 typed data signing.
29
+ */
30
+ sign(message: Uint8Array): Promise<Uint8Array>;
31
+ }
32
+
33
+ export { Eip712Signer, Signer };
package/dist/wasm.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { a as Signer } from './signer-DID_aCoy.js';
2
+ import { MetaMaskInpageProvider } from '@metamask/providers';
3
+
4
+ /**
5
+ * EIP-712 signer implementation that uses MetaMask's eth_signTypedData_v4 method.
6
+ *
7
+ * This signer expects the message to be a valid UnsignedTransaction that can be
8
+ * parsed by the schema's eip712Json() method.
9
+ *
10
+ * The signer uses the provided address for signing operations.
11
+ */
12
+ declare class Eip712Signer implements Signer {
13
+ private readonly provider;
14
+ private readonly schema;
15
+ private readonly address;
16
+ private cachedPublicKey?;
17
+ private static readonly SIGNER_ID;
18
+ constructor(provider: MetaMaskInpageProvider, schemaJson: Record<string, unknown>, address: string);
19
+ /**
20
+ * Cache the public key recovered from a signature.
21
+ */
22
+ private cachePublicKey;
23
+ /**
24
+ * Get the public key. Requires sign() to be called first to recover the key from a signature.
25
+ */
26
+ publicKey(): Promise<Uint8Array>;
27
+ /**
28
+ * Sign an UnsignedTransaction using EIP-712 typed data signing.
29
+ */
30
+ sign(message: Uint8Array): Promise<Uint8Array>;
31
+ }
32
+
33
+ export { Eip712Signer, Signer };
package/dist/wasm.js ADDED
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var __async = (__this, __arguments, generator) => {
30
+ return new Promise((resolve, reject) => {
31
+ var fulfilled = (value) => {
32
+ try {
33
+ step(generator.next(value));
34
+ } catch (e) {
35
+ reject(e);
36
+ }
37
+ };
38
+ var rejected = (value) => {
39
+ try {
40
+ step(generator.throw(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
46
+ step((generator = generator.apply(__this, __arguments)).next());
47
+ });
48
+ };
49
+
50
+ // src/wasm.ts
51
+ var wasm_exports = {};
52
+ __export(wasm_exports, {
53
+ Eip712Signer: () => Eip712Signer
54
+ });
55
+ module.exports = __toCommonJS(wasm_exports);
56
+
57
+ // src/wasm/eip712.ts
58
+ var secp = __toESM(require("@noble/secp256k1"));
59
+ var import_universal_wallet_wasm = require("@sovereign-sdk/universal-wallet-wasm");
60
+ var import_utils = require("@sovereign-sdk/utils");
61
+ var import_ethers = require("ethers");
62
+
63
+ // src/errors.ts
64
+ var SignerError = class extends Error {
65
+ /**
66
+ * Create a new SignerError.
67
+ *
68
+ * @param message - The error message
69
+ * @param signerId - The ID of the signer that threw the error
70
+ */
71
+ constructor(message, signerId) {
72
+ super(`[${signerId}] ${message}`);
73
+ this.signerId = signerId;
74
+ this.name = this.constructor.name;
75
+ this.stack = new Error().stack;
76
+ }
77
+ };
78
+
79
+ // src/wasm/eip712.ts
80
+ var _Eip712Signer = class _Eip712Signer {
81
+ constructor(provider, schemaJson, address) {
82
+ if (!address) {
83
+ throw new SignerError("Address is required", _Eip712Signer.SIGNER_ID);
84
+ }
85
+ this.provider = provider;
86
+ this.schema = import_universal_wallet_wasm.Schema.fromJSON(JSON.stringify(schemaJson));
87
+ this.address = address;
88
+ }
89
+ /**
90
+ * Cache the public key recovered from a signature.
91
+ */
92
+ cachePublicKey(signingHash, signature) {
93
+ if (this.cachedPublicKey) return;
94
+ let secpSig = secp.Signature.fromCompact(
95
+ new Uint8Array([
96
+ ...(0, import_utils.hexToBytes)(signature.r.slice(2)),
97
+ ...(0, import_utils.hexToBytes)(signature.s.slice(2))
98
+ ])
99
+ );
100
+ secpSig = secpSig.addRecoveryBit(signature.yParity);
101
+ const publicKey = secpSig.recoverPublicKey(signingHash);
102
+ this.cachedPublicKey = publicKey.toBytes(true);
103
+ }
104
+ /**
105
+ * Get the public key. Requires sign() to be called first to recover the key from a signature.
106
+ */
107
+ publicKey() {
108
+ return __async(this, null, function* () {
109
+ if (!this.cachedPublicKey) {
110
+ throw new SignerError(
111
+ "Public key was not available, you must call sign() first",
112
+ _Eip712Signer.SIGNER_ID
113
+ );
114
+ }
115
+ return this.cachedPublicKey;
116
+ });
117
+ }
118
+ /**
119
+ * Sign an UnsignedTransaction using EIP-712 typed data signing.
120
+ */
121
+ sign(message) {
122
+ return __async(this, null, function* () {
123
+ const address = this.address;
124
+ if (message.length < 32) {
125
+ throw new SignerError(
126
+ "Message too short, expected at least 32 bytes for chain hash",
127
+ _Eip712Signer.SIGNER_ID
128
+ );
129
+ }
130
+ const unsignedTxBytes = message.slice(0, -32);
131
+ const typeIndex = this.schema.knownTypeIndex(
132
+ import_universal_wallet_wasm.KnownTypeId.UnsignedTransaction
133
+ );
134
+ let eip712Json;
135
+ try {
136
+ eip712Json = this.schema.eip712Json(typeIndex, unsignedTxBytes);
137
+ } catch (error) {
138
+ throw new SignerError(
139
+ `Failed to generate EIP-712 JSON from message: ${error}`,
140
+ _Eip712Signer.SIGNER_ID
141
+ );
142
+ }
143
+ let signingHash;
144
+ try {
145
+ signingHash = this.schema.eip712SigningHash(typeIndex, unsignedTxBytes);
146
+ } catch (error) {
147
+ throw new SignerError(
148
+ `Failed to generate EIP-712 signing hash: ${error}`,
149
+ _Eip712Signer.SIGNER_ID
150
+ );
151
+ }
152
+ let signatureHex;
153
+ try {
154
+ signatureHex = yield this.provider.request({
155
+ method: "eth_signTypedData_v4",
156
+ params: [address, eip712Json]
157
+ });
158
+ } catch (error) {
159
+ throw new SignerError(
160
+ `Failed to sign with EIP-712: ${error}`,
161
+ _Eip712Signer.SIGNER_ID
162
+ );
163
+ }
164
+ const signature = import_ethers.Signature.from(signatureHex);
165
+ this.cachePublicKey(signingHash, signature);
166
+ return (0, import_utils.hexToBytes)(signature.compactSerialized.slice(2));
167
+ });
168
+ }
169
+ };
170
+ _Eip712Signer.SIGNER_ID = "Eip712";
171
+ var Eip712Signer = _Eip712Signer;
172
+ // Annotate the CommonJS export names for ESM import in node:
173
+ 0 && (module.exports = {
174
+ Eip712Signer
175
+ });
176
+ //# sourceMappingURL=wasm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/wasm.ts","../src/wasm/eip712.ts","../src/errors.ts"],"sourcesContent":["export { type Signer } from \"./signer\";\nexport * from \"./wasm/eip712\";\n","import type { MetaMaskInpageProvider } from \"@metamask/providers\";\nimport * as secp from \"@noble/secp256k1\";\nimport { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { hexToBytes } from \"@sovereign-sdk/utils\";\nimport { Signature } from \"ethers\";\nimport { SignerError } from \"../errors\";\nimport type { Signer } from \"../signer\";\n\n/**\n * EIP-712 signer implementation that uses MetaMask's eth_signTypedData_v4 method.\n *\n * This signer expects the message to be a valid UnsignedTransaction that can be\n * parsed by the schema's eip712Json() method.\n *\n * The signer uses the provided address for signing operations.\n */\nexport class Eip712Signer implements Signer {\n private readonly provider: MetaMaskInpageProvider;\n private readonly schema: Schema;\n private readonly address: string;\n private cachedPublicKey?: Uint8Array;\n private static readonly SIGNER_ID = \"Eip712\";\n\n constructor(\n provider: MetaMaskInpageProvider,\n schemaJson: Record<string, unknown>,\n address: string,\n ) {\n if (!address) {\n throw new SignerError(\"Address is required\", Eip712Signer.SIGNER_ID);\n }\n this.provider = provider;\n this.schema = Schema.fromJSON(JSON.stringify(schemaJson));\n this.address = address;\n }\n\n /**\n * Cache the public key recovered from a signature.\n */\n private cachePublicKey(signingHash: Uint8Array, signature: Signature): void {\n if (this.cachedPublicKey) return;\n\n let secpSig = secp.Signature.fromCompact(\n new Uint8Array([\n ...hexToBytes(signature.r.slice(2)),\n ...hexToBytes(signature.s.slice(2)),\n ]),\n );\n secpSig = secpSig.addRecoveryBit(signature.yParity);\n const publicKey = secpSig.recoverPublicKey(signingHash);\n\n this.cachedPublicKey = publicKey.toBytes(true); // compressed form\n }\n\n /**\n * Get the public key. Requires sign() to be called first to recover the key from a signature.\n */\n async publicKey(): Promise<Uint8Array> {\n if (!this.cachedPublicKey) {\n throw new SignerError(\n \"Public key was not available, you must call sign() first\",\n Eip712Signer.SIGNER_ID,\n );\n }\n\n return this.cachedPublicKey;\n }\n\n /**\n * Sign an UnsignedTransaction using EIP-712 typed data signing.\n */\n async sign(message: Uint8Array): Promise<Uint8Array> {\n const address = this.address;\n\n // Rollups invoke the signer with the chain hash appended, so drop the last 32 bytes of the message\n if (message.length < 32) {\n throw new SignerError(\n \"Message too short, expected at least 32 bytes for chain hash\",\n Eip712Signer.SIGNER_ID,\n );\n }\n const unsignedTxBytes = message.slice(0, -32);\n\n // Get the UnsignedTransaction type index\n const typeIndex = this.schema.knownTypeIndex(\n KnownTypeId.UnsignedTransaction,\n );\n\n // Generate the EIP-712 JSON from the message bytes\n let eip712Json: string;\n try {\n eip712Json = this.schema.eip712Json(typeIndex, unsignedTxBytes);\n } catch (error) {\n throw new SignerError(\n `Failed to generate EIP-712 JSON from message: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n // Get the EIP-712 signing hash for public key recovery\n let signingHash: Uint8Array;\n try {\n signingHash = this.schema.eip712SigningHash(typeIndex, unsignedTxBytes);\n } catch (error) {\n throw new SignerError(\n `Failed to generate EIP-712 signing hash: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n let signatureHex: string;\n try {\n signatureHex = (await this.provider.request({\n method: \"eth_signTypedData_v4\",\n params: [address, eip712Json],\n })) as string;\n } catch (error) {\n throw new SignerError(\n `Failed to sign with EIP-712: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n // Parse the signature for public key recovery\n const signature = Signature.from(signatureHex);\n this.cachePublicKey(signingHash, signature);\n\n // Return the standard compact serialized signature (r + s, 64 bytes)\n return hexToBytes(signature.compactSerialized.slice(2));\n }\n}\n","/**\n * An error that can be thrown by a signer.\n */\nexport class SignerError extends Error {\n public readonly signerId: string;\n\n /**\n * Create a new SignerError.\n *\n * @param message - The error message\n * @param signerId - The ID of the signer that threw the error\n */\n constructor(message: string, signerId: string) {\n super(`[${signerId}] ${message}`);\n\n this.signerId = signerId;\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,WAAsB;AACtB,mCAAoC;AACpC,mBAA2B;AAC3B,oBAA0B;;;ACDnB,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA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,MAAM,GAAG,GAAG;AAG5C,YAAM,YAAY,KAAK,OAAO;AAAA,QAC5B,yCAAY;AAAA,MACd;AAGA,UAAI;AACJ,UAAI;AACF,qBAAa,KAAK,OAAO,WAAW,WAAW,eAAe;AAAA,MAChE,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,iDAAiD,KAAK;AAAA,UACtD,cAAa;AAAA,QACf;AAAA,MACF;AAGA,UAAI;AACJ,UAAI;AACF,sBAAc,KAAK,OAAO,kBAAkB,WAAW,eAAe;AAAA,MACxE,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,4CAA4C,KAAK;AAAA,UACjD,cAAa;AAAA,QACf;AAAA,MACF;AAEA,UAAI;AACJ,UAAI;AACF,uBAAgB,MAAM,KAAK,SAAS,QAAQ;AAAA,UAC1C,QAAQ;AAAA,UACR,QAAQ,CAAC,SAAS,UAAU;AAAA,QAC9B,CAAC;AAAA,MACH,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,gCAAgC,KAAK;AAAA,UACrC,cAAa;AAAA,QACf;AAAA,MACF;AAGA,YAAM,YAAY,wBAAU,KAAK,YAAY;AAC7C,WAAK,eAAe,aAAa,SAAS;AAG1C,iBAAO,yBAAW,UAAU,kBAAkB,MAAM,CAAC,CAAC;AAAA,IACxD;AAAA;AACF;AAlHa,cAKa,YAAY;AAL/B,IAAM,eAAN;","names":[]}
package/dist/wasm.mjs ADDED
@@ -0,0 +1,140 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/wasm/eip712.ts
23
+ import * as secp from "@noble/secp256k1";
24
+ import { KnownTypeId, Schema } from "@sovereign-sdk/universal-wallet-wasm";
25
+ import { hexToBytes } from "@sovereign-sdk/utils";
26
+ import { Signature as Signature2 } from "ethers";
27
+
28
+ // src/errors.ts
29
+ var SignerError = class extends Error {
30
+ /**
31
+ * Create a new SignerError.
32
+ *
33
+ * @param message - The error message
34
+ * @param signerId - The ID of the signer that threw the error
35
+ */
36
+ constructor(message, signerId) {
37
+ super(`[${signerId}] ${message}`);
38
+ this.signerId = signerId;
39
+ this.name = this.constructor.name;
40
+ this.stack = new Error().stack;
41
+ }
42
+ };
43
+
44
+ // src/wasm/eip712.ts
45
+ var _Eip712Signer = class _Eip712Signer {
46
+ constructor(provider, schemaJson, address) {
47
+ if (!address) {
48
+ throw new SignerError("Address is required", _Eip712Signer.SIGNER_ID);
49
+ }
50
+ this.provider = provider;
51
+ this.schema = Schema.fromJSON(JSON.stringify(schemaJson));
52
+ this.address = address;
53
+ }
54
+ /**
55
+ * Cache the public key recovered from a signature.
56
+ */
57
+ cachePublicKey(signingHash, signature) {
58
+ if (this.cachedPublicKey) return;
59
+ let secpSig = secp.Signature.fromCompact(
60
+ new Uint8Array([
61
+ ...hexToBytes(signature.r.slice(2)),
62
+ ...hexToBytes(signature.s.slice(2))
63
+ ])
64
+ );
65
+ secpSig = secpSig.addRecoveryBit(signature.yParity);
66
+ const publicKey = secpSig.recoverPublicKey(signingHash);
67
+ this.cachedPublicKey = publicKey.toBytes(true);
68
+ }
69
+ /**
70
+ * Get the public key. Requires sign() to be called first to recover the key from a signature.
71
+ */
72
+ publicKey() {
73
+ return __async(this, null, function* () {
74
+ if (!this.cachedPublicKey) {
75
+ throw new SignerError(
76
+ "Public key was not available, you must call sign() first",
77
+ _Eip712Signer.SIGNER_ID
78
+ );
79
+ }
80
+ return this.cachedPublicKey;
81
+ });
82
+ }
83
+ /**
84
+ * Sign an UnsignedTransaction using EIP-712 typed data signing.
85
+ */
86
+ sign(message) {
87
+ return __async(this, null, function* () {
88
+ const address = this.address;
89
+ if (message.length < 32) {
90
+ throw new SignerError(
91
+ "Message too short, expected at least 32 bytes for chain hash",
92
+ _Eip712Signer.SIGNER_ID
93
+ );
94
+ }
95
+ const unsignedTxBytes = message.slice(0, -32);
96
+ const typeIndex = this.schema.knownTypeIndex(
97
+ KnownTypeId.UnsignedTransaction
98
+ );
99
+ let eip712Json;
100
+ try {
101
+ eip712Json = this.schema.eip712Json(typeIndex, unsignedTxBytes);
102
+ } catch (error) {
103
+ throw new SignerError(
104
+ `Failed to generate EIP-712 JSON from message: ${error}`,
105
+ _Eip712Signer.SIGNER_ID
106
+ );
107
+ }
108
+ let signingHash;
109
+ try {
110
+ signingHash = this.schema.eip712SigningHash(typeIndex, unsignedTxBytes);
111
+ } catch (error) {
112
+ throw new SignerError(
113
+ `Failed to generate EIP-712 signing hash: ${error}`,
114
+ _Eip712Signer.SIGNER_ID
115
+ );
116
+ }
117
+ let signatureHex;
118
+ try {
119
+ signatureHex = yield this.provider.request({
120
+ method: "eth_signTypedData_v4",
121
+ params: [address, eip712Json]
122
+ });
123
+ } catch (error) {
124
+ throw new SignerError(
125
+ `Failed to sign with EIP-712: ${error}`,
126
+ _Eip712Signer.SIGNER_ID
127
+ );
128
+ }
129
+ const signature = Signature2.from(signatureHex);
130
+ this.cachePublicKey(signingHash, signature);
131
+ return hexToBytes(signature.compactSerialized.slice(2));
132
+ });
133
+ }
134
+ };
135
+ _Eip712Signer.SIGNER_ID = "Eip712";
136
+ var Eip712Signer = _Eip712Signer;
137
+ export {
138
+ Eip712Signer
139
+ };
140
+ //# sourceMappingURL=wasm.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/wasm/eip712.ts","../src/errors.ts"],"sourcesContent":["import type { MetaMaskInpageProvider } from \"@metamask/providers\";\nimport * as secp from \"@noble/secp256k1\";\nimport { KnownTypeId, Schema } from \"@sovereign-sdk/universal-wallet-wasm\";\nimport { hexToBytes } from \"@sovereign-sdk/utils\";\nimport { Signature } from \"ethers\";\nimport { SignerError } from \"../errors\";\nimport type { Signer } from \"../signer\";\n\n/**\n * EIP-712 signer implementation that uses MetaMask's eth_signTypedData_v4 method.\n *\n * This signer expects the message to be a valid UnsignedTransaction that can be\n * parsed by the schema's eip712Json() method.\n *\n * The signer uses the provided address for signing operations.\n */\nexport class Eip712Signer implements Signer {\n private readonly provider: MetaMaskInpageProvider;\n private readonly schema: Schema;\n private readonly address: string;\n private cachedPublicKey?: Uint8Array;\n private static readonly SIGNER_ID = \"Eip712\";\n\n constructor(\n provider: MetaMaskInpageProvider,\n schemaJson: Record<string, unknown>,\n address: string,\n ) {\n if (!address) {\n throw new SignerError(\"Address is required\", Eip712Signer.SIGNER_ID);\n }\n this.provider = provider;\n this.schema = Schema.fromJSON(JSON.stringify(schemaJson));\n this.address = address;\n }\n\n /**\n * Cache the public key recovered from a signature.\n */\n private cachePublicKey(signingHash: Uint8Array, signature: Signature): void {\n if (this.cachedPublicKey) return;\n\n let secpSig = secp.Signature.fromCompact(\n new Uint8Array([\n ...hexToBytes(signature.r.slice(2)),\n ...hexToBytes(signature.s.slice(2)),\n ]),\n );\n secpSig = secpSig.addRecoveryBit(signature.yParity);\n const publicKey = secpSig.recoverPublicKey(signingHash);\n\n this.cachedPublicKey = publicKey.toBytes(true); // compressed form\n }\n\n /**\n * Get the public key. Requires sign() to be called first to recover the key from a signature.\n */\n async publicKey(): Promise<Uint8Array> {\n if (!this.cachedPublicKey) {\n throw new SignerError(\n \"Public key was not available, you must call sign() first\",\n Eip712Signer.SIGNER_ID,\n );\n }\n\n return this.cachedPublicKey;\n }\n\n /**\n * Sign an UnsignedTransaction using EIP-712 typed data signing.\n */\n async sign(message: Uint8Array): Promise<Uint8Array> {\n const address = this.address;\n\n // Rollups invoke the signer with the chain hash appended, so drop the last 32 bytes of the message\n if (message.length < 32) {\n throw new SignerError(\n \"Message too short, expected at least 32 bytes for chain hash\",\n Eip712Signer.SIGNER_ID,\n );\n }\n const unsignedTxBytes = message.slice(0, -32);\n\n // Get the UnsignedTransaction type index\n const typeIndex = this.schema.knownTypeIndex(\n KnownTypeId.UnsignedTransaction,\n );\n\n // Generate the EIP-712 JSON from the message bytes\n let eip712Json: string;\n try {\n eip712Json = this.schema.eip712Json(typeIndex, unsignedTxBytes);\n } catch (error) {\n throw new SignerError(\n `Failed to generate EIP-712 JSON from message: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n // Get the EIP-712 signing hash for public key recovery\n let signingHash: Uint8Array;\n try {\n signingHash = this.schema.eip712SigningHash(typeIndex, unsignedTxBytes);\n } catch (error) {\n throw new SignerError(\n `Failed to generate EIP-712 signing hash: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n let signatureHex: string;\n try {\n signatureHex = (await this.provider.request({\n method: \"eth_signTypedData_v4\",\n params: [address, eip712Json],\n })) as string;\n } catch (error) {\n throw new SignerError(\n `Failed to sign with EIP-712: ${error}`,\n Eip712Signer.SIGNER_ID,\n );\n }\n\n // Parse the signature for public key recovery\n const signature = Signature.from(signatureHex);\n this.cachePublicKey(signingHash, signature);\n\n // Return the standard compact serialized signature (r + s, 64 bytes)\n return hexToBytes(signature.compactSerialized.slice(2));\n }\n}\n","/**\n * An error that can be thrown by a signer.\n */\nexport class SignerError extends Error {\n public readonly signerId: string;\n\n /**\n * Create a new SignerError.\n *\n * @param message - The error message\n * @param signerId - The ID of the signer that threw the error\n */\n constructor(message: string, signerId: string) {\n super(`[${signerId}] ${message}`);\n\n this.signerId = signerId;\n this.name = this.constructor.name;\n this.stack = new Error().stack;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,YAAY,UAAU;AACtB,SAAS,aAAa,cAAc;AACpC,SAAS,kBAAkB;AAC3B,SAAS,aAAAA,kBAAiB;;;ACDnB,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASrC,YAAY,SAA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AAAA,QAC5B,YAAY;AAAA,MACd;AAGA,UAAI;AACJ,UAAI;AACF,qBAAa,KAAK,OAAO,WAAW,WAAW,eAAe;AAAA,MAChE,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,iDAAiD,KAAK;AAAA,UACtD,cAAa;AAAA,QACf;AAAA,MACF;AAGA,UAAI;AACJ,UAAI;AACF,sBAAc,KAAK,OAAO,kBAAkB,WAAW,eAAe;AAAA,MACxE,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,4CAA4C,KAAK;AAAA,UACjD,cAAa;AAAA,QACf;AAAA,MACF;AAEA,UAAI;AACJ,UAAI;AACF,uBAAgB,MAAM,KAAK,SAAS,QAAQ;AAAA,UAC1C,QAAQ;AAAA,UACR,QAAQ,CAAC,SAAS,UAAU;AAAA,QAC9B,CAAC;AAAA,MACH,SAAS,OAAO;AACd,cAAM,IAAI;AAAA,UACR,gCAAgC,KAAK;AAAA,UACrC,cAAa;AAAA,QACf;AAAA,MACF;AAGA,YAAM,YAAYC,WAAU,KAAK,YAAY;AAC7C,WAAK,eAAe,aAAa,SAAS;AAG1C,aAAO,WAAW,UAAU,kBAAkB,MAAM,CAAC,CAAC;AAAA,IACxD;AAAA;AACF;AAlHa,cAKa,YAAY;AAL/B,IAAM,eAAN;","names":["Signature","Signature"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sovereign-sdk/signers",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Signer interface and default implementations for use with Sovereign SDK applications",
5
5
  "repository": "github:Sovereign-Labs/sovereign-sdk-web3-js",
6
6
  "files": [
@@ -13,6 +13,11 @@
13
13
  "types": "./dist/index.d.ts",
14
14
  "require": "./dist/index.js",
15
15
  "import": "./dist/index.mjs"
16
+ },
17
+ "./wasm": {
18
+ "types": "./dist/wasm.d.ts",
19
+ "require": "./dist/wasm.js",
20
+ "import": "./dist/wasm.mjs"
16
21
  }
17
22
  },
18
23
  "main": "./dist/index.js",
@@ -36,6 +41,7 @@
36
41
  "@noble/hashes": "^1.5.0",
37
42
  "@noble/secp256k1": "^2.3.0",
38
43
  "ethers": "^6.15.0",
44
+ "@sovereign-sdk/universal-wallet-wasm": "^0.5.0",
39
45
  "@sovereign-sdk/utils": "^0.0.4"
40
46
  },
41
47
  "scripts": {