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
|
const axios = require("axios");
const crypto = require("crypto");
app.get("/assets", ah(async (req, res) => {
try {
const { data: assets } = await axios.get(
"https://assets.blockstream.info/"
);
const accounts = await db.Account.findAll({
group: ["asset"],
});
Object.keys(assets).map((a) => (assets[a].registered = true));
accounts.map((a) => {
if (!assets[a.asset]) assets[a.asset] = a;
});
res.send(assets);
} catch (e) {
l.error("error fetching assets", e.message);
res.status(500).send("error fetching assets");
}
}));
app.post("/assets", auth, ah(async (req, res) => {
try {
const sha256 = crypto.createHash("sha256");
const { id: user_id } = req.user;
const token_address = await lq.getNewAddress("", "legacy");
const asset_address = await lq.getNewAddress("", "legacy");
const blind = false;
const info = await lq.getAddressInfo(asset_address);
const { pubkey: issuer_pubkey } = info;
const {
domain,
name,
asset_amount,
token_amount,
precision,
ticker,
} = req.body;
const version = 0;
const contract = {
entity: { domain },
issuer_pubkey,
name,
precision,
ticker,
version,
};
sha256.update(JSON.stringify(contract));
const hash = sha256.digest("hex");
const contract_hash = hash
.match(/[a-f0-9]{2}/g)
.reverse()
.join("");
const rawtx = await lq.createRawTransaction([], { data: "00" });
const funded = await lq.fundRawTransaction(rawtx, { feeRate: 0.000003 });
const params = {
asset_amount,
asset_address,
blind,
contract_hash,
};
params.asset_amount = asset_amount / (SATS / 10 ** precision);
l.info("asset_amount", params.asset_amount);
if (token_amount) {
params.token_amount = token_amount;
params.token_address = token_address;
params.token_amount = parseInt(params.token_amount);
}
let ria;
try {
ria = await lq.rawIssueAsset(funded.hex, [params]);
} catch (e) {
l.info(asset_amount, token_amount, params);
throw new Error(e.message);
}
const { asset, hex, token } = ria[0];
const brt = await lq.blindRawTransaction(hex, true, [], false);
const srt = await lq.signRawTransactionWithWallet(brt);
const allowed = (await lq.testMempoolAccept([srt.hex]))[0].allowed;
if (allowed) {
const txid = await lq.sendRawTransaction(srt.hex);
await db.transaction(async (transaction) => {
let account = await db.Account.findOne({
where: {
user_id,
asset: config.liquid.btcasset,
},
include: {
model: db.User,
as: "user",
},
lock: transaction.LOCK.UPDATE,
transaction,
});
let { user } = account;
if (Math.round(funded.fee * SATS) > account.balance) {
l.error(
"amount exceeds balance",
asset_amount,
funded.fee,
account.balance
);
throw new Error(`Insufficient funds to pay fee of ${funded.fee} BTC`);
}
account.balance -= Math.round(funded.fee * SATS);
await account.save({ transaction });
emit(user.username, "account", account);
account = await db.Account.create(
{
asset,
contract,
domain,
user_id,
ticker,
precision,
name,
balance: 0,
pending: params.asset_amount * SATS,
},
{ transaction }
);
emit(user.username, "account", account);
const asset_payment = await db.Payment.create(
{
account_id: account.id,
user_id,
hash: txid,
amount: params.asset_amount * SATS,
received: true,
confirmed: false,
address: asset_address,
network: "LBTC",
},
{ transaction }
);
emit(user.username, "payment", asset_payment);
issuances[txid] = {
user_id,
asset,
asset_amount: params.asset_amount,
asset_payment_id: asset_payment.id,
};
if (token_amount) {
account = await db.Account.create(
{
asset: token,
user_id,
domain,
ticker: `${ticker}REISSUANCETOKEN`,
precision: 8,
name: `${name} Reissuance Token`,
balance: 0,
pending: token_amount * SATS,
},
{ transaction }
);
emit(user.username, "account", account);
const token_payment = await db.Payment.create(
{
account_id: account.id,
user_id,
hash: txid,
amount: token_amount * SATS,
received: true,
confirmed: false,
address: token_address,
network: "LBTC",
},
{ transaction }
);
emit(user.username, "payment", token_payment);
issuances[txid].token = token;
issuances[txid].token_amount = token_amount;
issuances[txid].token_payment_id = token_payment.id;
}
});
res.send(issuances[txid]);
}
} catch (e) {
l.error("asset issuance failed", e.message);
res.status(500).send(e.message);
}
}));
app.post("/assets/register", auth, ah(async (req, res) => {
const { asset } = req.body;
const account = await db.Account.findOne({
where: {
user_id: req.user.id,
asset,
},
});
try {
const { data: result } = await axios.post(
"https://assets.blockstream.info/",
{
asset_id: asset,
contract: account.contract,
}
);
l.info("register asset result", req.user.username, result);
res.send(result);
} catch (e) {
l.error("asset registration failed", e.message);
res.status(500).send(e.message);
}
}));
|