-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTokenPocketUser.ts
97 lines (84 loc) · 2.26 KB
/
TokenPocketUser.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
import tp from 'tp-eosjs'
import {
Chain,
SignTransactionConfig,
SignTransactionResponse,
UALErrorType,
User
} from 'universal-authenticator-library'
import { EosAuthSignResponse, PushEosActionResponse, Wallet } from './interfaces'
import { UALTokenPocketError } from './UALTokenPocketError'
export class TokenPocketUser extends User {
private wallet: Wallet
private keys: string[] = []
private chainId = ''
constructor(
chain: Chain | null,
wallet: Wallet
) {
super()
this.wallet = wallet
if (chain && chain.chainId) {
this.chainId = chain.chainId
}
}
public async signTransaction(
transaction: any,
_config: SignTransactionConfig
): Promise<SignTransactionResponse> {
let response: PushEosActionResponse
try {
response = await tp.pushEosAction({ ...transaction, account: this.wallet.name, address: this.wallet.address})
if (response.result) {
return {
wasBroadcast: true,
transactionId: response.data.transactionId,
transaction,
}
} else {
throw new Error('No result returned')
}
} catch (e) {
throw new UALTokenPocketError(
'Unable to sign the given transaction',
UALErrorType.Signing,
e)
}
}
public async signArbitrary(
publicKey: string,
data: string,
_helpText: string
): Promise<string> {
let response: EosAuthSignResponse
try {
response = await tp.eosAuthSign({ from: this.wallet.name, publicKey, signdata: data })
if (response.result) {
return response.data.signature
} else {
throw new Error('No result returned')
}
} catch (e) {
throw new UALTokenPocketError(
'Unable to sign arbitrary string',
UALErrorType.Signing,
e
)
}
}
public async verifyKeyOwnership(_: string): Promise<boolean> {
throw new Error('Token Pocket does not currently support verifyKeyOwnership')
}
public async getAccountName(): Promise<string> {
return this.wallet.name
}
public async getChainId(): Promise<string> {
return this.chainId
}
public async getKeys(): Promise<string[]> {
if (this.keys.length === 0) {
this.keys.push(this.wallet.address)
}
return this.keys
}
}