@getpara/ethers-v5-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,107 @@
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.ParaEthersV5Signer = void 0;
13
+ const ethers_1 = require("ethers");
14
+ const hash_1 = require("@ethersproject/hash");
15
+ const core_sdk_1 = require("@getpara/core-sdk");
16
+ const utils_1 = require("ethers/lib/utils");
17
+ class ParaEthersV5Signer extends ethers_1.ethers.Signer {
18
+ constructor(para, provider, walletId) {
19
+ super();
20
+ this.currentWalletId = para.findWalletId(walletId, { type: ['EVM'] });
21
+ this.para = para;
22
+ (0, utils_1.defineReadOnly)(this, 'provider', provider);
23
+ }
24
+ setCurrentWalletId(walletId) {
25
+ if (!this.para.wallets[walletId]) {
26
+ throw new Error(`no wallet exists with id ${walletId}`);
27
+ }
28
+ this.currentWalletId = walletId;
29
+ }
30
+ getCurrentWalletId() {
31
+ const id = this.currentWalletId;
32
+ if (!id) {
33
+ throw new Error(`no wallet available`);
34
+ }
35
+ if (!this.para.wallets[id]) {
36
+ throw new Error(`no wallet exists with id ${id}`);
37
+ }
38
+ return id;
39
+ }
40
+ getAddress() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const walletId = this.getCurrentWalletId();
43
+ if (!walletId) {
44
+ throw new Error('no wallet available');
45
+ }
46
+ return this.para.wallets[walletId].address;
47
+ });
48
+ }
49
+ connect(provider) {
50
+ return new ParaEthersV5Signer(this.para, provider, this.currentWalletId);
51
+ }
52
+ signMessage(message) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const hashedMessage = ethers_1.ethers.utils.hashMessage(message);
55
+ const base64HashedMessage = (0, core_sdk_1.hexStringToBase64)(hashedMessage);
56
+ const res = yield this.para.signMessage({ walletId: this.getCurrentWalletId(), messageBase64: base64HashedMessage });
57
+ const signature = res.signature;
58
+ return `0x${signature}`;
59
+ });
60
+ }
61
+ signTransaction(tx) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ return (0, utils_1.resolveProperties)(tx).then((tx) => __awaiter(this, void 0, void 0, function* () {
64
+ if (tx.from != null) {
65
+ if ((yield this.getAddress()).toLowerCase() !== ethers_1.ethers.utils.getAddress(tx.from).toLowerCase()) {
66
+ throw new Error('transaction from address mismatch');
67
+ }
68
+ delete tx.from;
69
+ }
70
+ let txObj = tx;
71
+ txObj.r = '0x0';
72
+ txObj.s = '0x0';
73
+ txObj.v = 0;
74
+ const serializedTx = (0, utils_1.serializeTransaction)(txObj);
75
+ const message = (0, core_sdk_1.hexStringToBase64)((0, utils_1.keccak256)(serializedTx));
76
+ const signature = yield this.signMessage(message);
77
+ return (0, utils_1.serializeTransaction)(txObj, signature);
78
+ }));
79
+ });
80
+ }
81
+ signTypedData(domain, types, value) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const populated = yield hash_1._TypedDataEncoder.resolveNames(domain, types, value, (name) => __awaiter(this, void 0, void 0, function* () {
84
+ if (!this.provider) {
85
+ throw new Error('cannot resolve ENS names without a provider');
86
+ }
87
+ const address = yield this.provider.resolveName(name);
88
+ if (!address) {
89
+ throw new Error('unconfigured ENS name');
90
+ }
91
+ return address;
92
+ }));
93
+ const res = yield this.para.signMessage({
94
+ walletId: this.getCurrentWalletId(),
95
+ messageBase64: (0, core_sdk_1.hexStringToBase64)(hash_1._TypedDataEncoder.hash(populated.domain, types, populated.value)),
96
+ });
97
+ const signature = res.signature;
98
+ return `0x${signature}`;
99
+ });
100
+ }
101
+ _signTypedData(domain, types, value) {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ return this.signTypedData(domain, types, value);
104
+ });
105
+ }
106
+ }
107
+ exports.ParaEthersV5Signer = ParaEthersV5Signer;
@@ -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,103 @@
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 { _TypedDataEncoder } from '@ethersproject/hash';
12
+ import { hexStringToBase64 } from '@getpara/core-sdk';
13
+ import { defineReadOnly, keccak256, resolveProperties, serializeTransaction } from 'ethers/lib/utils';
14
+ export class ParaEthersV5Signer extends ethers.Signer {
15
+ constructor(para, provider, walletId) {
16
+ super();
17
+ this.currentWalletId = para.findWalletId(walletId, { type: ['EVM'] });
18
+ this.para = para;
19
+ defineReadOnly(this, 'provider', provider);
20
+ }
21
+ setCurrentWalletId(walletId) {
22
+ if (!this.para.wallets[walletId]) {
23
+ throw new Error(`no wallet exists with id ${walletId}`);
24
+ }
25
+ this.currentWalletId = walletId;
26
+ }
27
+ getCurrentWalletId() {
28
+ const id = this.currentWalletId;
29
+ if (!id) {
30
+ throw new Error(`no wallet available`);
31
+ }
32
+ if (!this.para.wallets[id]) {
33
+ throw new Error(`no wallet exists with id ${id}`);
34
+ }
35
+ return id;
36
+ }
37
+ getAddress() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const walletId = this.getCurrentWalletId();
40
+ if (!walletId) {
41
+ throw new Error('no wallet available');
42
+ }
43
+ return this.para.wallets[walletId].address;
44
+ });
45
+ }
46
+ connect(provider) {
47
+ return new ParaEthersV5Signer(this.para, provider, this.currentWalletId);
48
+ }
49
+ signMessage(message) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const hashedMessage = ethers.utils.hashMessage(message);
52
+ const base64HashedMessage = hexStringToBase64(hashedMessage);
53
+ const res = yield this.para.signMessage({ walletId: this.getCurrentWalletId(), messageBase64: base64HashedMessage });
54
+ const signature = res.signature;
55
+ return `0x${signature}`;
56
+ });
57
+ }
58
+ signTransaction(tx) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return resolveProperties(tx).then((tx) => __awaiter(this, void 0, void 0, function* () {
61
+ if (tx.from != null) {
62
+ if ((yield this.getAddress()).toLowerCase() !== ethers.utils.getAddress(tx.from).toLowerCase()) {
63
+ throw new Error('transaction from address mismatch');
64
+ }
65
+ delete tx.from;
66
+ }
67
+ let txObj = tx;
68
+ txObj.r = '0x0';
69
+ txObj.s = '0x0';
70
+ txObj.v = 0;
71
+ const serializedTx = serializeTransaction(txObj);
72
+ const message = hexStringToBase64(keccak256(serializedTx));
73
+ const signature = yield this.signMessage(message);
74
+ return serializeTransaction(txObj, signature);
75
+ }));
76
+ });
77
+ }
78
+ signTypedData(domain, types, value) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ const populated = yield _TypedDataEncoder.resolveNames(domain, types, value, (name) => __awaiter(this, void 0, void 0, function* () {
81
+ if (!this.provider) {
82
+ throw new Error('cannot resolve ENS names without a provider');
83
+ }
84
+ const address = yield this.provider.resolveName(name);
85
+ if (!address) {
86
+ throw new Error('unconfigured ENS name');
87
+ }
88
+ return address;
89
+ }));
90
+ const res = yield this.para.signMessage({
91
+ walletId: this.getCurrentWalletId(),
92
+ messageBase64: hexStringToBase64(_TypedDataEncoder.hash(populated.domain, types, populated.value)),
93
+ });
94
+ const signature = res.signature;
95
+ return `0x${signature}`;
96
+ });
97
+ }
98
+ _signTypedData(domain, types, value) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ return this.signTypedData(domain, types, value);
101
+ });
102
+ }
103
+ }
@@ -0,0 +1 @@
1
+ export * from './ethersSigner.js';
@@ -0,0 +1 @@
1
+ {"type":"module","sideEffects":false}
@@ -0,0 +1,15 @@
1
+ import { ethers } from 'ethers';
2
+ import ParaCore from '@getpara/core-sdk';
3
+ export declare class ParaEthersV5Signer extends ethers.Signer {
4
+ private para;
5
+ private currentWalletId;
6
+ constructor(para: ParaCore, provider?: null | ethers.providers.Provider, walletId?: string);
7
+ setCurrentWalletId(walletId: string): void;
8
+ getCurrentWalletId(): string;
9
+ getAddress(): Promise<string>;
10
+ connect(provider: ethers.providers.Provider | null): ParaEthersV5Signer;
11
+ signMessage(message: string | Uint8Array): Promise<string>;
12
+ signTransaction(tx: ethers.providers.TransactionRequest): Promise<string>;
13
+ signTypedData(domain: ethers.TypedDataDomain, types: Record<string, Array<ethers.TypedDataField>>, value: Record<string, any>): Promise<string>;
14
+ _signTypedData(domain: ethers.TypedDataDomain, types: Record<string, Array<ethers.TypedDataField>>, value: Record<string, any>): Promise<string>;
15
+ }
@@ -0,0 +1 @@
1
+ export * from './ethersSigner.js';
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@getpara/ethers-v5-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
+ "ethers": "^5.7.2"
12
+ },
13
+ "scripts": {
14
+ "build": "yarn build:cjs && yarn build:esm && yarn build:types",
15
+ "build:cjs": "rm -rf dist/cjs && tsc --module commonjs --outDir dist/cjs && printf '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
16
+ "build:esm": "rm -rf dist/esm && tsc --module es6 --outDir dist/esm && printf '{\"type\":\"module\",\"sideEffects\":false}' > dist/esm/package.json",
17
+ "build:types": "rm -rf dist/types && tsc --module es6 --declarationDir dist/types --emitDeclarationOnly --declaration"
18
+ },
19
+ "devDependencies": {
20
+ "typescript": "5.1.6"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "package.json"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/esm/index.js",
29
+ "require": "./dist/cjs/index.js",
30
+ "types": "./dist/types/index.d.ts"
31
+ }
32
+ },
33
+ "gitHead": "625aaa94001a5461dcde8d6775c3b73f3862c76c"
34
+ }