aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-07-27 11:34:17 -0700
committerAdam Soltys <[email protected]>2020-07-27 11:35:36 -0700
commit3d7b27aa81165c15658f46687a2748d126c17adb (patch)
treeef737f641657501683f9f035e3aad28d49668795
parentbfb643b82b559cb4ee78c6501d339920c6df8176 (diff)
downloadcoinos-server-3d7b27aa81165c15658f46687a2748d126c17adb.tar.gz
coinos-server-3d7b27aa81165c15658f46687a2748d126c17adb.zip
asset registration
-rw-r--r--db/models/accounts.js15
-rw-r--r--db/schema.sql15
-rw-r--r--routes/assets.js63
3 files changed, 71 insertions, 22 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js
index 470ca842..5a6f0bd3 100644
--- a/db/models/accounts.js
+++ b/db/models/accounts.js
@@ -46,6 +46,21 @@ const attributes = {
comment: null,
field: "name"
},
+ contract: {
+ type: DataTypes.TEXT,
+ get: function() {
+ return JSON.parse(this.getDataValue("contract"));
+ },
+ set: function(value) {
+ return this.setDataValue("contract", JSON.stringify(value));
+ },
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "contract"
+ },
domain: {
type: DataTypes.STRING(255),
allowNull: true,
diff --git a/db/schema.sql b/db/schema.sql
index ff272a43..9d1feaa3 100644
--- a/db/schema.sql
+++ b/db/schema.sql
@@ -56,8 +56,10 @@ CREATE TABLE `accounts` (
`name` varchar(255) DEFAULT NULL,
`ticker` varchar(255) DEFAULT NULL,
`precision` int(11) DEFAULT NULL,
+ `domain` varchar(255) DEFAULT NULL,
+ `contract` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=3731 DEFAULT CHARSET=latin1;
+) ENGINE=InnoDB AUTO_INCREMENT=3750 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -98,7 +100,7 @@ CREATE TABLE `invoices` (
`uuid` varchar(255) DEFAULT NULL,
`memo` text DEFAULT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=4644 DEFAULT CHARSET=latin1;
+) ENGINE=InnoDB AUTO_INCREMENT=4672 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -115,7 +117,7 @@ CREATE TABLE `linkingkeys` (
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=4497 DEFAULT CHARSET=latin1;
+) ENGINE=InnoDB AUTO_INCREMENT=4498 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -144,7 +146,7 @@ CREATE TABLE `payments` (
`preimage` varchar(255) DEFAULT NULL,
`memo` text DEFAULT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=4459 DEFAULT CHARSET=latin1;
+) ENGINE=InnoDB AUTO_INCREMENT=4572 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -165,8 +167,9 @@ CREATE TABLE `proposals` (
`updatedAt` datetime NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT 0,
`text` text DEFAULT NULL,
+ `public` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
+) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -226,4 +229,4 @@ CREATE TABLE `users` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
--- Dump completed on 2020-07-20 21:16:00
+-- Dump completed on 2020-07-27 11:35:33
diff --git a/routes/assets.js b/routes/assets.js
index 5f838992..c9acf245 100644
--- a/routes/assets.js
+++ b/routes/assets.js
@@ -3,18 +3,22 @@ const crypto = require("crypto");
app.get("/assets", async (req, res) => {
try {
- const { data: assets } = await axios.get("https://assets.blockstream.info/");
+ const { data: assets } = await axios.get(
+ "https://assets.blockstream.info/"
+ );
const accounts = await db.Account.findAll({
- group: ['asset'],
+ group: ["asset"],
});
- accounts.map(a => {
+ 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);
+ l.error("error fetching assets", e.message);
res.status(500).send("error fetching assets");
}
});
@@ -29,7 +33,14 @@ app.post("/assets", auth, async (req, res) => {
const info = await lq.getAddressInfo(asset_address);
const { pubkey: issuer_pubkey } = info;
- const { domain, name, asset_amount, token_amount, precision, ticker } = req.body;
+ const {
+ domain,
+ name,
+ asset_amount,
+ token_amount,
+ precision,
+ ticker,
+ } = req.body;
const version = 0;
const contract = {
@@ -39,7 +50,7 @@ app.post("/assets", auth, async (req, res) => {
name,
precision,
ticker,
- version
+ version,
};
sha256.update(JSON.stringify(contract));
@@ -54,7 +65,7 @@ app.post("/assets", auth, async (req, res) => {
asset_amount,
asset_address,
blind,
- contract_hash
+ contract_hash,
};
params.asset_amount = parseInt(params.asset_amount);
@@ -80,18 +91,18 @@ app.post("/assets", auth, async (req, res) => {
if (allowed) {
const txid = await lq.sendRawTransaction(srt.hex);
- await db.transaction(async transaction => {
+ await db.transaction(async (transaction) => {
let account = await db.Account.findOne({
where: {
user_id,
- asset: config.liquid.btcasset
+ asset: config.liquid.btcasset,
},
include: {
model: db.User,
- as: "user"
+ as: "user",
},
lock: transaction.LOCK.UPDATE,
- transaction
+ transaction,
});
let { user } = account;
@@ -113,13 +124,14 @@ app.post("/assets", auth, async (req, res) => {
account = await db.Account.create(
{
asset,
+ contract,
domain,
user_id,
ticker,
precision,
name,
balance: 0,
- pending: asset_amount * SATS
+ pending: asset_amount * SATS,
},
{ transaction }
);
@@ -135,7 +147,7 @@ app.post("/assets", auth, async (req, res) => {
received: true,
confirmed: false,
address: asset_address,
- network: "LBTC"
+ network: "LBTC",
},
{ transaction }
);
@@ -145,7 +157,7 @@ app.post("/assets", auth, async (req, res) => {
user_id,
asset,
asset_amount,
- asset_payment_id: asset_payment.id
+ asset_payment_id: asset_payment.id,
};
if (token_amount) {
@@ -158,7 +170,7 @@ app.post("/assets", auth, async (req, res) => {
precision: 8,
name: `${name} Reissuance Token`,
balance: 0,
- pending: token_amount * SATS
+ pending: token_amount * SATS,
},
{ transaction }
);
@@ -173,7 +185,7 @@ app.post("/assets", auth, async (req, res) => {
received: true,
confirmed: false,
address: token_address,
- network: "LBTC"
+ network: "LBTC",
},
{ transaction }
);
@@ -192,3 +204,22 @@ app.post("/assets", auth, async (req, res) => {
res.status(500).send(e.message);
}
});
+
+app.post("/assets/register", auth, 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);
+ }
+});