-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
136 lines (114 loc) · 8.1 KB
/
index.js
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
'use strict';
const axios = require("axios");
function YatMoneroLookup(opts = {}) {
this.validEmojis = [];
// this.debugMode denotes whether we use the live or the test API url
this.debugMode = (opts.debugMode !== undefined) ? opts.debugMode : false;
// this.remoteLookup determines whether we should retrieve the list of emojis from the Yat server
this.remoteLookup = (opts.remoteLookup !== undefined) ? opts.remoteLookup : false;
this.staticEmojiList = ["🐶","🍼","💃","🏦","🔫","📷","🔦","📡","🔔","🍷","💼","🎛️","🤧","✍️","🥒","💥","🤡","💺","🔋","💯","🐬","🕉️","📺","💾","🗽","🍦","🌴","🦂","☦️","🐭","📦","👘","🍈","😍","🎾","🎂","🗿","🍐","👃","♒","📻","☪️","✨","⚾","🥃","🔮","🐽","🌙","😢","🍤","👕","🐯","🍡","🏎️","⛄","🐱","🎐","🗺️","🍪","🤘","⚛️","🏐","🤐","🎹","🗾","🎏","🎨","🤔","😵","👶","🥝","🥗","♉","🏖️","🗞️","🍾","🎃","🆘","🎋","🐙","🎈","💨","🕸️","🚪","☄️","✉️","🐾","🍗","💡","🎤","🍿","♣️","🐛","🛵","🍳","🖨️","🎢","🧀","🏕️","🚦","🌭","🔒","🦍","💍","⚙️","📌","🤝","👽","🆚","🎠","🛍️","🏀","🏏","🐀","🐧","👎","👗","🖖","💩","🗡️","🤖","🐵","🛒","🍭","🔪","📖","🍔","🚚","✡️","🐉","🤠","🏸","❗","😱","🐌","🤑","💪","👏","☀️","🍑","🎀","🆕","😷","🆒","☢️","👻","🦉","⛵","🦀","🎳","📏","🆔","🎸","👣","🍉","✊","🏈","🏹","🦋","☁️","🌈","✂️","🌕","📟","🥛","🏮","🏓","🍽️","💵","🎭","🍱","🕹️","🗄️","🚜","🎻","💊","⌚","🦄","🛋️","🌊","🐊","🥄","🐣","🎰","🚒","👁️","🐮","🕯️","🃏","🐋","🍶","🖍️","🚽","👌","🍇","🎉","😇","🍍","⭐","🙃","🦅","💦","🍕","🏺","🍥","🏆","🚓","📈","💐","🌪️","🍩","🌻","🎥","🀄","🎮","🛢️","👍","🚢","🛡️","🦃","💄","🎷","✏️","🕌","👟","♊","🥁","✌️","⚖️","🗼","❤️","👀","🥞","✈️","🤕","🏁","♟️","🎧","♏","👾","🐗","🎼","🐪","📱","🐜","🐐","🚧","🌮","🐼","🍣","🌯","🦈","🔥","🆓","🐑","🎖️","🥊","⛳","💈","🥙","🤳","🐰","⚜️","🏟️","🎒","🥑","🍺","🎿","🐚","🎎","👛","🚰","💱","🦎","🎁","👒","🎽","👂","🥚","😘","♎","👑","🍀","🍓","🎵","⛪","🏒","😶","🍋","👞","🎣","💅","⚰️","🎩","🍄","🍌","👉","🏰","🍁","❄️","🍬","🚂","🏧","🐨","🚿","🕎","🥜","🔬","🥅","🚭","⚽","💻","🗑️","⏰","♓","😂","🎲","🦁","🤓","♠️","🐝","🥕","🦏","⚠️","💋","🏥","♻️","🛶","👙","😜","🎡","♌","🚠","💰","🐸","🔱","⛰️","📐","🍆","☯️","🚀","🐺","🍜","👠","🎯","🍵","🏯","🦇","🤢","🍊","🌵","💳","🌶️","🍫","✝️","♋","♐","💔","♑","📿","🦆","🥐","🍝","🌰","🍟","🎱","🌽","🏛️","🙏","🍯","🥔","🚫","🖼️","🏭","🍸","🎺","🙌","🔌","⛸️","💣","⚓","☠️","🙈","🐷","☕","☸️","🔑","♈","🍒","🍎","📜","🦊","🚁","🍞","🐃","🎬","⌛","🍘","🐘","🌸","👖","😎","🏠","♍","🕳️","🚗","🍚","💉","🚬","🔧","🌹","🔩","🚑","🥓","⚡","🐞","🎓","📎","🎟️","🐢","📓","🕍","🏍️","👋","🥋","❓","🔭","👢","🕷️","😈","🎪","🚨","🌲","⛓️","🆙","🐍","🚲","🐴","🦌","🐔","💎","➕","🐻"];
this.apiUrl = (this.debugMode == false) ? "https://a.y.at" : "https://api-dev.yat.rocks";
// These properties are functions defined further below in this file
this.getSupportedEmojis = getSupportedEmojis
this.isValidYatCharacter = isValidYatCharacter
this.lookupMoneroAddresses = lookupMoneroAddresses
this.testEmojisAgainstUnicodePropertyEscape = testEmojisAgainstUnicodePropertyEscape
this.isValidYatHandle = isValidYatHandle
if (this.remoteLookup == true) {
this.getSupportedEmojis().then((response) => {
this.validEmojis = Object.values(response); // properly clone array using spread operator
})
} else {
this.validEmojis = this.staticEmojiList;
}
this.getBasePath = (() => {
return this.apiUrl;
})
}
function isEmojiCharacter(char) {
return /\p{Emoji}/u.test(char);
}
function isValidYatHandle(handle) {
if (typeof(handle) !== 'string') {
return false;
}
// Remember that an emoji is two-bytes in length
if (handle.length > 10 || handle.length < 1) {
return false;
}
// Iterate through all known valid Yat characters and check that they are members of
// This is commented out until it's possible for us to get a full list of emoji mapping
// for (const c of handle) {
// if (this.isValidYatCharacter(c) == false) {
// return false
// }
// }
return true;
}
function getSupportedEmojis() {
let instance = this;
return new Promise((resolve, reject) => {
if (instance.remoteLookup == true) {
let endpoint = `${instance.apiUrl}/emoji`;
try {
axios.get(endpoint)
.then((response) => {
resolve(response.data);
}).catch((error) => {
// we could resolve with base emoji list here
//self.validEmojis = [...validEmojis]; // properly clone array using spread operator
resolve(validEmojis);
})
} catch (error) {
reject(error)
}
}
resolve(instance.staticEmojiList)
})
}
function isValidYatCharacter(char) {
const self = this;
let response = self.validEmojis.includes(char);
return response;
}
// Returns empty object when successful but no data is set. Otherwise, returns object with key => value pair - eg { "0x1001" => "moneroaddress", "0x1002" => "monerosubaddress" }
// *
function lookupMoneroAddresses(yat) {
const self = this;
// 0x1001 is a Monero address, 0x1002 is a Monero subaddress
let endpointString = `${self.apiUrl}/emoji_id/${yat}/payment`;
let endpoint = encodeURI(endpointString);
return new Promise((resolve, reject) => {
axios.get(endpoint)
.then((response) => {
let returnData = new Map();
if (typeof(response.data.result['0x1001']) !== "undefined") {
returnData.set('0x1001', response.data.result['0x1001'].address);
}
if (typeof(response.data.result['0x1002']) !== "undefined") {
returnData.set('0x1002', response.data.result['0x1002'].address);
}
resolve(returnData);
}).catch(function (error) {
// We could land up here because a Yat does not exist, or a networking error / server error is encountered -- non-existent Yat handles return a 404
// Return the error object to the invocator
reject(error);
});
});
}
// This function checks that all returned emoji characters have appropriate properties to be able to parse handles without error.
// TODO: Refactor into unit test -- if this stops working, it means that the underlying browser / node version does not correctly process regular expressions
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes
// Remember that [0..9], *, #, digits will match true when checking their properties to see if they are
function testEmojisAgainstUnicodePropertyEscape() {
let alerted = 0;
let cnt = 0;
for (let i = 0; i < this.validEmojis.length; i++) {
let match = isEmojiCharacter(this.validEmojis[i]);
if (match !== true) {
alerted++;
}
cnt++;
}
}
module.exports = YatMoneroLookup;