|
|
|
@ -54,4 +54,73 @@ export function qr_options(url: string, imageSize: number = 0.4, imageMargin: nu
|
|
|
|
|
export function parse_zone(domain: string) { |
|
|
|
|
// extract the zone from the domain (e.g. example.ton from 123.example.ton)
|
|
|
|
|
return domain.split('.').slice(1).join('.'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// look up tables
|
|
|
|
|
const to_hex_array = []; |
|
|
|
|
// map from string to a number
|
|
|
|
|
const to_byte_map: any = {}; |
|
|
|
|
for (let ord = 0; ord <= 0xff; ord++) { |
|
|
|
|
let s = ord.toString(16); |
|
|
|
|
if (s.length < 2) { |
|
|
|
|
s = "0" + s; |
|
|
|
|
} |
|
|
|
|
to_hex_array.push(s); |
|
|
|
|
to_byte_map[s] = ord; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function hexToBytes(s: string) { |
|
|
|
|
s = s.toLowerCase(); |
|
|
|
|
const length2 = s.length; |
|
|
|
|
if (length2 % 2 !== 0) { |
|
|
|
|
throw "hex string must have length a multiple of 2"; |
|
|
|
|
} |
|
|
|
|
const length = length2 / 2; |
|
|
|
|
const result = new Uint8Array(length); |
|
|
|
|
for (let i = 0; i < length; i++) { |
|
|
|
|
const i2 = i * 2; |
|
|
|
|
const b = s.substring(i2, i2 + 2); |
|
|
|
|
if (!to_byte_map.hasOwnProperty(b)) throw new Error('invalid hex character ' + b); |
|
|
|
|
result[i] = to_byte_map[b]; |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function crc16(data: any) { |
|
|
|
|
const poly = 0x1021; |
|
|
|
|
let reg = 0; |
|
|
|
|
const message = new Uint8Array(data.length + 2); |
|
|
|
|
message.set(data); |
|
|
|
|
for (let byte of message) { |
|
|
|
|
let mask = 0x80; |
|
|
|
|
while (mask > 0) { |
|
|
|
|
reg <<= 1; |
|
|
|
|
if (byte & mask) { |
|
|
|
|
reg += 1; |
|
|
|
|
} |
|
|
|
|
mask >>= 1 |
|
|
|
|
if (reg > 0xffff) { |
|
|
|
|
reg &= 0xffff; |
|
|
|
|
reg ^= poly; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return new Uint8Array([Math.floor(reg / 256), reg % 256]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function convertAddress(address: string) { |
|
|
|
|
const addrSrc = hexToBytes(address.split(':')[1]); |
|
|
|
|
const addr = new Int8Array(34); |
|
|
|
|
addr[0] = 0x11; // tag (bounceable)
|
|
|
|
|
addr[1] = 0; // workchain
|
|
|
|
|
addr.set(addrSrc, 2); |
|
|
|
|
|
|
|
|
|
const addressWithChecksum = new Uint8Array(36); |
|
|
|
|
addressWithChecksum.set(addr); |
|
|
|
|
addressWithChecksum.set(crc16(addr), 34); |
|
|
|
|
// @ts-ignore
|
|
|
|
|
let addressBase64 = btoa(String.fromCharCode.apply(null, new Uint8Array(addressWithChecksum))); |
|
|
|
|
addressBase64 = addressBase64.replace(/\+/g, '-').replace(/\//g, '_'); |
|
|
|
|
return addressBase64; |
|
|
|
|
} |