@getpara/ethers-v6-integration 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.
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ParaEthersSigner = void 0;
13
+ const ethers_1 = require("ethers");
14
+ const core_sdk_1 = require("@getpara/core-sdk");
15
+ class ParaEthersSigner extends ethers_1.ethers.AbstractSigner {
16
+ /**
17
+ * Signs a message.
18
+ *
19
+ * @param para - the ParaCore instance
20
+ * @param provider - the ethers provider to use. If not present, will use the default ethers.Provider.
21
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
22
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
23
+ **/
24
+ constructor(para, provider, walletId, messageSigningTimeoutMs) {
25
+ super(provider);
26
+ this.currentWalletId = para.findWalletId(walletId, { type: ['EVM'] });
27
+ this.para = para;
28
+ this.messageSigningTimeoutMs = messageSigningTimeoutMs;
29
+ }
30
+ getAddress() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ return this.para.wallets[this.currentWalletId].address;
33
+ });
34
+ }
35
+ connect(provider) {
36
+ return new ParaEthersSigner(this.para, provider, this.currentWalletId);
37
+ }
38
+ /**
39
+ * Signs a message.
40
+ *
41
+ * @param message - the message to be signed
42
+ **/
43
+ signMessage(message) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const hashedMessage = ethers_1.ethers.hashMessage(message);
46
+ const base64HashedMessage = (0, core_sdk_1.hexStringToBase64)(hashedMessage);
47
+ const res = yield this.para.signMessage({
48
+ walletId: this.currentWalletId,
49
+ messageBase64: base64HashedMessage,
50
+ timeoutMs: this.messageSigningTimeoutMs,
51
+ });
52
+ const signature = res.signature;
53
+ return `0x${signature}`;
54
+ });
55
+ }
56
+ validateTx(tx) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ const { to, from } = yield ethers_1.ethers.resolveProperties({
59
+ to: tx.to ? ethers_1.ethers.resolveAddress(tx.to, this.provider) : undefined,
60
+ from: tx.from ? ethers_1.ethers.resolveAddress(tx.from, this.provider) : undefined,
61
+ });
62
+ if (to) {
63
+ tx.to = to;
64
+ }
65
+ if (from) {
66
+ tx.from = from;
67
+ }
68
+ if (tx.from) {
69
+ ethers_1.ethers.assertArgument(ethers_1.ethers.getAddress(tx.from).toLowerCase() === (yield this.getAddress()).toLowerCase(), 'transaction from address mismatch', 'tx.from', tx.from);
70
+ delete tx.from;
71
+ }
72
+ return ethers_1.ethers.Transaction.from(tx);
73
+ });
74
+ }
75
+ signTransaction(tx) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const txObj = yield this.validateTx(tx);
78
+ txObj.signature = {
79
+ r: '0x0',
80
+ s: '0x0',
81
+ v: 0,
82
+ };
83
+ const res = yield this.para.signTransaction({
84
+ walletId: this.currentWalletId,
85
+ rlpEncodedTxBase64: (0, core_sdk_1.hexStringToBase64)(txObj.serialized),
86
+ chainId: `${txObj.chainId}`,
87
+ });
88
+ const signature = res.signature;
89
+ const btx = ethers_1.ethers.Transaction.from(tx);
90
+ btx.signature = `0x${signature}`;
91
+ return btx.serialized;
92
+ });
93
+ }
94
+ signTypedData(domain, types, value) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ const populated = yield ethers_1.ethers.TypedDataEncoder.resolveNames(domain, types, value, (name) => __awaiter(this, void 0, void 0, function* () {
97
+ ethers_1.ethers.assert(this.provider != null, 'cannot resolve ENS names without a provider', 'UNSUPPORTED_OPERATION', {
98
+ operation: 'resolveName',
99
+ info: { name },
100
+ });
101
+ const address = yield this.provider.resolveName(name);
102
+ ethers_1.ethers.assert(address != null, 'unconfigured ENS name', 'UNCONFIGURED_NAME', {
103
+ value: name,
104
+ });
105
+ return address;
106
+ }));
107
+ const res = yield this.para.signMessage({
108
+ walletId: this.currentWalletId,
109
+ messageBase64: (0, core_sdk_1.hexStringToBase64)(ethers_1.ethers.TypedDataEncoder.hash(populated.domain, types, populated.value)),
110
+ });
111
+ const signature = res.signature;
112
+ return `0x${signature}`;
113
+ });
114
+ }
115
+ }
116
+ exports.ParaEthersSigner = ParaEthersSigner;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ethersSigner.js"), exports);
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,112 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { ethers } from 'ethers';
11
+ import { hexStringToBase64 } from '@getpara/core-sdk';
12
+ export class ParaEthersSigner extends ethers.AbstractSigner {
13
+ /**
14
+ * Signs a message.
15
+ *
16
+ * @param para - the ParaCore instance
17
+ * @param provider - the ethers provider to use. If not present, will use the default ethers.Provider.
18
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
19
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
20
+ **/
21
+ constructor(para, provider, walletId, messageSigningTimeoutMs) {
22
+ super(provider);
23
+ this.currentWalletId = para.findWalletId(walletId, { type: ['EVM'] });
24
+ this.para = para;
25
+ this.messageSigningTimeoutMs = messageSigningTimeoutMs;
26
+ }
27
+ getAddress() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ return this.para.wallets[this.currentWalletId].address;
30
+ });
31
+ }
32
+ connect(provider) {
33
+ return new ParaEthersSigner(this.para, provider, this.currentWalletId);
34
+ }
35
+ /**
36
+ * Signs a message.
37
+ *
38
+ * @param message - the message to be signed
39
+ **/
40
+ signMessage(message) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const hashedMessage = ethers.hashMessage(message);
43
+ const base64HashedMessage = hexStringToBase64(hashedMessage);
44
+ const res = yield this.para.signMessage({
45
+ walletId: this.currentWalletId,
46
+ messageBase64: base64HashedMessage,
47
+ timeoutMs: this.messageSigningTimeoutMs,
48
+ });
49
+ const signature = res.signature;
50
+ return `0x${signature}`;
51
+ });
52
+ }
53
+ validateTx(tx) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const { to, from } = yield ethers.resolveProperties({
56
+ to: tx.to ? ethers.resolveAddress(tx.to, this.provider) : undefined,
57
+ from: tx.from ? ethers.resolveAddress(tx.from, this.provider) : undefined,
58
+ });
59
+ if (to) {
60
+ tx.to = to;
61
+ }
62
+ if (from) {
63
+ tx.from = from;
64
+ }
65
+ if (tx.from) {
66
+ ethers.assertArgument(ethers.getAddress(tx.from).toLowerCase() === (yield this.getAddress()).toLowerCase(), 'transaction from address mismatch', 'tx.from', tx.from);
67
+ delete tx.from;
68
+ }
69
+ return ethers.Transaction.from(tx);
70
+ });
71
+ }
72
+ signTransaction(tx) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ const txObj = yield this.validateTx(tx);
75
+ txObj.signature = {
76
+ r: '0x0',
77
+ s: '0x0',
78
+ v: 0,
79
+ };
80
+ const res = yield this.para.signTransaction({
81
+ walletId: this.currentWalletId,
82
+ rlpEncodedTxBase64: hexStringToBase64(txObj.serialized),
83
+ chainId: `${txObj.chainId}`,
84
+ });
85
+ const signature = res.signature;
86
+ const btx = ethers.Transaction.from(tx);
87
+ btx.signature = `0x${signature}`;
88
+ return btx.serialized;
89
+ });
90
+ }
91
+ signTypedData(domain, types, value) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const populated = yield ethers.TypedDataEncoder.resolveNames(domain, types, value, (name) => __awaiter(this, void 0, void 0, function* () {
94
+ ethers.assert(this.provider != null, 'cannot resolve ENS names without a provider', 'UNSUPPORTED_OPERATION', {
95
+ operation: 'resolveName',
96
+ info: { name },
97
+ });
98
+ const address = yield this.provider.resolveName(name);
99
+ ethers.assert(address != null, 'unconfigured ENS name', 'UNCONFIGURED_NAME', {
100
+ value: name,
101
+ });
102
+ return address;
103
+ }));
104
+ const res = yield this.para.signMessage({
105
+ walletId: this.currentWalletId,
106
+ messageBase64: hexStringToBase64(ethers.TypedDataEncoder.hash(populated.domain, types, populated.value)),
107
+ });
108
+ const signature = res.signature;
109
+ return `0x${signature}`;
110
+ });
111
+ }
112
+ }
@@ -0,0 +1 @@
1
+ export * from './ethersSigner.js';
@@ -0,0 +1 @@
1
+ {"type":"module","sideEffects":false}
@@ -0,0 +1,27 @@
1
+ import { ethers } from 'ethers';
2
+ import ParaCore from '@getpara/core-sdk';
3
+ export declare class ParaEthersSigner extends ethers.AbstractSigner {
4
+ private para;
5
+ private currentWalletId;
6
+ private messageSigningTimeoutMs?;
7
+ /**
8
+ * Signs a message.
9
+ *
10
+ * @param para - the ParaCore instance
11
+ * @param provider - the ethers provider to use. If not present, will use the default ethers.Provider.
12
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
13
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
14
+ **/
15
+ constructor(para: ParaCore, provider?: null | ethers.Provider, walletId?: string, messageSigningTimeoutMs?: number);
16
+ getAddress(): Promise<string>;
17
+ connect(provider: ethers.Provider | null): ParaEthersSigner;
18
+ /**
19
+ * Signs a message.
20
+ *
21
+ * @param message - the message to be signed
22
+ **/
23
+ signMessage(message: string | Uint8Array): Promise<string>;
24
+ private validateTx;
25
+ signTransaction(tx: ethers.TransactionRequest): Promise<string>;
26
+ signTypedData(domain: ethers.TypedDataDomain, types: Record<string, Array<ethers.TypedDataField>>, value: Record<string, any>): Promise<string>;
27
+ }
@@ -0,0 +1 @@
1
+ export * from './ethersSigner.js';
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@getpara/ethers-v6-integration",
3
+ "version": "0.1.0",
4
+ "main": "dist/cjs/index.js",
5
+ "module": "dist/esm/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "typings": "dist/types/index.d.ts",
8
+ "sideEffects": false,
9
+ "dependencies": {
10
+ "@getpara/core-sdk": "0.1.0"
11
+ },
12
+ "scripts": {
13
+ "build": "yarn build:cjs && yarn build:esm && yarn build:types",
14
+ "build:cjs": "rm -rf dist/cjs && tsc --module commonjs --outDir dist/cjs && printf '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
15
+ "build:esm": "rm -rf dist/esm && tsc --module es6 --outDir dist/esm && printf '{\"type\":\"module\",\"sideEffects\":false}' > dist/esm/package.json",
16
+ "build:types": "rm -rf dist/types && tsc --module es6 --declarationDir dist/types --emitDeclarationOnly --declaration"
17
+ },
18
+ "devDependencies": {
19
+ "ethers": "6.x",
20
+ "typescript": "5.1.6"
21
+ },
22
+ "peerDependencies": {
23
+ "ethers": "6.x"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "package.json"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "import": "./dist/esm/index.js",
32
+ "require": "./dist/cjs/index.js",
33
+ "types": "./dist/types/index.d.ts"
34
+ }
35
+ },
36
+ "gitHead": "625aaa94001a5461dcde8d6775c3b73f3862c76c"
37
+ }