-
Notifications
You must be signed in to change notification settings - Fork 18
/
jsonrpcsigningserver.ts
210 lines (193 loc) · 6.04 KB
/
jsonrpcsigningserver.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
import { ChainId, isUnsignedTransaction, UnsignedTransaction } from "@iov/bcp";
import { isJsonCompatibleDictionary, TransactionEncoder } from "@iov/encoding";
import {
jsonRpcCode,
JsonRpcErrorResponse,
JsonRpcRequest,
JsonRpcResponse,
JsonRpcSuccessResponse,
parseJsonRpcId,
parseJsonRpcRequest,
} from "@iov/jsonrpc";
import { SigningServerCore } from "./signingservercore";
interface RpcCallGetIdentities {
readonly name: "getIdentities";
readonly reason: string;
readonly chainIds: readonly ChainId[];
}
interface RpcCallSignAndPost {
readonly name: "signAndPost";
readonly reason: string;
readonly transaction: UnsignedTransaction;
}
type RpcCall = RpcCallGetIdentities | RpcCallSignAndPost;
class ParamsError extends Error {}
class MethodNotFoundError extends Error {
public constructor(message?: string) {
super(message || "Unknown method name");
}
}
function isArrayOfStrings(array: readonly any[]): array is readonly string[] {
return array.every(element => typeof element === "string");
}
function parseRpcCall(data: JsonRpcRequest): RpcCall {
if (!isJsonCompatibleDictionary(data.params)) {
throw new Error("Request params are only supported as dictionary");
}
switch (data.method) {
case "getIdentities": {
const { reason, chainIds } = TransactionEncoder.fromJson(data.params);
if (typeof reason !== "string") {
throw new ParamsError("Parameter 'reason' must be a string");
}
if (!Array.isArray(chainIds) || !isArrayOfStrings(chainIds)) {
throw new ParamsError("Parameter 'chainIds' must be an array of strings");
}
return {
name: "getIdentities",
reason: reason,
chainIds: chainIds,
};
}
case "signAndPost": {
const { reason, transaction } = TransactionEncoder.fromJson(data.params);
if (typeof reason !== "string") {
throw new ParamsError("Parameter 'reason' must be a string");
}
if (!isUnsignedTransaction(transaction)) {
throw new ParamsError("Parameter 'transaction' does not look like an unsigned transaction");
}
return {
name: "signAndPost",
reason: reason,
transaction: transaction,
};
}
default:
throw new MethodNotFoundError();
}
}
/**
* A transport-agnostic JSON-RPC wrapper around SigningServerCore
*/
export class JsonRpcSigningServer {
private readonly core: SigningServerCore;
public constructor(core: SigningServerCore) {
this.core = core;
}
/**
* Handles a request from a possibly untrusted source.
*
* 1. Parse request as a JSON-RPC request
* 2. Convert JSON-RPC request into calls to SigningServerCore
* 3. Call SigningServerCore
* 4. Convert result to JSON-RPC response
*
* @param request The JSON-RPC request to be handled
* @param meta An arbitrary object that is passed by reference into the autorization callbacks
*/
public async handleUnchecked(request: unknown, meta?: any): Promise<JsonRpcResponse> {
let checkedRequest: JsonRpcRequest;
try {
checkedRequest = parseJsonRpcRequest(request);
} catch (error) {
const requestId = parseJsonRpcId(request);
const errorResponse: JsonRpcErrorResponse = {
jsonrpc: "2.0",
id: requestId,
error: {
code: jsonRpcCode.invalidRequest,
message: error.toString(),
},
};
return errorResponse;
}
return this.handleChecked(checkedRequest, meta);
}
/**
* Handles a checked request, i.e. a request that is known to be a valid
* JSON-RPC "Request object".
*
* 1. Convert JSON-RPC request into calls to SigningServerCore
* 2. Call SigningServerCore
* 3. Convert result to JSON-RPC response
*
* @param request The JSON-RPC request to be handled
* @param meta An arbitrary object that is passed by reference into the autorization callbacks
*/
public async handleChecked(request: JsonRpcRequest, meta?: any): Promise<JsonRpcResponse> {
let call: RpcCall;
try {
call = parseRpcCall(request);
} catch (error) {
let errorCode: number;
if (error instanceof MethodNotFoundError) {
errorCode = jsonRpcCode.methodNotFound;
} else if (error instanceof ParamsError) {
errorCode = jsonRpcCode.invalidParams;
} else {
// An unexpected error is the server's fault
errorCode = jsonRpcCode.serverError.default;
}
const errorResponse: JsonRpcErrorResponse = {
jsonrpc: "2.0",
id: request.id,
error: {
code: errorCode,
message: error.toString(),
},
};
return errorResponse;
}
try {
let response: JsonRpcSuccessResponse;
switch (call.name) {
case "getIdentities": {
const result = await this.core.getIdentities(call.reason, call.chainIds, meta);
response = {
jsonrpc: "2.0",
id: request.id,
result: TransactionEncoder.toJson(result),
};
break;
}
case "signAndPost": {
const result = await this.core.signAndPost(call.reason, call.transaction, meta);
response = {
jsonrpc: "2.0",
id: request.id,
result: TransactionEncoder.toJson(result),
};
break;
}
default:
throw new Error("Unsupported RPC call");
}
return response;
} catch (error) {
let errorCode: number;
if (error instanceof ParamsError) {
errorCode = jsonRpcCode.invalidParams;
} else if (error instanceof MethodNotFoundError) {
errorCode = jsonRpcCode.methodNotFound;
} else {
errorCode = jsonRpcCode.serverError.default;
}
const errorResponse: JsonRpcErrorResponse = {
jsonrpc: "2.0",
id: request.id,
error: {
code: errorCode,
message: error.toString(),
},
};
return errorResponse;
}
}
/**
* Call this to free ressources when server is not needed anymore
*/
public shutdown(): void {
this.core.shutdown();
}
}