Browse Source

Converting the address

vue
Lev 2 years ago
parent
commit
ff77f8fe2a
  1. 9
      .idea/workspace.xml
  2. 5
      src/components/Header.vue
  3. 69
      src/utils.ts

9
.idea/workspace.xml

@ -3,11 +3,8 @@
<component name="ChangeListManager">
<list default="true" id="ddb8afd5-d3ba-47b1-b6d0-227403f1abf7" name="Changes" comment="Checkout">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/api.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/api.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/DomainResult.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/DomainResult.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/main.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/result.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/result.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/views/Checkout.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/views/Checkout.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/Header.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/Header.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/utils.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils.ts" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -93,6 +90,8 @@
<workItem from="1673123515646" duration="82000" />
<workItem from="1673195751389" duration="2458000" />
<workItem from="1673683299593" duration="12019000" />
<workItem from="1673906442760" duration="1896000" />
<workItem from="1674161982371" duration="2229000" />
</task>
<task id="LOCAL-00001" summary="Wrote the landing">
<created>1670844191163</created>

5
src/components/Header.vue

@ -26,6 +26,8 @@
</template>
<script>
import {convertAddress} from "../utils.ts";
export default {
name: "Header",
props: {
@ -43,8 +45,7 @@ export default {
if (address_raw === undefined) {
return '';
}
// let address = convert_address(address_raw);
let address = address_raw;
let address = convertAddress(address_raw);
return address.slice(0, 5) + '...' + address.slice(-4);
}
}

69
src/utils.ts

@ -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;
}
Loading…
Cancel
Save