MoralisDot.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  10. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  11. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  12. var _ParseACL = _interopRequireDefault(require("./ParseACL"));
  13. var _createSigningData = _interopRequireDefault(require("./createSigningData"));
  14. var web3EnablePromise = null;
  15. var MoralisDot = function () {
  16. function MoralisDot() {
  17. (0, _classCallCheck2.default)(this, MoralisDot);
  18. }
  19. (0, _createClass2.default)(MoralisDot, null, [{
  20. key: "web3IsInjected",
  21. value: function () {
  22. return Object.keys(window.injectedWeb3).length !== 0;
  23. }
  24. }, {
  25. key: "enable",
  26. value: function (opts) {
  27. var _window$injectedWeb, _window$injectedWeb$t;
  28. var type,
  29. _args = arguments;
  30. return _regenerator.default.async(function (_context) {
  31. while (1) {
  32. switch (_context.prev = _context.next) {
  33. case 0:
  34. type = _args.length > 1 && _args[1] !== undefined ? _args[1] : 'polkadot-js';
  35. if (!web3EnablePromise) {
  36. _context.next = 3;
  37. break;
  38. }
  39. return _context.abrupt("return", web3EnablePromise);
  40. case 3:
  41. web3EnablePromise = (_window$injectedWeb = window.injectedWeb3) == null ? void 0 : (_window$injectedWeb$t = _window$injectedWeb[type]) == null ? void 0 : _window$injectedWeb$t.enable(opts);
  42. return _context.abrupt("return", web3EnablePromise);
  43. case 5:
  44. case "end":
  45. return _context.stop();
  46. }
  47. }
  48. }, null, null, null, Promise);
  49. }
  50. }, {
  51. key: "authenticate",
  52. value: function (opts) {
  53. var _opts$name;
  54. var allAccounts, account, address, dotAddress, accounts, message, data, signature, authData, user;
  55. return _regenerator.default.async(function (_context2) {
  56. while (1) {
  57. switch (_context2.prev = _context2.next) {
  58. case 0:
  59. _context2.next = 2;
  60. return _regenerator.default.awrap(MoralisDot.enable((_opts$name = opts == null ? void 0 : opts.name) != null ? _opts$name : 'Moralis'));
  61. case 2:
  62. MoralisDot.web3 = _context2.sent;
  63. _context2.next = 5;
  64. return _regenerator.default.awrap(MoralisDot.web3.accounts.get());
  65. case 5:
  66. allAccounts = _context2.sent;
  67. account = allAccounts[0];
  68. address = account == null ? void 0 : account.address;
  69. if (address) {
  70. _context2.next = 10;
  71. break;
  72. }
  73. throw new Error('Address not found');
  74. case 10:
  75. dotAddress = address;
  76. accounts = [dotAddress];
  77. message = MoralisDot.getSigningData();
  78. _context2.next = 15;
  79. return _regenerator.default.awrap((0, _createSigningData.default)(message));
  80. case 15:
  81. data = _context2.sent;
  82. _context2.next = 18;
  83. return _regenerator.default.awrap(MoralisDot.sign(address, data));
  84. case 18:
  85. signature = _context2.sent;
  86. authData = {
  87. id: dotAddress,
  88. signature: signature,
  89. data: data
  90. };
  91. _context2.next = 22;
  92. return _regenerator.default.awrap(_ParseUser.default.logInWith('moralisDot', {
  93. authData: authData
  94. }));
  95. case 22:
  96. user = _context2.sent;
  97. if (user) {
  98. _context2.next = 25;
  99. break;
  100. }
  101. throw new Error('Could not get user');
  102. case 25:
  103. _context2.next = 27;
  104. return _regenerator.default.awrap(user.setACL(new _ParseACL.default(user)));
  105. case 27:
  106. user.addAllUnique('dotAccounts', accounts);
  107. user.set('dotAddress', dotAddress);
  108. _context2.next = 31;
  109. return _regenerator.default.awrap(user.save());
  110. case 31:
  111. return _context2.abrupt("return", user);
  112. case 32:
  113. case "end":
  114. return _context2.stop();
  115. }
  116. }
  117. }, null, null, null, Promise);
  118. }
  119. }, {
  120. key: "link",
  121. value: function (account, options) {
  122. var message, user, dotAddress, DotAddress, query, dotAddressRecord, data, signature, authData;
  123. return _regenerator.default.async(function (_context3) {
  124. while (1) {
  125. switch (_context3.prev = _context3.next) {
  126. case 0:
  127. message = (options == null ? void 0 : options.signingMessage) || MoralisDot.getSigningData();
  128. _context3.next = 3;
  129. return _regenerator.default.awrap(_ParseUser.default.current());
  130. case 3:
  131. user = _context3.sent;
  132. dotAddress = account;
  133. DotAddress = _ParseObject.default.extend('_DotAddress');
  134. query = new _ParseQuery.default(DotAddress);
  135. _context3.next = 9;
  136. return _regenerator.default.awrap(query.get(dotAddress).catch(function () {
  137. return null;
  138. }));
  139. case 9:
  140. dotAddressRecord = _context3.sent;
  141. if (dotAddressRecord) {
  142. _context3.next = 20;
  143. break;
  144. }
  145. _context3.next = 13;
  146. return _regenerator.default.awrap((0, _createSigningData.default)(message));
  147. case 13:
  148. data = _context3.sent;
  149. _context3.next = 16;
  150. return _regenerator.default.awrap(MoralisDot.sign(dotAddress, data));
  151. case 16:
  152. signature = _context3.sent;
  153. authData = {
  154. id: dotAddress,
  155. signature: signature,
  156. data: data
  157. };
  158. _context3.next = 20;
  159. return _regenerator.default.awrap(user.linkWith('moralisDot', {
  160. authData: authData
  161. }));
  162. case 20:
  163. user.addAllUnique('dotAccounts', [dotAddress]);
  164. user.set('dotAddress', dotAddress);
  165. _context3.next = 24;
  166. return _regenerator.default.awrap(user.save());
  167. case 24:
  168. return _context3.abrupt("return", user);
  169. case 25:
  170. case "end":
  171. return _context3.stop();
  172. }
  173. }
  174. }, null, null, null, Promise);
  175. }
  176. }, {
  177. key: "unlink",
  178. value: function (account) {
  179. var _user$get;
  180. var accountsLower, DotAddress, query, dotAddressRecord, user, accounts, nextAccounts;
  181. return _regenerator.default.async(function (_context4) {
  182. while (1) {
  183. switch (_context4.prev = _context4.next) {
  184. case 0:
  185. accountsLower = account;
  186. DotAddress = _ParseObject.default.extend('_DotAddress');
  187. query = new _ParseQuery.default(DotAddress);
  188. _context4.next = 5;
  189. return _regenerator.default.awrap(query.get(accountsLower));
  190. case 5:
  191. dotAddressRecord = _context4.sent;
  192. _context4.next = 8;
  193. return _regenerator.default.awrap(dotAddressRecord.destroy());
  194. case 8:
  195. _context4.next = 10;
  196. return _regenerator.default.awrap(_ParseUser.default.current());
  197. case 10:
  198. user = _context4.sent;
  199. accounts = (_user$get = user.get('dotAccounts')) != null ? _user$get : [];
  200. nextAccounts = accounts.filter(function (v) {
  201. return v !== accountsLower;
  202. });
  203. user.set('dotAccounts', nextAccounts);
  204. user.set('dotAddress', nextAccounts[0]);
  205. _context4.next = 17;
  206. return _regenerator.default.awrap(user._unlinkFrom('moralisDot'));
  207. case 17:
  208. _context4.next = 19;
  209. return _regenerator.default.awrap(user.save());
  210. case 19:
  211. return _context4.abrupt("return", user);
  212. case 20:
  213. case "end":
  214. return _context4.stop();
  215. }
  216. }
  217. }, null, null, null, Promise);
  218. }
  219. }, {
  220. key: "sign",
  221. value: function (address, data) {
  222. var web3, _await$web3$signer$si, signature;
  223. return _regenerator.default.async(function (_context5) {
  224. while (1) {
  225. switch (_context5.prev = _context5.next) {
  226. case 0:
  227. if (web3EnablePromise) {
  228. _context5.next = 2;
  229. break;
  230. }
  231. throw new Error('Must enable MoralisDot');
  232. case 2:
  233. _context5.next = 4;
  234. return _regenerator.default.awrap(web3EnablePromise);
  235. case 4:
  236. web3 = _context5.sent;
  237. if (web3.signer) {
  238. _context5.next = 7;
  239. break;
  240. }
  241. throw new Error('No signer found');
  242. case 7:
  243. _context5.next = 9;
  244. return _regenerator.default.awrap(web3.signer.signRaw({
  245. address: address,
  246. data: stringToHex(data),
  247. type: 'bytes'
  248. }));
  249. case 9:
  250. _await$web3$signer$si = _context5.sent;
  251. signature = _await$web3$signer$si.signature;
  252. return _context5.abrupt("return", signature);
  253. case 12:
  254. case "end":
  255. return _context5.stop();
  256. }
  257. }
  258. }, null, null, null, Promise);
  259. }
  260. }, {
  261. key: "getSigningData",
  262. value: function () {
  263. return 'Moralis Authentication';
  264. }
  265. }]);
  266. return MoralisDot;
  267. }();
  268. var _default = MoralisDot;
  269. exports.default = _default;
  270. function stringToHex(value) {
  271. return toHexString(stringToU8a(value));
  272. }
  273. function stringToU8a(value) {
  274. var u8a = new Uint8Array(value.length);
  275. for (var i = 0; i < value.length; i++) {
  276. u8a[i] = value.charCodeAt(i);
  277. }
  278. return u8a;
  279. }
  280. function toHexString(byteArray) {
  281. return "0x" + Array.from(byteArray, function (byte) {
  282. return ("0" + (byte & 0xff).toString(16)).slice(-2);
  283. }).join('');
  284. }