forked from trezor/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
197 lines (176 loc) · 5.89 KB
/
plugin.js
File metadata and controls
197 lines (176 loc) · 5.89 KB
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
const StellarSdk = require('stellar-sdk');
const BigNumber = require('bignumber.js');
/**
* Transforms StellarSdk.Signer to TrezorConnect.StellarTransaction.Signer
* @param {StellarSdk.Signer} signer
* @returns { type: 1 | 2 | 3, key: string, weight: number }
*/
const transformSigner = (signer) => {
let type = 0;
let key;
const weight = signer.weight;
if (typeof signer.ed25519PublicKey === 'string') {
const keyPair = StellarSdk.Keypair.fromPublicKey(signer.ed25519PublicKey);
key = keyPair.rawPublicKey().toString('hex');
}
if (signer.preAuthTx instanceof Buffer) {
type = 1;
key = signer.preAuthTx.toString('hex');
}
if (signer.sha256Hash instanceof Buffer) {
type = 2;
key = signer.sha256Hash.toString('hex');
}
return {
type,
key,
weight,
};
};
/**
* Transforms StellarSdk.Asset to TrezorConnect.StellarTransaction.Asset
* @param {StellarSdk.Asset} asset
* @returns { type: 0 | 1 | 2, code: string, issuer?: string }
*/
const transformAsset = (asset) => {
if (asset.isNative()) {
return {
type: 0,
code: asset.getCode(),
};
}
return {
type: asset.getAssetType() === 'credit_alphanum4' ? 1 : 2,
code: asset.getCode(),
issuer: asset.getIssuer(),
};
};
/**
* Transforms amount from decimals (lumens) to integer (stroop)
* @param {string} amount
* @returns {string}
*/
const transformAmount = (amount) => {
return new BigNumber(amount).times(10000000).toString();
};
/**
* Transforms StellarSdk.Operation.type to TrezorConnect.StellarTransaction.Operation.type
* @param {string} type
* @returns {string}
*/
const transformType = (type) => {
switch (type) {
case 'pathPaymentStrictReceive':
// case 'pathPaymentStrictSend':
return 'pathPayment';
case 'createPassiveSellOffer':
return 'createPassiveOffer';
case 'manageSellOffer':
// case 'manageBuyOffer':
return 'manageOffer';
default:
return type;
}
};
/**
* Transforms StellarSdk.Memo to TrezorConnect.StellarTransaction.Memo
* @param {string} type
* @returns {string}
*/
const transformMemo = (memo) => {
switch (memo.type) {
case StellarSdk.MemoText:
return { type: 1, text: memo.value };
case StellarSdk.MemoID:
return { type: 2, id: memo.value };
case StellarSdk.MemoHash:
// stringify is not necessary, Buffer is also accepted
return { type: 3, hash: memo.value.toString('hex') };
case StellarSdk.MemoReturn:
// stringify is not necessary, Buffer is also accepted
return { type: 4, hash: memo.value.toString('hex') };
default:
return { type: 0 };
}
};
/**
* Transforms StellarSdk.Transaction.timeBounds to TrezorConnect.StellarTransaction.timebounds
* @param {string} path
* @param {StellarSdk.Transaction.timeBounds} timebounds
* @returns {minTime: number, maxTime: number}
*/
const transformTimebounds = (timebounds) => {
if (!timebounds) return undefined;
// those values are defined in Trezor firmware messages as numbers
return {
minTime: Number.parseInt(timebounds.minTime, 10),
maxTime: Number.parseInt(timebounds.maxTime, 10),
};
};
/**
* Transforms StellarSdk.Transaction to TrezorConnect.StellarTransaction
* @param {string} path
* @param {StellarSdk.Transaction} transaction
* @returns {TrezorConnect.StellarTransaction}
*/
const transformTransaction = (path, transaction) => {
const amounts = ['amount', 'sendMax', 'destAmount', 'startingBalance', 'limit'];
const assets = ['asset', 'sendAsset', 'destAsset', 'selling', 'buying', 'line'];
const operations = transaction.operations.map((o, i) => {
const operation = Object.assign({}, o);
// transform StellarSdk.Signer
if (operation.signer) {
operation.signer = transformSigner(operation.signer);
}
// transform asset path
if (operation.path) {
operation.path = operation.path.map(transformAsset);
}
// transform "price" field to { n: number, d: number }
if (typeof operation.price === 'string') {
const xdrOperation = transaction.tx.operations()[i];
operation.price = {
n: xdrOperation.body().value().price().n(),
d: xdrOperation.body().value().price().d(),
};
}
// transform amounts
amounts.forEach(field => {
if (typeof operation[field] === 'string') {
operation[field] = transformAmount(operation[field]);
}
});
// transform assets
assets.forEach(field => {
if (operation[field]) {
operation[field] = transformAsset(operation[field]);
}
});
// add missing field
if (operation.type === 'allowTrust') {
const allowTrustAsset = new StellarSdk.Asset(operation.assetCode, operation.trustor);
operation.assetType = transformAsset(allowTrustAsset).type;
}
if (operation.type === 'manageData' && operation.value) {
// stringify is not necessary, Buffer is also accepted
operation.value = operation.value.toString('hex');
}
// transform type
operation.type = transformType(o.type);
return operation;
});
return {
path,
networkPassphrase: transaction.networkPassphrase,
transaction: {
source: transaction.source,
fee: transaction.fee,
sequence: transaction.sequence,
memo: transformMemo(transaction.memo),
timebounds: transformTimebounds(transaction.timeBounds),
operations,
},
};
};
exports['default'] = transformTransaction;
module.exports = exports.default;