@0xsequence/guard 0.0.0-20230505164841 → 0.0.0-20230509145455
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/0xsequence-guard.cjs.d.ts +11 -1
- package/dist/0xsequence-guard.cjs.js +15 -6
- package/package.json +4 -6
- package/src/guard.gen.ts +32 -24
- package/src/index.ts +0 -1
- package/dist/0xsequence-guard.cjs.dev.js +0 -218
- package/dist/0xsequence-guard.cjs.prod.js +0 -218
- package/dist/0xsequence-guard.esm.js +0 -210
- package/dist/declarations/src/guard.gen.d.ts +0 -91
- package/dist/declarations/src/index.d.ts +0 -2
- package/dist/declarations/src/signer.d.ts +0 -21
- package/src/signer.ts +0 -117
|
@@ -1 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
// are you seeing an error that a default export doesn't exist but your source file has a default export?
|
|
2
|
+
// you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
|
|
3
|
+
|
|
4
|
+
// curious why you need to?
|
|
5
|
+
// this file exists so that you can import from the entrypoint normally
|
|
6
|
+
// except that it points to your source file and you don't need to run build constantly
|
|
7
|
+
// which means we need to re-export all of the modules from your source file
|
|
8
|
+
// and since export * doesn't include default exports, we need to read your source file
|
|
9
|
+
// to check for a default export and re-export it if it exists
|
|
10
|
+
// it's not ideal, but it works pretty well ¯\_(ツ)_/¯
|
|
11
|
+
export * from "../src/index";
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
// this file might look strange and you might be wondering what it's for
|
|
3
|
+
// it's lets you import your source files by importing this entrypoint
|
|
4
|
+
// as you would import it if it was built with preconstruct build
|
|
5
|
+
// this file is slightly different to some others though
|
|
6
|
+
// it has a require hook which compiles your code with Babel
|
|
7
|
+
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
+
// but you can still require this module and it'll be compiled
|
|
2
9
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
// this bit of code imports the require hook and registers it
|
|
11
|
+
let unregister = require("../../../node_modules/.pnpm/@preconstruct+hook@0.4.0/node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../..", "..");
|
|
12
|
+
|
|
13
|
+
// this re-exports the source file
|
|
14
|
+
module.exports = require("../src/index.ts");
|
|
15
|
+
|
|
16
|
+
unregister();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xsequence/guard",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-20230509145455",
|
|
4
4
|
"description": "guard sub-package for Sequence",
|
|
5
5
|
"repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/guard",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -8,11 +8,9 @@
|
|
|
8
8
|
"module": "dist/0xsequence-guard.esm.js",
|
|
9
9
|
"author": "Horizon Blockchain Games",
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
|
-
"dependencies": {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"@0xsequence/signhub": "0.0.0-20230505164841"
|
|
15
|
-
},
|
|
11
|
+
"dependencies": {},
|
|
12
|
+
"peerDependencies": {},
|
|
13
|
+
"devDependencies": {},
|
|
16
14
|
"files": [
|
|
17
15
|
"src",
|
|
18
16
|
"dist"
|
package/src/guard.gen.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
|
-
// sequence-guard v0.4.0
|
|
2
|
+
// sequence-guard v0.4.0 98e0726794b2b5922a0e356280daac3a409602e4
|
|
3
3
|
// --
|
|
4
4
|
// Code generated by webrpc-gen@v0.10.x-dev with typescript generator. DO NOT EDIT.
|
|
5
5
|
//
|
|
@@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
|
|
|
12
12
|
export const WebRPCSchemaVersion = "v0.4.0"
|
|
13
13
|
|
|
14
14
|
// Schema hash generated from your RIDL schema
|
|
15
|
-
export const WebRPCSchemaHash = "
|
|
15
|
+
export const WebRPCSchemaHash = "98e0726794b2b5922a0e356280daac3a409602e4"
|
|
16
16
|
|
|
17
17
|
//
|
|
18
18
|
// Types
|
|
@@ -35,9 +35,18 @@ export interface RuntimeStatus {
|
|
|
35
35
|
commitHash: string
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
export interface SequenceContext {
|
|
39
|
+
factory: string
|
|
40
|
+
mainModule: string
|
|
41
|
+
mainModuleUpgradable: string
|
|
42
|
+
guestModule: string
|
|
43
|
+
utils: string
|
|
44
|
+
}
|
|
45
|
+
|
|
38
46
|
export interface WalletConfig {
|
|
39
47
|
address: string
|
|
40
|
-
|
|
48
|
+
signers: Array<WalletSigner>
|
|
49
|
+
threshold: number
|
|
41
50
|
}
|
|
42
51
|
|
|
43
52
|
export interface WalletSigner {
|
|
@@ -55,9 +64,9 @@ export interface Guard {
|
|
|
55
64
|
ping(headers?: object): Promise<PingReturn>
|
|
56
65
|
version(headers?: object): Promise<VersionReturn>
|
|
57
66
|
runtimeStatus(headers?: object): Promise<RuntimeStatusReturn>
|
|
58
|
-
|
|
67
|
+
getSequenceContext(headers?: object): Promise<GetSequenceContextReturn>
|
|
68
|
+
getSignerConfig(headers?: object): Promise<GetSignerConfigReturn>
|
|
59
69
|
sign(args: SignArgs, headers?: object): Promise<SignReturn>
|
|
60
|
-
signWith(args: SignWithArgs, headers?: object): Promise<SignWithReturn>
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
export interface PingArgs {
|
|
@@ -78,8 +87,13 @@ export interface RuntimeStatusArgs {
|
|
|
78
87
|
export interface RuntimeStatusReturn {
|
|
79
88
|
status: RuntimeStatus
|
|
80
89
|
}
|
|
90
|
+
export interface GetSequenceContextArgs {
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface GetSequenceContextReturn {
|
|
94
|
+
data: SequenceContext
|
|
95
|
+
}
|
|
81
96
|
export interface GetSignerConfigArgs {
|
|
82
|
-
signer: string
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
export interface GetSignerConfigReturn {
|
|
@@ -92,14 +106,6 @@ export interface SignArgs {
|
|
|
92
106
|
export interface SignReturn {
|
|
93
107
|
sig: string
|
|
94
108
|
}
|
|
95
|
-
export interface SignWithArgs {
|
|
96
|
-
signer: string
|
|
97
|
-
request: SignRequest
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface SignWithReturn {
|
|
101
|
-
sig: string
|
|
102
|
-
}
|
|
103
109
|
|
|
104
110
|
|
|
105
111
|
|
|
@@ -159,33 +165,35 @@ export class Guard implements Guard {
|
|
|
159
165
|
})
|
|
160
166
|
}
|
|
161
167
|
|
|
162
|
-
|
|
168
|
+
getSequenceContext = (headers?: object): Promise<GetSequenceContextReturn> => {
|
|
163
169
|
return this.fetch(
|
|
164
|
-
this.url('
|
|
165
|
-
createHTTPRequest(
|
|
170
|
+
this.url('GetSequenceContext'),
|
|
171
|
+
createHTTPRequest({}, headers)
|
|
172
|
+
).then((res) => {
|
|
166
173
|
return buildResponse(res).then(_data => {
|
|
167
174
|
return {
|
|
168
|
-
|
|
175
|
+
data: <SequenceContext>(_data.data)
|
|
169
176
|
}
|
|
170
177
|
})
|
|
171
178
|
})
|
|
172
179
|
}
|
|
173
180
|
|
|
174
|
-
|
|
181
|
+
getSignerConfig = (headers?: object): Promise<GetSignerConfigReturn> => {
|
|
175
182
|
return this.fetch(
|
|
176
|
-
this.url('
|
|
177
|
-
createHTTPRequest(
|
|
183
|
+
this.url('GetSignerConfig'),
|
|
184
|
+
createHTTPRequest({}, headers)
|
|
185
|
+
).then((res) => {
|
|
178
186
|
return buildResponse(res).then(_data => {
|
|
179
187
|
return {
|
|
180
|
-
|
|
188
|
+
signerConfig: <WalletConfig>(_data.signerConfig)
|
|
181
189
|
}
|
|
182
190
|
})
|
|
183
191
|
})
|
|
184
192
|
}
|
|
185
193
|
|
|
186
|
-
|
|
194
|
+
sign = (args: SignArgs, headers?: object): Promise<SignReturn> => {
|
|
187
195
|
return this.fetch(
|
|
188
|
-
this.url('
|
|
196
|
+
this.url('Sign'),
|
|
189
197
|
createHTTPRequest(args, headers)).then((res) => {
|
|
190
198
|
return buildResponse(res).then(_data => {
|
|
191
199
|
return {
|
package/src/index.ts
CHANGED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var ethers = require('ethers');
|
|
6
|
-
var core = require('@0xsequence/core');
|
|
7
|
-
|
|
8
|
-
function _extends() {
|
|
9
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
10
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
11
|
-
var source = arguments[i];
|
|
12
|
-
for (var key in source) {
|
|
13
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
14
|
-
target[key] = source[key];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
return _extends.apply(this, arguments);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/* eslint-disable */
|
|
24
|
-
// sequence-guard v0.4.0 a29651d1d5f63268e8d03b51e46557e0632c144d
|
|
25
|
-
// --
|
|
26
|
-
// Code generated by webrpc-gen@v0.10.x-dev with typescript generator. DO NOT EDIT.
|
|
27
|
-
//
|
|
28
|
-
// webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
|
|
29
|
-
|
|
30
|
-
// WebRPC description and code-gen version
|
|
31
|
-
const WebRPCVersion = "v1";
|
|
32
|
-
|
|
33
|
-
// Schema version of your RIDL schema
|
|
34
|
-
const WebRPCSchemaVersion = "v0.4.0";
|
|
35
|
-
|
|
36
|
-
// Schema hash generated from your RIDL schema
|
|
37
|
-
const WebRPCSchemaHash = "a29651d1d5f63268e8d03b51e46557e0632c144d";
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
// Types
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
// Client
|
|
44
|
-
//
|
|
45
|
-
class Guard {
|
|
46
|
-
constructor(hostname, fetch) {
|
|
47
|
-
this.path = '/rpc/Guard/';
|
|
48
|
-
this.ping = headers => {
|
|
49
|
-
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers)).then(res => {
|
|
50
|
-
return buildResponse(res).then(_data => {
|
|
51
|
-
return {
|
|
52
|
-
status: _data.status
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
this.version = headers => {
|
|
58
|
-
return this.fetch(this.url('Version'), createHTTPRequest({}, headers)).then(res => {
|
|
59
|
-
return buildResponse(res).then(_data => {
|
|
60
|
-
return {
|
|
61
|
-
version: _data.version
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
this.runtimeStatus = headers => {
|
|
67
|
-
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers)).then(res => {
|
|
68
|
-
return buildResponse(res).then(_data => {
|
|
69
|
-
return {
|
|
70
|
-
status: _data.status
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
this.getSignerConfig = (args, headers) => {
|
|
76
|
-
return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers)).then(res => {
|
|
77
|
-
return buildResponse(res).then(_data => {
|
|
78
|
-
return {
|
|
79
|
-
signerConfig: _data.signerConfig
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
this.sign = (args, headers) => {
|
|
85
|
-
return this.fetch(this.url('Sign'), createHTTPRequest(args, headers)).then(res => {
|
|
86
|
-
return buildResponse(res).then(_data => {
|
|
87
|
-
return {
|
|
88
|
-
sig: _data.sig
|
|
89
|
-
};
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
this.signWith = (args, headers) => {
|
|
94
|
-
return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers)).then(res => {
|
|
95
|
-
return buildResponse(res).then(_data => {
|
|
96
|
-
return {
|
|
97
|
-
sig: _data.sig
|
|
98
|
-
};
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
this.hostname = hostname;
|
|
103
|
-
this.fetch = (input, init) => fetch(input, init);
|
|
104
|
-
}
|
|
105
|
-
url(name) {
|
|
106
|
-
return this.hostname + this.path + name;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const createHTTPRequest = (body = {}, headers = {}) => {
|
|
110
|
-
return {
|
|
111
|
-
method: 'POST',
|
|
112
|
-
headers: _extends({}, headers, {
|
|
113
|
-
'Content-Type': 'application/json'
|
|
114
|
-
}),
|
|
115
|
-
body: JSON.stringify(body || {})
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
const buildResponse = res => {
|
|
119
|
-
return res.text().then(text => {
|
|
120
|
-
let data;
|
|
121
|
-
try {
|
|
122
|
-
data = JSON.parse(text);
|
|
123
|
-
} catch (err) {
|
|
124
|
-
throw {
|
|
125
|
-
code: 'unknown',
|
|
126
|
-
msg: `expecting JSON, got: ${text}`,
|
|
127
|
-
status: res.status
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
if (!res.ok) {
|
|
131
|
-
throw data; // webrpc error response
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return data;
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
class GuardSigner {
|
|
139
|
-
constructor(address, url, appendSuffix = false) {
|
|
140
|
-
this.requests = new Map();
|
|
141
|
-
this.address = address;
|
|
142
|
-
this.url = url;
|
|
143
|
-
this.appendSuffix = appendSuffix;
|
|
144
|
-
this.guard = new Guard(url, global.fetch);
|
|
145
|
-
}
|
|
146
|
-
async getAddress() {
|
|
147
|
-
return this.address;
|
|
148
|
-
}
|
|
149
|
-
async requestSignature(id, _message, metadata, callbacks) {
|
|
150
|
-
if (!core.commons.isWalletSignRequestMetadata(metadata)) {
|
|
151
|
-
callbacks.onRejection('Expected Sequence-like metadata');
|
|
152
|
-
} else {
|
|
153
|
-
// Queue the request first, this method only does that
|
|
154
|
-
// the requesting to the API is later handled on every status change
|
|
155
|
-
this.requests.set(id, callbacks);
|
|
156
|
-
}
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
notifyStatusChange(id, status, metadata) {
|
|
160
|
-
if (!this.requests.has(id)) return;
|
|
161
|
-
if (!core.commons.isWalletSignRequestMetadata(metadata)) {
|
|
162
|
-
this.requests.get(id).onRejection('Expected Sequence-like metadata (status update)');
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
this.evaluateRequest(id, status.message, status, metadata);
|
|
166
|
-
}
|
|
167
|
-
packMsgAndSig(address, msg, sig, chainId) {
|
|
168
|
-
return ethers.ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
169
|
-
}
|
|
170
|
-
keyOfRequest(signer, msg, auxData, chainId) {
|
|
171
|
-
return ethers.ethers.utils.solidityKeccak256(['address', 'uint256', 'bytes', 'bytes'], [signer, chainId, msg, auxData]);
|
|
172
|
-
}
|
|
173
|
-
async evaluateRequest(id, message, _, metadata) {
|
|
174
|
-
var _metadata$signaturePa;
|
|
175
|
-
// Building auxData, notice: this uses the old v1 format
|
|
176
|
-
// TODO: We should update the guard API so we can pass the metadata directly
|
|
177
|
-
const coder = core.universal.genericCoderFor(metadata.config.version);
|
|
178
|
-
const {
|
|
179
|
-
encoded
|
|
180
|
-
} = coder.signature.encodeSigners(metadata.config, (_metadata$signaturePa = metadata.signatureParts) != null ? _metadata$signaturePa : new Map(), [], metadata.chainId);
|
|
181
|
-
try {
|
|
182
|
-
var _this$requests$get;
|
|
183
|
-
const key = this.keyOfRequest(this.address, message, encoded, metadata.chainId);
|
|
184
|
-
const lastAttempt = (_this$requests$get = this.requests.get(id)) == null ? void 0 : _this$requests$get.lastAttempt;
|
|
185
|
-
if (lastAttempt === key) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
this.requests.get(id).lastAttempt = key;
|
|
189
|
-
const result = await this.guard.signWith({
|
|
190
|
-
signer: this.address,
|
|
191
|
-
request: {
|
|
192
|
-
msg: ethers.ethers.utils.hexlify(message),
|
|
193
|
-
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
194
|
-
chainId: ethers.ethers.BigNumber.from(metadata.chainId).toNumber() // TODO: This should be a string (in the API)
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
if (ethers.ethers.utils.arrayify(result.sig).length !== 0) {
|
|
199
|
-
this.requests.get(id).onSignature(result.sig);
|
|
200
|
-
this.requests.delete(id);
|
|
201
|
-
}
|
|
202
|
-
} catch (e) {
|
|
203
|
-
// The guard signer may reject the request for a number of reasons
|
|
204
|
-
// like for example, if it's being the first signer (it waits for other signers to sign first)
|
|
205
|
-
// for now we ignore all errors, but we should probably handle them
|
|
206
|
-
// TODO: Filter real errors from control flow errors
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
suffix() {
|
|
210
|
-
return this.appendSuffix ? [3] : [];
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
exports.Guard = Guard;
|
|
215
|
-
exports.GuardSigner = GuardSigner;
|
|
216
|
-
exports.WebRPCSchemaHash = WebRPCSchemaHash;
|
|
217
|
-
exports.WebRPCSchemaVersion = WebRPCSchemaVersion;
|
|
218
|
-
exports.WebRPCVersion = WebRPCVersion;
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var ethers = require('ethers');
|
|
6
|
-
var core = require('@0xsequence/core');
|
|
7
|
-
|
|
8
|
-
function _extends() {
|
|
9
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
10
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
11
|
-
var source = arguments[i];
|
|
12
|
-
for (var key in source) {
|
|
13
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
14
|
-
target[key] = source[key];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
return _extends.apply(this, arguments);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/* eslint-disable */
|
|
24
|
-
// sequence-guard v0.4.0 a29651d1d5f63268e8d03b51e46557e0632c144d
|
|
25
|
-
// --
|
|
26
|
-
// Code generated by webrpc-gen@v0.10.x-dev with typescript generator. DO NOT EDIT.
|
|
27
|
-
//
|
|
28
|
-
// webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
|
|
29
|
-
|
|
30
|
-
// WebRPC description and code-gen version
|
|
31
|
-
const WebRPCVersion = "v1";
|
|
32
|
-
|
|
33
|
-
// Schema version of your RIDL schema
|
|
34
|
-
const WebRPCSchemaVersion = "v0.4.0";
|
|
35
|
-
|
|
36
|
-
// Schema hash generated from your RIDL schema
|
|
37
|
-
const WebRPCSchemaHash = "a29651d1d5f63268e8d03b51e46557e0632c144d";
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
// Types
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
// Client
|
|
44
|
-
//
|
|
45
|
-
class Guard {
|
|
46
|
-
constructor(hostname, fetch) {
|
|
47
|
-
this.path = '/rpc/Guard/';
|
|
48
|
-
this.ping = headers => {
|
|
49
|
-
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers)).then(res => {
|
|
50
|
-
return buildResponse(res).then(_data => {
|
|
51
|
-
return {
|
|
52
|
-
status: _data.status
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
this.version = headers => {
|
|
58
|
-
return this.fetch(this.url('Version'), createHTTPRequest({}, headers)).then(res => {
|
|
59
|
-
return buildResponse(res).then(_data => {
|
|
60
|
-
return {
|
|
61
|
-
version: _data.version
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
this.runtimeStatus = headers => {
|
|
67
|
-
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers)).then(res => {
|
|
68
|
-
return buildResponse(res).then(_data => {
|
|
69
|
-
return {
|
|
70
|
-
status: _data.status
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
this.getSignerConfig = (args, headers) => {
|
|
76
|
-
return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers)).then(res => {
|
|
77
|
-
return buildResponse(res).then(_data => {
|
|
78
|
-
return {
|
|
79
|
-
signerConfig: _data.signerConfig
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
this.sign = (args, headers) => {
|
|
85
|
-
return this.fetch(this.url('Sign'), createHTTPRequest(args, headers)).then(res => {
|
|
86
|
-
return buildResponse(res).then(_data => {
|
|
87
|
-
return {
|
|
88
|
-
sig: _data.sig
|
|
89
|
-
};
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
this.signWith = (args, headers) => {
|
|
94
|
-
return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers)).then(res => {
|
|
95
|
-
return buildResponse(res).then(_data => {
|
|
96
|
-
return {
|
|
97
|
-
sig: _data.sig
|
|
98
|
-
};
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
this.hostname = hostname;
|
|
103
|
-
this.fetch = (input, init) => fetch(input, init);
|
|
104
|
-
}
|
|
105
|
-
url(name) {
|
|
106
|
-
return this.hostname + this.path + name;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const createHTTPRequest = (body = {}, headers = {}) => {
|
|
110
|
-
return {
|
|
111
|
-
method: 'POST',
|
|
112
|
-
headers: _extends({}, headers, {
|
|
113
|
-
'Content-Type': 'application/json'
|
|
114
|
-
}),
|
|
115
|
-
body: JSON.stringify(body || {})
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
const buildResponse = res => {
|
|
119
|
-
return res.text().then(text => {
|
|
120
|
-
let data;
|
|
121
|
-
try {
|
|
122
|
-
data = JSON.parse(text);
|
|
123
|
-
} catch (err) {
|
|
124
|
-
throw {
|
|
125
|
-
code: 'unknown',
|
|
126
|
-
msg: `expecting JSON, got: ${text}`,
|
|
127
|
-
status: res.status
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
if (!res.ok) {
|
|
131
|
-
throw data; // webrpc error response
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return data;
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
class GuardSigner {
|
|
139
|
-
constructor(address, url, appendSuffix = false) {
|
|
140
|
-
this.requests = new Map();
|
|
141
|
-
this.address = address;
|
|
142
|
-
this.url = url;
|
|
143
|
-
this.appendSuffix = appendSuffix;
|
|
144
|
-
this.guard = new Guard(url, global.fetch);
|
|
145
|
-
}
|
|
146
|
-
async getAddress() {
|
|
147
|
-
return this.address;
|
|
148
|
-
}
|
|
149
|
-
async requestSignature(id, _message, metadata, callbacks) {
|
|
150
|
-
if (!core.commons.isWalletSignRequestMetadata(metadata)) {
|
|
151
|
-
callbacks.onRejection('Expected Sequence-like metadata');
|
|
152
|
-
} else {
|
|
153
|
-
// Queue the request first, this method only does that
|
|
154
|
-
// the requesting to the API is later handled on every status change
|
|
155
|
-
this.requests.set(id, callbacks);
|
|
156
|
-
}
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
notifyStatusChange(id, status, metadata) {
|
|
160
|
-
if (!this.requests.has(id)) return;
|
|
161
|
-
if (!core.commons.isWalletSignRequestMetadata(metadata)) {
|
|
162
|
-
this.requests.get(id).onRejection('Expected Sequence-like metadata (status update)');
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
this.evaluateRequest(id, status.message, status, metadata);
|
|
166
|
-
}
|
|
167
|
-
packMsgAndSig(address, msg, sig, chainId) {
|
|
168
|
-
return ethers.ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
169
|
-
}
|
|
170
|
-
keyOfRequest(signer, msg, auxData, chainId) {
|
|
171
|
-
return ethers.ethers.utils.solidityKeccak256(['address', 'uint256', 'bytes', 'bytes'], [signer, chainId, msg, auxData]);
|
|
172
|
-
}
|
|
173
|
-
async evaluateRequest(id, message, _, metadata) {
|
|
174
|
-
var _metadata$signaturePa;
|
|
175
|
-
// Building auxData, notice: this uses the old v1 format
|
|
176
|
-
// TODO: We should update the guard API so we can pass the metadata directly
|
|
177
|
-
const coder = core.universal.genericCoderFor(metadata.config.version);
|
|
178
|
-
const {
|
|
179
|
-
encoded
|
|
180
|
-
} = coder.signature.encodeSigners(metadata.config, (_metadata$signaturePa = metadata.signatureParts) != null ? _metadata$signaturePa : new Map(), [], metadata.chainId);
|
|
181
|
-
try {
|
|
182
|
-
var _this$requests$get;
|
|
183
|
-
const key = this.keyOfRequest(this.address, message, encoded, metadata.chainId);
|
|
184
|
-
const lastAttempt = (_this$requests$get = this.requests.get(id)) == null ? void 0 : _this$requests$get.lastAttempt;
|
|
185
|
-
if (lastAttempt === key) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
this.requests.get(id).lastAttempt = key;
|
|
189
|
-
const result = await this.guard.signWith({
|
|
190
|
-
signer: this.address,
|
|
191
|
-
request: {
|
|
192
|
-
msg: ethers.ethers.utils.hexlify(message),
|
|
193
|
-
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
194
|
-
chainId: ethers.ethers.BigNumber.from(metadata.chainId).toNumber() // TODO: This should be a string (in the API)
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
if (ethers.ethers.utils.arrayify(result.sig).length !== 0) {
|
|
199
|
-
this.requests.get(id).onSignature(result.sig);
|
|
200
|
-
this.requests.delete(id);
|
|
201
|
-
}
|
|
202
|
-
} catch (e) {
|
|
203
|
-
// The guard signer may reject the request for a number of reasons
|
|
204
|
-
// like for example, if it's being the first signer (it waits for other signers to sign first)
|
|
205
|
-
// for now we ignore all errors, but we should probably handle them
|
|
206
|
-
// TODO: Filter real errors from control flow errors
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
suffix() {
|
|
210
|
-
return this.appendSuffix ? [3] : [];
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
exports.Guard = Guard;
|
|
215
|
-
exports.GuardSigner = GuardSigner;
|
|
216
|
-
exports.WebRPCSchemaHash = WebRPCSchemaHash;
|
|
217
|
-
exports.WebRPCSchemaVersion = WebRPCSchemaVersion;
|
|
218
|
-
exports.WebRPCVersion = WebRPCVersion;
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import { commons, universal } from '@0xsequence/core';
|
|
3
|
-
|
|
4
|
-
function _extends() {
|
|
5
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
6
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
7
|
-
var source = arguments[i];
|
|
8
|
-
for (var key in source) {
|
|
9
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
10
|
-
target[key] = source[key];
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return target;
|
|
15
|
-
};
|
|
16
|
-
return _extends.apply(this, arguments);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/* eslint-disable */
|
|
20
|
-
// sequence-guard v0.4.0 a29651d1d5f63268e8d03b51e46557e0632c144d
|
|
21
|
-
// --
|
|
22
|
-
// Code generated by webrpc-gen@v0.10.x-dev with typescript generator. DO NOT EDIT.
|
|
23
|
-
//
|
|
24
|
-
// webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
|
|
25
|
-
|
|
26
|
-
// WebRPC description and code-gen version
|
|
27
|
-
const WebRPCVersion = "v1";
|
|
28
|
-
|
|
29
|
-
// Schema version of your RIDL schema
|
|
30
|
-
const WebRPCSchemaVersion = "v0.4.0";
|
|
31
|
-
|
|
32
|
-
// Schema hash generated from your RIDL schema
|
|
33
|
-
const WebRPCSchemaHash = "a29651d1d5f63268e8d03b51e46557e0632c144d";
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
// Types
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
// Client
|
|
40
|
-
//
|
|
41
|
-
class Guard {
|
|
42
|
-
constructor(hostname, fetch) {
|
|
43
|
-
this.path = '/rpc/Guard/';
|
|
44
|
-
this.ping = headers => {
|
|
45
|
-
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers)).then(res => {
|
|
46
|
-
return buildResponse(res).then(_data => {
|
|
47
|
-
return {
|
|
48
|
-
status: _data.status
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
this.version = headers => {
|
|
54
|
-
return this.fetch(this.url('Version'), createHTTPRequest({}, headers)).then(res => {
|
|
55
|
-
return buildResponse(res).then(_data => {
|
|
56
|
-
return {
|
|
57
|
-
version: _data.version
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
this.runtimeStatus = headers => {
|
|
63
|
-
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers)).then(res => {
|
|
64
|
-
return buildResponse(res).then(_data => {
|
|
65
|
-
return {
|
|
66
|
-
status: _data.status
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
this.getSignerConfig = (args, headers) => {
|
|
72
|
-
return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers)).then(res => {
|
|
73
|
-
return buildResponse(res).then(_data => {
|
|
74
|
-
return {
|
|
75
|
-
signerConfig: _data.signerConfig
|
|
76
|
-
};
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
this.sign = (args, headers) => {
|
|
81
|
-
return this.fetch(this.url('Sign'), createHTTPRequest(args, headers)).then(res => {
|
|
82
|
-
return buildResponse(res).then(_data => {
|
|
83
|
-
return {
|
|
84
|
-
sig: _data.sig
|
|
85
|
-
};
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
this.signWith = (args, headers) => {
|
|
90
|
-
return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers)).then(res => {
|
|
91
|
-
return buildResponse(res).then(_data => {
|
|
92
|
-
return {
|
|
93
|
-
sig: _data.sig
|
|
94
|
-
};
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
};
|
|
98
|
-
this.hostname = hostname;
|
|
99
|
-
this.fetch = (input, init) => fetch(input, init);
|
|
100
|
-
}
|
|
101
|
-
url(name) {
|
|
102
|
-
return this.hostname + this.path + name;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
const createHTTPRequest = (body = {}, headers = {}) => {
|
|
106
|
-
return {
|
|
107
|
-
method: 'POST',
|
|
108
|
-
headers: _extends({}, headers, {
|
|
109
|
-
'Content-Type': 'application/json'
|
|
110
|
-
}),
|
|
111
|
-
body: JSON.stringify(body || {})
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
const buildResponse = res => {
|
|
115
|
-
return res.text().then(text => {
|
|
116
|
-
let data;
|
|
117
|
-
try {
|
|
118
|
-
data = JSON.parse(text);
|
|
119
|
-
} catch (err) {
|
|
120
|
-
throw {
|
|
121
|
-
code: 'unknown',
|
|
122
|
-
msg: `expecting JSON, got: ${text}`,
|
|
123
|
-
status: res.status
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
if (!res.ok) {
|
|
127
|
-
throw data; // webrpc error response
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return data;
|
|
131
|
-
});
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
class GuardSigner {
|
|
135
|
-
constructor(address, url, appendSuffix = false) {
|
|
136
|
-
this.requests = new Map();
|
|
137
|
-
this.address = address;
|
|
138
|
-
this.url = url;
|
|
139
|
-
this.appendSuffix = appendSuffix;
|
|
140
|
-
this.guard = new Guard(url, global.fetch);
|
|
141
|
-
}
|
|
142
|
-
async getAddress() {
|
|
143
|
-
return this.address;
|
|
144
|
-
}
|
|
145
|
-
async requestSignature(id, _message, metadata, callbacks) {
|
|
146
|
-
if (!commons.isWalletSignRequestMetadata(metadata)) {
|
|
147
|
-
callbacks.onRejection('Expected Sequence-like metadata');
|
|
148
|
-
} else {
|
|
149
|
-
// Queue the request first, this method only does that
|
|
150
|
-
// the requesting to the API is later handled on every status change
|
|
151
|
-
this.requests.set(id, callbacks);
|
|
152
|
-
}
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
notifyStatusChange(id, status, metadata) {
|
|
156
|
-
if (!this.requests.has(id)) return;
|
|
157
|
-
if (!commons.isWalletSignRequestMetadata(metadata)) {
|
|
158
|
-
this.requests.get(id).onRejection('Expected Sequence-like metadata (status update)');
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
this.evaluateRequest(id, status.message, status, metadata);
|
|
162
|
-
}
|
|
163
|
-
packMsgAndSig(address, msg, sig, chainId) {
|
|
164
|
-
return ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes', 'bytes'], [address, chainId, msg, sig]);
|
|
165
|
-
}
|
|
166
|
-
keyOfRequest(signer, msg, auxData, chainId) {
|
|
167
|
-
return ethers.utils.solidityKeccak256(['address', 'uint256', 'bytes', 'bytes'], [signer, chainId, msg, auxData]);
|
|
168
|
-
}
|
|
169
|
-
async evaluateRequest(id, message, _, metadata) {
|
|
170
|
-
var _metadata$signaturePa;
|
|
171
|
-
// Building auxData, notice: this uses the old v1 format
|
|
172
|
-
// TODO: We should update the guard API so we can pass the metadata directly
|
|
173
|
-
const coder = universal.genericCoderFor(metadata.config.version);
|
|
174
|
-
const {
|
|
175
|
-
encoded
|
|
176
|
-
} = coder.signature.encodeSigners(metadata.config, (_metadata$signaturePa = metadata.signatureParts) != null ? _metadata$signaturePa : new Map(), [], metadata.chainId);
|
|
177
|
-
try {
|
|
178
|
-
var _this$requests$get;
|
|
179
|
-
const key = this.keyOfRequest(this.address, message, encoded, metadata.chainId);
|
|
180
|
-
const lastAttempt = (_this$requests$get = this.requests.get(id)) == null ? void 0 : _this$requests$get.lastAttempt;
|
|
181
|
-
if (lastAttempt === key) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
this.requests.get(id).lastAttempt = key;
|
|
185
|
-
const result = await this.guard.signWith({
|
|
186
|
-
signer: this.address,
|
|
187
|
-
request: {
|
|
188
|
-
msg: ethers.utils.hexlify(message),
|
|
189
|
-
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
190
|
-
chainId: ethers.BigNumber.from(metadata.chainId).toNumber() // TODO: This should be a string (in the API)
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
if (ethers.utils.arrayify(result.sig).length !== 0) {
|
|
195
|
-
this.requests.get(id).onSignature(result.sig);
|
|
196
|
-
this.requests.delete(id);
|
|
197
|
-
}
|
|
198
|
-
} catch (e) {
|
|
199
|
-
// The guard signer may reject the request for a number of reasons
|
|
200
|
-
// like for example, if it's being the first signer (it waits for other signers to sign first)
|
|
201
|
-
// for now we ignore all errors, but we should probably handle them
|
|
202
|
-
// TODO: Filter real errors from control flow errors
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
suffix() {
|
|
206
|
-
return this.appendSuffix ? [3] : [];
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export { Guard, GuardSigner, WebRPCSchemaHash, WebRPCSchemaVersion, WebRPCVersion };
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
export declare const WebRPCVersion = "v1";
|
|
2
|
-
export declare const WebRPCSchemaVersion = "v0.4.0";
|
|
3
|
-
export declare const WebRPCSchemaHash = "a29651d1d5f63268e8d03b51e46557e0632c144d";
|
|
4
|
-
export interface Version {
|
|
5
|
-
webrpcVersion: string;
|
|
6
|
-
schemaVersion: string;
|
|
7
|
-
schemaHash: string;
|
|
8
|
-
appVersion: string;
|
|
9
|
-
}
|
|
10
|
-
export interface RuntimeStatus {
|
|
11
|
-
healthOK: boolean;
|
|
12
|
-
startTime: string;
|
|
13
|
-
uptime: number;
|
|
14
|
-
ver: string;
|
|
15
|
-
branch: string;
|
|
16
|
-
commitHash: string;
|
|
17
|
-
}
|
|
18
|
-
export interface WalletConfig {
|
|
19
|
-
address: string;
|
|
20
|
-
content: string;
|
|
21
|
-
}
|
|
22
|
-
export interface WalletSigner {
|
|
23
|
-
address: string;
|
|
24
|
-
weight: number;
|
|
25
|
-
}
|
|
26
|
-
export interface SignRequest {
|
|
27
|
-
chainId: number;
|
|
28
|
-
msg: string;
|
|
29
|
-
auxData: string;
|
|
30
|
-
}
|
|
31
|
-
export interface Guard {
|
|
32
|
-
ping(headers?: object): Promise<PingReturn>;
|
|
33
|
-
version(headers?: object): Promise<VersionReturn>;
|
|
34
|
-
runtimeStatus(headers?: object): Promise<RuntimeStatusReturn>;
|
|
35
|
-
getSignerConfig(args: GetSignerConfigArgs, headers?: object): Promise<GetSignerConfigReturn>;
|
|
36
|
-
sign(args: SignArgs, headers?: object): Promise<SignReturn>;
|
|
37
|
-
signWith(args: SignWithArgs, headers?: object): Promise<SignWithReturn>;
|
|
38
|
-
}
|
|
39
|
-
export interface PingArgs {
|
|
40
|
-
}
|
|
41
|
-
export interface PingReturn {
|
|
42
|
-
status: boolean;
|
|
43
|
-
}
|
|
44
|
-
export interface VersionArgs {
|
|
45
|
-
}
|
|
46
|
-
export interface VersionReturn {
|
|
47
|
-
version: Version;
|
|
48
|
-
}
|
|
49
|
-
export interface RuntimeStatusArgs {
|
|
50
|
-
}
|
|
51
|
-
export interface RuntimeStatusReturn {
|
|
52
|
-
status: RuntimeStatus;
|
|
53
|
-
}
|
|
54
|
-
export interface GetSignerConfigArgs {
|
|
55
|
-
signer: string;
|
|
56
|
-
}
|
|
57
|
-
export interface GetSignerConfigReturn {
|
|
58
|
-
signerConfig: WalletConfig;
|
|
59
|
-
}
|
|
60
|
-
export interface SignArgs {
|
|
61
|
-
request: SignRequest;
|
|
62
|
-
}
|
|
63
|
-
export interface SignReturn {
|
|
64
|
-
sig: string;
|
|
65
|
-
}
|
|
66
|
-
export interface SignWithArgs {
|
|
67
|
-
signer: string;
|
|
68
|
-
request: SignRequest;
|
|
69
|
-
}
|
|
70
|
-
export interface SignWithReturn {
|
|
71
|
-
sig: string;
|
|
72
|
-
}
|
|
73
|
-
export declare class Guard implements Guard {
|
|
74
|
-
protected hostname: string;
|
|
75
|
-
protected fetch: Fetch;
|
|
76
|
-
protected path: string;
|
|
77
|
-
constructor(hostname: string, fetch: Fetch);
|
|
78
|
-
private url;
|
|
79
|
-
ping: (headers?: object | undefined) => Promise<PingReturn>;
|
|
80
|
-
version: (headers?: object | undefined) => Promise<VersionReturn>;
|
|
81
|
-
runtimeStatus: (headers?: object | undefined) => Promise<RuntimeStatusReturn>;
|
|
82
|
-
getSignerConfig: (args: GetSignerConfigArgs, headers?: object | undefined) => Promise<GetSignerConfigReturn>;
|
|
83
|
-
sign: (args: SignArgs, headers?: object | undefined) => Promise<SignReturn>;
|
|
84
|
-
signWith: (args: SignWithArgs, headers?: object | undefined) => Promise<SignWithReturn>;
|
|
85
|
-
}
|
|
86
|
-
export interface WebRPCError extends Error {
|
|
87
|
-
code: string;
|
|
88
|
-
msg: string;
|
|
89
|
-
status: number;
|
|
90
|
-
}
|
|
91
|
-
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { signers, Status } from '@0xsequence/signhub';
|
|
2
|
-
import { BytesLike } from 'ethers';
|
|
3
|
-
export declare class GuardSigner implements signers.SapientSigner {
|
|
4
|
-
readonly address: string;
|
|
5
|
-
readonly url: string;
|
|
6
|
-
readonly appendSuffix: boolean;
|
|
7
|
-
private guard;
|
|
8
|
-
private requests;
|
|
9
|
-
constructor(address: string, url: string, appendSuffix?: boolean);
|
|
10
|
-
getAddress(): Promise<string>;
|
|
11
|
-
requestSignature(id: string, _message: BytesLike, metadata: Object, callbacks: {
|
|
12
|
-
onSignature: (signature: BytesLike) => void;
|
|
13
|
-
onRejection: (error: string) => void;
|
|
14
|
-
onStatus: (situation: string) => void;
|
|
15
|
-
}): Promise<boolean>;
|
|
16
|
-
notifyStatusChange(id: string, status: Status, metadata: Object): void;
|
|
17
|
-
private packMsgAndSig;
|
|
18
|
-
private keyOfRequest;
|
|
19
|
-
private evaluateRequest;
|
|
20
|
-
suffix(): BytesLike;
|
|
21
|
-
}
|
package/src/signer.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { signers, Status } from '@0xsequence/signhub'
|
|
3
|
-
import { BytesLike, ethers } from 'ethers'
|
|
4
|
-
import { Guard } from './guard.gen'
|
|
5
|
-
import { commons, universal } from '@0xsequence/core'
|
|
6
|
-
|
|
7
|
-
export class GuardSigner implements signers.SapientSigner {
|
|
8
|
-
private guard: Guard
|
|
9
|
-
private requests: Map<string, {
|
|
10
|
-
lastAttempt?: string;
|
|
11
|
-
onSignature: (signature: BytesLike) => void;
|
|
12
|
-
onRejection: (error: string) => void;
|
|
13
|
-
onStatus: (situation: string) => void;
|
|
14
|
-
}> = new Map()
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
public readonly address: string,
|
|
18
|
-
public readonly url: string,
|
|
19
|
-
public readonly appendSuffix: boolean = false
|
|
20
|
-
) {
|
|
21
|
-
this.guard = new Guard(url, global.fetch)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async getAddress(): Promise<string> {
|
|
25
|
-
return this.address
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async requestSignature(
|
|
29
|
-
id: string,
|
|
30
|
-
_message: BytesLike,
|
|
31
|
-
metadata: Object,
|
|
32
|
-
callbacks: {
|
|
33
|
-
onSignature: (signature: BytesLike) => void;
|
|
34
|
-
onRejection: (error: string) => void;
|
|
35
|
-
onStatus: (situation: string) => void;
|
|
36
|
-
}
|
|
37
|
-
): Promise<boolean> {
|
|
38
|
-
if (!commons.isWalletSignRequestMetadata(metadata)) {
|
|
39
|
-
callbacks.onRejection('Expected Sequence-like metadata')
|
|
40
|
-
} else {
|
|
41
|
-
// Queue the request first, this method only does that
|
|
42
|
-
// the requesting to the API is later handled on every status change
|
|
43
|
-
this.requests.set(id, callbacks)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return true
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
notifyStatusChange(
|
|
50
|
-
id: string,
|
|
51
|
-
status: Status,
|
|
52
|
-
metadata: Object
|
|
53
|
-
): void {
|
|
54
|
-
if (!this.requests.has(id)) return
|
|
55
|
-
|
|
56
|
-
if (!commons.isWalletSignRequestMetadata(metadata)) {
|
|
57
|
-
this.requests.get(id)!.onRejection('Expected Sequence-like metadata (status update)')
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.evaluateRequest(id, status.message, status, metadata)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private packMsgAndSig(address: string, msg: BytesLike, sig: BytesLike, chainId: ethers.BigNumberish): string {
|
|
65
|
-
return ethers.utils.defaultAbiCoder.encode(
|
|
66
|
-
['address', 'uint256', 'bytes', 'bytes'],
|
|
67
|
-
[address, chainId, msg, sig]
|
|
68
|
-
)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private keyOfRequest(signer: string, msg: BytesLike, auxData: BytesLike, chainId: ethers.BigNumberish): string {
|
|
72
|
-
return ethers.utils.solidityKeccak256(
|
|
73
|
-
['address', 'uint256', 'bytes', 'bytes'],
|
|
74
|
-
[signer, chainId, msg, auxData]
|
|
75
|
-
)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private async evaluateRequest(id: string, message: BytesLike, _: Status, metadata: commons.WalletSignRequestMetadata): Promise<void> {
|
|
79
|
-
// Building auxData, notice: this uses the old v1 format
|
|
80
|
-
// TODO: We should update the guard API so we can pass the metadata directly
|
|
81
|
-
const coder = universal.genericCoderFor(metadata.config.version)
|
|
82
|
-
const { encoded } = coder.signature.encodeSigners(metadata.config, metadata.signatureParts ?? new Map(), [], metadata.chainId)
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
const key = this.keyOfRequest(this.address, message, encoded, metadata.chainId)
|
|
86
|
-
const lastAttempt = this.requests.get(id)?.lastAttempt
|
|
87
|
-
if (lastAttempt === key) {
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.requests.get(id)!.lastAttempt = key
|
|
92
|
-
|
|
93
|
-
const result = await this.guard.signWith({
|
|
94
|
-
signer: this.address,
|
|
95
|
-
request: {
|
|
96
|
-
msg: ethers.utils.hexlify(message),
|
|
97
|
-
auxData: this.packMsgAndSig(metadata.address, metadata.digest, encoded, metadata.chainId),
|
|
98
|
-
chainId: ethers.BigNumber.from(metadata.chainId).toNumber() // TODO: This should be a string (in the API)
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
if (ethers.utils.arrayify(result.sig).length !== 0) {
|
|
103
|
-
this.requests.get(id)!.onSignature(result.sig)
|
|
104
|
-
this.requests.delete(id)
|
|
105
|
-
}
|
|
106
|
-
} catch (e) {
|
|
107
|
-
// The guard signer may reject the request for a number of reasons
|
|
108
|
-
// like for example, if it's being the first signer (it waits for other signers to sign first)
|
|
109
|
-
// for now we ignore all errors, but we should probably handle them
|
|
110
|
-
// TODO: Filter real errors from control flow errors
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
suffix(): BytesLike {
|
|
115
|
-
return this.appendSuffix ? [ 3 ] : []
|
|
116
|
-
}
|
|
117
|
-
}
|