-
Notifications
You must be signed in to change notification settings - Fork 7
/
signer.service.ts
224 lines (195 loc) · 8.07 KB
/
signer.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { BigNumber, providers, utils, Wallet, ethers, Signer } from "ethers";
import base64url from "base64url";
import WalletConnectProvider from "@walletconnect/ethereum-provider";
import { Methods } from "@ew-did-registry/did";
import { ERROR_MESSAGES } from "../../errors/ErrorMessages";
import { chainConfigs } from "../../config/chain.config";
import { ExecutionEnvironment, executionEnvironment } from "../../utils/detectEnvironment";
import { IPubKeyAndIdentityToken, ProviderType, ProviderEvent, AccountInfo, PUBLIC_KEY } from "./signer.types";
import { EkcSigner } from "./ekcSigner";
const { arrayify, keccak256, recoverPublicKey, computeAddress, computePublicKey, getAddress, hashMessage } = utils;
export type ServiceInitializer = () => Promise<void>;
export class SignerService {
private _publicKey: string;
private _identityToken: string;
private _address: string;
private _account: string;
private _chainId: number;
private _chainName: string;
private _servicesInitializers: ServiceInitializer[] = [];
private _walletEventListeners: { event: ProviderEvent; cb: any }[] = [];
constructor(private _signer: Required<Signer>, private _providerType: ProviderType) {}
async init() {
if (executionEnvironment() === ExecutionEnvironment.BROWSER) {
this._publicKey = localStorage.getItem(PUBLIC_KEY) as string;
}
this._address = await this.signer.getAddress();
this._chainId = (await this._signer.provider.getNetwork()).chainId;
this._chainName = chainConfigs()[this._chainId].chainName;
if (this._signer instanceof providers.JsonRpcSigner) {
this._account = (await this._signer.provider.listAccounts())[0];
} else if (this._signer instanceof Wallet) {
this._account = this._address;
}
/**
* @todo provide general way to initialize with previously saved key
*/
this.initEventHandlers();
for await (const initializer of this._servicesInitializers) {
await initializer();
}
}
/**
* Registers reinitialization of dependent service on signer reconnection
*/
onInit(initializer: ServiceInitializer) {
this._servicesInitializers.push(initializer);
}
async emit(e: ProviderEvent) {
await Promise.all(
this._walletEventListeners
.map(({ event, cb }) => {
return e === event ? cb() : null;
})
.filter(Boolean),
);
}
on(event: ProviderEvent, cb) {
this._walletEventListeners.push({ event, cb });
}
/**
* Add event handler for certain events
* @requires to be called after the connection to wallet was initialized
*/
initEventHandlers() {
const accChangedHandler = async () => {
await this.closeConnection();
await this.init();
};
if (this._providerType === ProviderType.MetaMask) {
this.on(ProviderEvent.AccountChanged, accChangedHandler);
this.on(ProviderEvent.NetworkChanged, accChangedHandler);
} else if (this._providerType === ProviderType.WalletConnect) {
this.on(ProviderEvent.SessionUpdate, accChangedHandler);
this.on(ProviderEvent.Disconnected, this.closeConnection);
}
}
get signer() {
return this._signer;
}
get address() {
return this._address;
}
get accountInfo(): AccountInfo {
return { account: this._account, chainId: this._chainId, chainName: this._chainName };
}
get provider() {
return this._signer.provider;
}
get chainId() {
return this._chainId;
}
async balance() {
return this.signer.getBalance();
}
get providerType() {
return this._providerType;
}
get did() {
return `did:${Methods.Erc1056}:${this._address}`;
}
async send({ to, data, value }: providers.TransactionRequest): Promise<providers.TransactionReceipt> {
const tx = { to, from: this.address, data, ...(value && { value: BigNumber.from(value) }) };
const receipt = await (await this._signer.sendTransaction(tx)).wait();
return receipt;
}
/**
* Makes a (readonly) call to a smart contract
* https://docs.ethers.io/v5/single-page/#/v5/api/providers/provider/-%23-Provider-call
* @param params.to adddress of contract
* @param params.data call data
* @returns The result of the call
*/
async call({ to, data }: providers.TransactionRequest): Promise<string> {
const tx = { to, from: this.address, data };
const result = await this._signer.call(tx);
return result;
}
async signMessage(message: Uint8Array) {
return this.signer.signMessage(message);
}
async connect(signer: Required<ethers.Signer>, providerType: ProviderType) {
this._signer = signer;
this._providerType = providerType;
await this.init();
}
async closeConnection() {
if (this._signer instanceof WalletConnectProvider) {
await this._signer.disconnect();
} else if (this._signer instanceof EkcSigner) {
try {
await this._signer.ekc.logout({ mode: "popup" });
return false;
} catch (error) {
console.log("error in azure logout ", error);
}
}
return true;
}
async publicKey() {
if (this._publicKey) return this._publicKey;
else if (this._signer instanceof Wallet) {
this._publicKey = this._signer.publicKey;
} else {
this._publicKey = (await this.publicKeyAndIdentityToken()).publicKey;
}
return this._publicKey;
}
async publicKeyAndIdentityToken(): Promise<IPubKeyAndIdentityToken> {
if (!this._publicKey || !this._identityToken) {
await this._calculatePubKeyAndIdentityToken();
}
return {
publicKey: this._publicKey,
identityToken: this._identityToken,
};
}
private async _calculatePubKeyAndIdentityToken() {
const header = {
alg: "ES256",
typ: "JWT",
};
const encodedHeader = base64url(JSON.stringify(header));
const address = this._address;
const payload = {
iss: `did:ethr:${address}`,
claimData: {
blockNumber: await this._signer.provider.getBlockNumber(),
},
};
const encodedPayload = base64url(JSON.stringify(payload));
const token = `0x${Buffer.from(`${encodedHeader}.${encodedPayload}`).toString("hex")}`;
// arrayification is necessary for WalletConnect signatures to work. eth_sign expects message in bytes: https://docs.walletconnect.org/json-rpc-api-methods/ethereum#eth_sign
// keccak256 hash is applied for Metamask to display a coherent hex value when signing
const message = arrayify(keccak256(token));
const sig = await this.signMessage(message);
const recoverValidatedPublicKey = (signedMessage: Uint8Array): string | undefined => {
const publicKey = recoverPublicKey(signedMessage, sig);
if (getAddress(address) === getAddress(computeAddress(publicKey))) {
return computePublicKey(publicKey, true).slice(2);
}
return undefined;
};
// Computation of the digest in order to recover the public key under the assumption
// that signature was performed as per the eth_sign spec (https://eth.wiki/json-rpc/API#eth_sign)
// In the event that the wallet isn't prefixing & hashing message as per spec, attempt recovery without digest
const digest = arrayify(hashMessage(message));
const publicKey = recoverValidatedPublicKey(digest) ?? recoverValidatedPublicKey(message);
if (publicKey) {
this._publicKey = publicKey;
this._identityToken = `${encodedHeader}.${encodedPayload}.${base64url(sig)}`;
} else {
throw new Error(ERROR_MESSAGES.PUBLIC_KEY_NOT_RECOVERED);
}
}
}