-
Notifications
You must be signed in to change notification settings - Fork 7
/
stellar.ts
302 lines (273 loc) · 9.45 KB
/
stellar.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import axios from "axios";
import { Buffer } from "buffer";
import * as PATH from "path";
import { default as StellarSdk } from "stellar-sdk";
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { appPath, BackendStorage, BackendStorageType, StorageUpdateAction } from "../storage/backend";
import {
BlockchainAssetModel,
BlockchainAssetsModel,
BlockchainCreateResultModel,
BlockchainDeleteModel,
BlockchainGetModel,
BlockchainGetResultModel,
BlockchainListResultModel,
BlockchainSignModel,
StellarWalletBalanceByAddressModel,
StellarWalletCreateModel,
StellarWalletInitModel,
StellarWalletTransferModel,
StellarWalletVerifyModel,
} from ".";
import blockchainInterface, { blockchainType } from "./blockchainInterface";
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");
class Stellar implements blockchainInterface {
fileName = "stellar.json";
backendStorage: BackendStorage;
mnemonic: string;
tfClient: TFClient;
constructor(public config: GridClientConfig) {
this.mnemonic = config.mnemonic;
this.backendStorage = new BackendStorage(
config.backendStorageType,
config.substrateURL,
config.mnemonic,
config.storeSecret,
config.keypairType,
config.backendStorage,
config.seed,
);
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
}
private async saveIfKVStoreBackend(extrinsics) {
if (this.config.backendStorageType === BackendStorageType.tfkvstore && extrinsics && extrinsics.length > 0) {
extrinsics = extrinsics.filter(e => e !== undefined);
if (extrinsics.length > 0) {
await this.tfClient.connect();
await this.tfClient.applyAllExtrinsics(extrinsics);
}
}
}
async _load() {
const path = PATH.join(appPath, this.fileName);
let data = await this.backendStorage.load(path);
if (!data) {
data = {};
}
return [path, data];
}
async save(name: string, secret: string) {
const [path, data] = await this._load();
if (data[name]) {
throw Error(`A wallet with the same name ${name} already exists`);
}
const updateOperations = await this.backendStorage.update(path, name, secret);
await this.saveIfKVStoreBackend(updateOperations);
}
async getWalletSecret(name: string) {
const [, data] = await this._load();
if (!data[name]) {
throw Error(`Couldn't find a wallet with name ${name}`);
}
return data[name];
}
@expose
@validateInput
async create(options: StellarWalletCreateModel): Promise<BlockchainCreateResultModel> {
const account_exists = await this.exist({ name: options.name });
if (account_exists) throw Error(`Name ${options.name} already exists`);
const account = StellarSdk.Keypair.random();
const publicKey = account.publicKey();
try {
await axios.get(`https://friendbot.stellar.org?addr=${encodeURIComponent(publicKey)}`);
} catch (e) {
throw Error(`An error happened while creating your account. ${e}`);
}
await this.save(options.name, account.secret());
return {
name: options.name,
public_key: publicKey,
secret: account.secret(),
blockchain_type: blockchainType.stellar,
};
}
@expose
@validateInput
async sign(options: BlockchainSignModel) {
const secret = await this.getWalletSecret(options.name);
const walletKeypair = StellarSdk.Keypair.fromSecret(secret);
const signed_content = walletKeypair.sign(options.content);
return Buffer.from(signed_content).toString("hex");
}
@expose
@validateInput
async verify(options: StellarWalletVerifyModel) {
const walletKeypair = StellarSdk.Keypair.fromPublicKey(options.public_key);
return walletKeypair.verify(options.content, Buffer.from(options.signedContent, "hex"));
}
@expose
@validateInput
async init(options: StellarWalletInitModel) {
const walletKeypair = StellarSdk.Keypair.fromSecret(options.secret);
const walletPublicKey = walletKeypair.publicKey();
await server.loadAccount(walletPublicKey);
await this.save(options.name, options.secret);
return walletPublicKey;
}
@expose
@validateInput
async get(options: BlockchainGetModel): Promise<BlockchainGetResultModel> {
const secret = await this.getWalletSecret(options.name);
const walletKeypair = StellarSdk.Keypair.fromSecret(secret);
return {
name: options.name,
public_key: walletKeypair.publicKey(),
secret: secret,
blockchain_type: blockchainType.stellar,
};
// TODO: return wallet secret after adding security context on the server calls
}
@expose
@validateInput
async update(options: StellarWalletInitModel) {
if (!(await this.exist(options))) {
throw Error(`Couldn't find a wallet with name ${options.name} to update`);
}
const secret = await this.getWalletSecret(options.name);
const deleteWallet = new BlockchainDeleteModel();
deleteWallet.name = options.name;
await this.delete(deleteWallet);
try {
return await this.init(options);
} catch (e) {
const oldSecret = options.secret;
options.secret = secret;
await this.init(options);
throw Error(`Couldn't import wallet with the secret ${oldSecret} due to: ${e}`);
}
}
@expose
@validateInput
async exist(options: BlockchainGetModel) {
return (await this.list()).map(account => account.name == options.name).includes(true);
}
@expose
@validateInput
async list(): Promise<BlockchainListResultModel[]> {
const [, data] = await this._load();
const accounts = [];
for (const [name, secret] of Object.entries(data)) {
accounts.push({
name: name,
public_key: StellarSdk.Keypair.fromSecret(secret).publicKey(),
blockchain_type: blockchainType.stellar,
});
}
return accounts;
}
@expose
@validateInput
async assets(options: BlockchainGetModel): Promise<BlockchainAssetsModel> {
const secret = await this.getWalletSecret(options.name);
if (!secret) {
throw Error(`Couldn't find a wallet with name ${options.name}`);
}
const walletKeypair = StellarSdk.Keypair.fromSecret(secret);
const walletPublicKey = walletKeypair.publicKey();
const walletAddress = new StellarWalletBalanceByAddressModel();
walletAddress.address = walletPublicKey;
const balances = await this.balance_by_address(walletAddress);
return {
name: options.name,
public_key: walletPublicKey,
blockchain_type: blockchainType.stellar,
assets: balances ? balances : [],
};
}
@expose
@validateInput
async balance_by_address(options: StellarWalletBalanceByAddressModel): Promise<BlockchainAssetModel[]> {
const account = await server.loadAccount(options.address);
const balances = [];
for (const balance of account.balances) {
if (!balance.asset_code) {
balance.asset_code = "XLM";
}
balances.push({ asset: balance.asset_code, amount: balance.balance });
}
return balances;
}
@expose
@validateInput
async pay(options: StellarWalletTransferModel) {
const secret = await this.getWalletSecret(options.name);
if (!secret) {
throw Error(`Couldn't find a wallet with name ${options.name}`);
}
const sourceKeypair = StellarSdk.Keypair.fromSecret(secret);
const sourcePublicKey = sourceKeypair.publicKey();
const sourceAccount = await server.loadAccount(sourcePublicKey);
let asset;
if (options.asset != "XLM") {
let issuer;
for (const balance of sourceAccount.balances) {
if (balance.asset_code === options.asset) {
issuer = balance.asset_issuer;
}
}
if (!issuer) {
throw Error(`couldn't find this asset ${options.asset} on source wallet`);
}
asset = new StellarSdk.Asset(options.asset, issuer);
} else {
asset = StellarSdk.Asset.native();
}
const fee = await server.fetchBaseFee();
const memo = StellarSdk.Memo.text(options.description);
const transaction = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: fee,
networkPassphrase: StellarSdk.Networks.TESTNET,
memo: memo,
})
.addOperation(
StellarSdk.Operation.payment({
destination: options.address_dest,
asset: asset,
amount: options.amount.toString(),
}),
)
.setTimeout(30)
.build();
transaction.sign(sourceKeypair);
console.log(transaction.toEnvelope().toXDR("base64"));
try {
const transactionResult = await server.submitTransaction(transaction);
console.log(JSON.stringify(transactionResult, null, 2));
console.log("Success! View the transaction at: ", transactionResult._links.transaction.href);
return transactionResult._links.transaction.href;
} catch (e) {
console.log("An error has occured:", e);
throw Error(e);
}
}
@expose
@validateInput
async delete(options: BlockchainDeleteModel) {
const [path, data] = await this._load();
if (!data[options.name]) {
throw Error(`Couldn't find a wallet with name ${options.name}`);
}
const updateOperations = await this.backendStorage.update(path, options.name, "", StorageUpdateAction.delete);
await this.saveIfKVStoreBackend(updateOperations);
return "Deleted";
}
}
export { Stellar as stellar };