-
Notifications
You must be signed in to change notification settings - Fork 29
/
User.ts
84 lines (73 loc) · 2.7 KB
/
User.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
import { RpcEndpoint, SignTransactionConfig, SignTransactionResponse } from './interfaces'
/**
* Represents a logged in User and provides the ability to sign transactions as that user.
*/
export abstract class User {
/**
* @param transaction The transaction to be signed (a object that matches the RpcAPI structure).
*/
public abstract signTransaction(transaction: any, config?: SignTransactionConfig): Promise<SignTransactionResponse>
/**
* @param publicKey The public key to use for signing.
* @param data The data to be signed.
* @param helpText Help text to explain the need for arbitrary data to be signed.
*
* @returns The signature
*/
public abstract signArbitrary(publicKey: string, data: string, helpText: string): Promise<string>
/**
* @param challenge Challenge text sent to the authenticator.
*
* @returns Whether the user owns the private keys corresponding with provided public keys.
*/
public abstract verifyKeyOwnership(challenge: string): Promise<boolean>
public abstract getAccountName(): Promise<string>
public abstract getChainId(): Promise<string>
public abstract getKeys(): Promise<string[]>
protected returnEosjsTransaction(wasBroadcast: boolean, completedTransaction: any): SignTransactionResponse {
if (wasBroadcast) {
if (completedTransaction.hasOwnProperty('transaction_id')) {
return {
wasBroadcast: true,
transactionId: completedTransaction.transaction_id,
status: completedTransaction.processed.receipt.status,
transaction: completedTransaction,
}
} else if (completedTransaction.hasOwnProperty('code')) {
return {
wasBroadcast: true,
error: {
code: completedTransaction.code,
message: completedTransaction.message,
name: completedTransaction.error.name,
},
transaction: completedTransaction,
}
} else {
return {
wasBroadcast: true,
transaction: completedTransaction,
}
}
} else {
return {
wasBroadcast: false,
transaction: completedTransaction,
}
}
}
protected buildRpcEndpoint(endPoint: RpcEndpoint): string {
let rpcEndpointString = `${endPoint.protocol}://${endPoint.host}:${endPoint.port}`
if (endPoint.path) {
let separator = '/'
if (endPoint.path.startsWith('/')) {
separator = ''
}
rpcEndpointString = `${rpcEndpointString}${separator}${endPoint.path}`
}
if (rpcEndpointString.endsWith('/')) {
rpcEndpointString = rpcEndpointString.substring(0, rpcEndpointString.length - 1)
}
return rpcEndpointString
}
}