This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
class.js
114 lines (108 loc) · 3.73 KB
/
class.js
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
/* @flow */
import GenericWallet from '@colony/purser-core/genericWallet';
import { userInputValidator } from '@colony/purser-core/helpers';
import { warning } from '@colony/purser-core/utils';
import { DESCRIPTORS, REQUIRED_PROPS } from '@colony/purser-core/defaults';
import { TYPE_HARDWARE, SUBTYPE_TREZOR } from '@colony/purser-core/types';
import type {
TransactionObjectType,
GenericClassArgumentsType,
} from '@colony/purser-core/flowtypes';
import { signTransaction, signMessage, verifyMessage } from './staticMethods';
import { classInstance as messages } from './messages';
import { REQUIRED_PROPS as REQUIRED_TREZOR_PROPS } from './defaults';
const { WALLET_PROPS } = DESCRIPTORS;
export default class TrezorWallet extends GenericWallet {
constructor(propObject: GenericClassArgumentsType) {
super(propObject);
Object.defineProperties(this, {
/*
* Set the actual type and subtype (overwrite the generic ones)
*/
type: Object.assign({}, { value: TYPE_HARDWARE }, WALLET_PROPS),
subtype: Object.assign({}, { value: SUBTYPE_TREZOR }, WALLET_PROPS),
sign: Object.assign(
{},
{
value: async (transactionObject: TransactionObjectType) => {
let requiredSignProps: Array<String> =
REQUIRED_TREZOR_PROPS.SIGN_TRANSACTION;
const { chainId = this.chainId, to } = transactionObject || {};
/*
* If we don't have a destination address, it means the user wants
* to deploy a contract.
*
* For this the Trezor service requires a `inputData` value set.
*
* Otherwise, a `to` address *must* be set.
*/
if (!to) {
requiredSignProps =
REQUIRED_TREZOR_PROPS.SIGN_TRANSACTION_CONTRACT;
/*
* Warn the user (in dev mode, at least) about Trezor's contract
* deployment requirements
*/
warning(messages.signContractDeployment);
}
/*
* Validate the trasaction's object input
*/
userInputValidator({
firstArgument: transactionObject,
requiredAll: requiredSignProps,
});
return signTransaction(
Object.assign({}, transactionObject, {
derivationPath: await this.derivationPath,
chainId,
}),
);
},
},
WALLET_PROPS,
),
signMessage: Object.assign(
{},
{
value: async (messageObject: Object = {}) => {
/*
* Validate the trasaction's object input
*/
userInputValidator({
firstArgument: messageObject,
requiredOr: REQUIRED_PROPS.SIGN_MESSAGE,
});
return signMessage({
derivationPath: await this.derivationPath,
message: messageObject.message,
messageData: messageObject.messageData,
});
},
},
WALLET_PROPS,
),
verifyMessage: Object.assign(
{},
{
value: async (signatureVerificationObject: Object = {}) => {
/*
* Validate the trasaction's object input
*/
userInputValidator({
firstArgument: signatureVerificationObject,
requiredAll: REQUIRED_PROPS.VERIFY_MESSAGE,
});
const { message, signature } = signatureVerificationObject;
return verifyMessage({
address: this.address,
message,
signature,
});
},
},
WALLET_PROPS,
),
});
}
}