@galacticcouncil/common 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +1 -0
- package/build/index.mjs +1 -0
- package/build/types/consts.d.ts +2 -0
- package/build/types/index.d.ts +3 -0
- package/build/types/utils/big.d.ts +4 -0
- package/build/types/utils/erc20.d.ts +5 -0
- package/build/types/utils/h160.d.ts +9 -0
- package/build/types/utils/index.d.ts +4 -0
- package/build/types/utils/xc.d.ts +1 -0
- package/package.json +51 -0
- package/src/consts.ts +5 -0
- package/src/index.ts +8 -0
- package/src/utils/big.spec.ts +73 -0
- package/src/utils/big.ts +26 -0
- package/src/utils/erc20.ts +29 -0
- package/src/utils/h160.spec.ts +39 -0
- package/src/utils/h160.ts +79 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/xc.spec.ts +8 -0
- package/src/utils/xc.ts +7 -0
package/build/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var v=Object.create;var u=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,U=Object.prototype.hasOwnProperty;var f=(t,r)=>{for(var e in r)u(t,e,{get:r[e],enumerable:!0})},I=(t,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of R(r))!U.call(t,n)&&n!==e&&u(t,n,{get:()=>r[n],enumerable:!(o=F(r,n))||o.enumerable});return t};var D=(t,r,e)=>(e=t!=null?v(w(t)):{},I(r||!t||!t.__esModule?u(e,"default",{value:t,enumerable:!0}):e,t)),T=t=>I(u({},"__esModule",{value:!0}),t);var X={};f(X,{HYDRATION_SS58_PREFIX:()=>a,RUNTIME_DECIMALS:()=>Y,big:()=>m,erc20:()=>l,h160:()=>y,version:()=>N,xc:()=>A});module.exports=T(X);var Y=18,a=63;var m={};f(m,{asBigInt:()=>_,toBigInt:()=>P,toDecimal:()=>H});var i=D(require("big.js"));i.default.NE=-18;function H(t,r,e=6,o){let n=(0,i.default)(t.toString()),d=(0,i.default)(10).pow(r);return n.div(d).round(e,o).toString()}function P(t,r){let e=(0,i.default)(10).pow(r),n=(0,i.default)(t).mul(e).toFixed(0,i.default.roundDown);return BigInt(n)}function _(t){return BigInt(t.round(0,i.default.roundDown).toFixed(0))}var l={};f(l,{ERC20:()=>x});var x=class{static fromAssetId(r){let e=Buffer.alloc(20,0);return e[15]=1,e.writeUInt32BE(r,16),"0x"+e.toString("hex")}static toAssetId(r){let e=Buffer.from(r.replace("0x",""),"hex");return e.length!==20||!this.isAssetAddress(r)?null:e.readUInt32BE(16)}static isAssetAddress(r){let e=Buffer.from("0000000000000000000000000000000100000000","hex"),o=Buffer.from(r.replace("0x",""),"hex");return o.length!==20?!1:o.subarray(0,16).equals(e.subarray(0,16))}};var y={};f(y,{H160:()=>B,isEvmAccount:()=>h,isEvmAddress:()=>b,isSs58Address:()=>E});var c=require("polkadot-api"),p=require("@polkadot-api/utils"),s=require("buffer");var g="ETH\0";function h(t){if(!t)return!1;try{let r=(0,c.AccountId)().enc(t),e=s.Buffer.from(g);return s.Buffer.from(r.subarray(0,e.length)).equals(e)}catch{return!1}}function b(t){return!!/^0x[a-fA-F0-9]{40}$/.test(t)}function E(t){try{return(0,c.AccountId)(63).enc(t),!0}catch{return!1}}var B=class t{static toAccount=r=>{let e=s.Buffer.from(r.slice(2),"hex"),o=s.Buffer.from(g),n=Uint8Array.from(s.Buffer.concat([o,e,s.Buffer.alloc(8)])),d=(0,p.toHex)(n);return(0,c.AccountId)(63).dec(d)};static fromAccount=r=>{let e=(0,c.AccountId)().enc(r),o=s.Buffer.from(g),n=e.slice(o.length,-8);return"0x"+s.Buffer.from(n).toString("hex")};static fromSS58=r=>{let o=(0,c.AccountId)().enc(r).slice(0,20);return(0,p.toHex)(o)};static fromAny=r=>{if(b(r))return r;if(h(r))return t.fromAccount(r);if(E(r))return t.fromSS58(r);throw new Error("Unknown address type")}};var A={};f(A,{convertToId:()=>M});var S=require("buffer");function M(t){let e=S.Buffer.from(t.replace("0x",""),"hex").subarray(16);return e.readUIntBE(0,e.length)}var N="0.1.0";0&&(module.exports={HYDRATION_SS58_PREFIX,RUNTIME_DECIMALS,big,erc20,h160,version,xc});
|
package/build/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var b=Object.defineProperty;var f=(e,r)=>{for(var t in r)b(e,t,{get:r[t],enumerable:!0})};var D=18,d=63;var l={};f(l,{asBigInt:()=>v,toBigInt:()=>S,toDecimal:()=>E});import i from"big.js";i.NE=-18;function E(e,r,t=6,o){let s=i(e.toString()),u=i(10).pow(r);return s.div(u).round(t,o).toString()}function S(e,r){let t=i(10).pow(r),s=i(e).mul(t).toFixed(0,i.roundDown);return BigInt(s)}function v(e){return BigInt(e.round(0,i.roundDown).toFixed(0))}var p={};f(p,{ERC20:()=>a});var a=class{static fromAssetId(r){let t=Buffer.alloc(20,0);return t[15]=1,t.writeUInt32BE(r,16),"0x"+t.toString("hex")}static toAssetId(r){let t=Buffer.from(r.replace("0x",""),"hex");return t.length!==20||!this.isAssetAddress(r)?null:t.readUInt32BE(16)}static isAssetAddress(r){let t=Buffer.from("0000000000000000000000000000000100000000","hex"),o=Buffer.from(r.replace("0x",""),"hex");return o.length!==20?!1:o.subarray(0,16).equals(t.subarray(0,16))}};var I={};f(I,{H160:()=>x,isEvmAccount:()=>B,isEvmAddress:()=>y,isSs58Address:()=>A});import{AccountId as c}from"polkadot-api";import{toHex as g}from"@polkadot-api/utils";import{Buffer as n}from"buffer";var m="ETH\0";function B(e){if(!e)return!1;try{let r=c().enc(e),t=n.from(m);return n.from(r.subarray(0,t.length)).equals(t)}catch{return!1}}function y(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function A(e){try{return c(63).enc(e),!0}catch{return!1}}var x=class e{static toAccount=r=>{let t=n.from(r.slice(2),"hex"),o=n.from(m),s=Uint8Array.from(n.concat([o,t,n.alloc(8)])),u=g(s);return c(63).dec(u)};static fromAccount=r=>{let t=c().enc(r),o=n.from(m),s=t.slice(o.length,-8);return"0x"+n.from(s).toString("hex")};static fromSS58=r=>{let o=c().enc(r).slice(0,20);return g(o)};static fromAny=r=>{if(y(r))return r;if(B(r))return e.fromAccount(r);if(A(r))return e.fromSS58(r);throw new Error("Unknown address type")}};var h={};f(h,{convertToId:()=>R});import{Buffer as F}from"buffer";function R(e){let t=F.from(e.replace("0x",""),"hex").subarray(16);return t.readUIntBE(0,t.length)}var Y="0.1.0";export{d as HYDRATION_SS58_PREFIX,D as RUNTIME_DECIMALS,l as big,p as erc20,I as h160,Y as version,h as xc};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import Big, { RoundingMode } from 'big.js';
|
|
2
|
+
export declare function toDecimal(amount: bigint, decimals: number, maxDecimal?: number, roundType?: RoundingMode): string;
|
|
3
|
+
export declare function toBigInt(amount: string | number, decimals: number): bigint;
|
|
4
|
+
export declare function asBigInt(value: Big): bigint;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function isEvmAccount(ss58addr: string): boolean;
|
|
2
|
+
export declare function isEvmAddress(address: string): boolean;
|
|
3
|
+
export declare function isSs58Address(address: string): boolean;
|
|
4
|
+
export declare class H160 {
|
|
5
|
+
static toAccount: (address: string) => import("polkadot-api").SS58String;
|
|
6
|
+
static fromAccount: (address: string) => string;
|
|
7
|
+
static fromSS58: (address: string) => string;
|
|
8
|
+
static fromAny: (address: string) => string;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function convertToId(xcAddress: string): number;
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@galacticcouncil/common",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Common utilities shared across Galactic SDK packages",
|
|
5
|
+
"author": "GalacticCouncil",
|
|
6
|
+
"repository": {
|
|
7
|
+
"directory": "packages/common",
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/galacticcouncil/sdk.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"hydration",
|
|
13
|
+
"utilities",
|
|
14
|
+
"common"
|
|
15
|
+
],
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/galacticcouncil/sdk/issues"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"build",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"main": "./build/index.cjs",
|
|
24
|
+
"module": "./build/index.mjs",
|
|
25
|
+
"types": "./build/types/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./build/types/index.d.ts",
|
|
29
|
+
"import": "./build/index.mjs",
|
|
30
|
+
"require": "./build/index.cjs",
|
|
31
|
+
"default": "./src/index.ts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "npm run clean && node ./esbuild.dist.mjs",
|
|
36
|
+
"build:watch": "node ./esbuild.dev.mjs",
|
|
37
|
+
"postbuild": "tsc --emitDeclarationOnly --outDir build/types/",
|
|
38
|
+
"clean": "rimraf build",
|
|
39
|
+
"link": "npm ln",
|
|
40
|
+
"test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-vm-modules jest"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"big.js": "^6.2.1"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/big.js": "^6.2.2"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"polkadot-api": "^1.20.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/consts.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { toBigInt, toDecimal } from './big';
|
|
3
|
+
|
|
4
|
+
describe('Big utils', () => {
|
|
5
|
+
describe('toDecimals', () => {
|
|
6
|
+
it('should convert to decimals with 18 decimals', () => {
|
|
7
|
+
expect(toDecimal(1_678_578_001_143_648_100n, 18)).toBe('1.678578');
|
|
8
|
+
expect(toDecimal(4_778_448_942_696_649_000n, 18, 0)).toBe('5');
|
|
9
|
+
expect(toDecimal(9_277_414_529_928_219_000n, 18, 1)).toBe('9.3');
|
|
10
|
+
expect(toDecimal(1_489_926_767_190_488_000n, 18, 2)).toBe('1.49');
|
|
11
|
+
expect(toDecimal(7_111_587_933_365_016_000n, 18, 3)).toBe('7.112');
|
|
12
|
+
expect(toDecimal(7_111_587_933_365_016_000n, 18, 18)).toBe(
|
|
13
|
+
'7.111587933365016'
|
|
14
|
+
);
|
|
15
|
+
expect(toDecimal(4756503344577211177520n, 18, 18)).toBe(
|
|
16
|
+
'4756.50334457721117752'
|
|
17
|
+
);
|
|
18
|
+
expect(toDecimal(198_000_012_317_550_812n, 18, 18)).toBe(
|
|
19
|
+
'0.198000012317550812'
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should convert to decimals with 12 decimals', () => {
|
|
24
|
+
expect(toDecimal(52_912_391_280_480n, 12)).toBe('52.912391');
|
|
25
|
+
expect(toDecimal(796_301_020_21449n, 12, 0)).toBe('80');
|
|
26
|
+
expect(toDecimal(370_024_965_982_774n, 12, 1)).toBe('370');
|
|
27
|
+
expect(toDecimal(776_879_141_948n, 12, 2)).toBe('0.78');
|
|
28
|
+
expect(toDecimal(372_209_875_392n, 12, 3)).toBe('0.372');
|
|
29
|
+
expect(toDecimal(372_209_875_392n, 12, 12)).toBe('0.372209875392');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should convert to decimals with the correct rounding types', () => {
|
|
33
|
+
expect(toDecimal(370_024_965_982_774n, 12, 1, Big.roundDown)).toBe('370');
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('toBigInt', () => {
|
|
38
|
+
it('should convert to BigInt', () => {
|
|
39
|
+
expect(toBigInt('1.678578', 18)).toBe(1_678_578_000_000_000_000n);
|
|
40
|
+
expect(toBigInt('5', 18)).toBe(5_000_000_000_000_000_000n);
|
|
41
|
+
expect(toBigInt('9.3', 18)).toBe(9_300_000_000_000_000_000n);
|
|
42
|
+
expect(toBigInt('1.49', 18)).toBe(1_490_000_000_000_000_000n);
|
|
43
|
+
expect(toBigInt('7.112', 18)).toBe(7_112_000_000_000_000_000n);
|
|
44
|
+
expect(toBigInt('7.111587933365016', 18)).toBe(
|
|
45
|
+
7_111_587_933_365_016_000n
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should convert to BigInt with 12 decimals', () => {
|
|
50
|
+
expect(toBigInt('52.912391', 12)).toBe(52_912_391_000_000n);
|
|
51
|
+
expect(toBigInt('80', 12)).toBe(80_000_000_000_000n);
|
|
52
|
+
expect(toBigInt('370', 12)).toBe(370_000_000_000_000n);
|
|
53
|
+
expect(toBigInt('0.78', 12)).toBe(780_000_000_000n);
|
|
54
|
+
expect(toBigInt('0.372', 12)).toBe(372_000_000_000n);
|
|
55
|
+
expect(toBigInt('0.372209875392', 12)).toBe(372_209_875_392n);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should convert to BigInt from number', () => {
|
|
59
|
+
expect(toBigInt(52.912391, 12)).toBe(52_912_391_000_000n);
|
|
60
|
+
expect(toBigInt(80, 12)).toBe(80_000_000_000_000n);
|
|
61
|
+
expect(toBigInt(370, 12)).toBe(370_000_000_000_000n);
|
|
62
|
+
expect(toBigInt(0.78, 12)).toBe(780_000_000_000n);
|
|
63
|
+
expect(toBigInt(0.372, 12)).toBe(372_000_000_000n);
|
|
64
|
+
expect(toBigInt(0.372209875392, 12)).toBe(372_209_875_392n);
|
|
65
|
+
expect(toBigInt('4756.50334457721117752', 18)).toBe(
|
|
66
|
+
4_756_503_344_577_211_177_520n
|
|
67
|
+
);
|
|
68
|
+
expect(toBigInt('66712.0367386073069948646', 18)).toBe(
|
|
69
|
+
66_712_036_738_607_306_994_864n
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
package/src/utils/big.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Big, { RoundingMode } from 'big.js';
|
|
2
|
+
|
|
3
|
+
Big.NE = -18;
|
|
4
|
+
|
|
5
|
+
export function toDecimal(
|
|
6
|
+
amount: bigint,
|
|
7
|
+
decimals: number,
|
|
8
|
+
maxDecimal = 6,
|
|
9
|
+
roundType?: RoundingMode
|
|
10
|
+
): string {
|
|
11
|
+
const dividend = Big(amount.toString());
|
|
12
|
+
const divisor = Big(10).pow(decimals);
|
|
13
|
+
const result = dividend.div(divisor).round(maxDecimal, roundType);
|
|
14
|
+
return result.toString();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function toBigInt(amount: string | number, decimals: number): bigint {
|
|
18
|
+
const multiplier = Big(10).pow(decimals);
|
|
19
|
+
const result = Big(amount).mul(multiplier);
|
|
20
|
+
const fixedPoint = result.toFixed(0, Big.roundDown);
|
|
21
|
+
return BigInt(fixedPoint);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function asBigInt(value: Big): bigint {
|
|
25
|
+
return BigInt(value.round(0, Big.roundDown).toFixed(0));
|
|
26
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class ERC20 {
|
|
2
|
+
static fromAssetId(assetId: number) {
|
|
3
|
+
const evmAddressBuffer = Buffer.alloc(20, 0);
|
|
4
|
+
evmAddressBuffer[15] = 1;
|
|
5
|
+
evmAddressBuffer.writeUInt32BE(assetId, 16);
|
|
6
|
+
|
|
7
|
+
return '0x' + evmAddressBuffer.toString('hex');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static toAssetId(address: string) {
|
|
11
|
+
const addressBuffer = Buffer.from(address.replace('0x', ''), 'hex');
|
|
12
|
+
if (addressBuffer.length !== 20 || !this.isAssetAddress(address)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return addressBuffer.readUInt32BE(16);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static isAssetAddress(address: string) {
|
|
19
|
+
const PREFIX_BUFFER = Buffer.from(
|
|
20
|
+
'0000000000000000000000000000000100000000',
|
|
21
|
+
'hex'
|
|
22
|
+
);
|
|
23
|
+
const addressBuffer = Buffer.from(address.replace('0x', ''), 'hex');
|
|
24
|
+
if (addressBuffer.length !== 20) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return addressBuffer.subarray(0, 16).equals(PREFIX_BUFFER.subarray(0, 16));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { H160, isEvmAddress } from './h160';
|
|
2
|
+
|
|
3
|
+
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
|
4
|
+
const ALICE_H160 = '0xd43593c715fdd31c61141abd04a99fd6822c8558';
|
|
5
|
+
|
|
6
|
+
const ALITH_H160 = '0x15fdd31c61141abd04a99fd6822c8558854ccde3';
|
|
7
|
+
const ALITH_SS58 = '7KATdGaiYbPauPtFrTfRj7zMM8KHqLbkjfurUnX6F57aHHqq';
|
|
8
|
+
|
|
9
|
+
describe('H160 Utils', () => {
|
|
10
|
+
it('Convert Alice (Substrate) SS85 => H160', () => {
|
|
11
|
+
const result = H160.fromSS58(ALICE);
|
|
12
|
+
expect(result).toStrictEqual(ALICE_H160);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('Convert Alith (Evm) H160 => SS58', () => {
|
|
16
|
+
const result = H160.toAccount(ALITH_H160);
|
|
17
|
+
expect(result).toStrictEqual(ALITH_SS58);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('Convert Alith (Evm) SS58 => H160', () => {
|
|
21
|
+
const result = H160.fromAccount(ALITH_SS58);
|
|
22
|
+
expect(result).toStrictEqual(ALITH_H160);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('is ALICE_H160 valid h160', () => {
|
|
26
|
+
const result = isEvmAddress(ALICE_H160);
|
|
27
|
+
expect(result).toStrictEqual(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('is ALITH_H160 valid h160', () => {
|
|
31
|
+
const result = isEvmAddress(ALITH_H160);
|
|
32
|
+
expect(result).toStrictEqual(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('is ALITH_SS58 not valid h160', () => {
|
|
36
|
+
const result = isEvmAddress(ALITH_SS58);
|
|
37
|
+
expect(result).toStrictEqual(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { AccountId } from 'polkadot-api';
|
|
2
|
+
import { toHex } from '@polkadot-api/utils';
|
|
3
|
+
|
|
4
|
+
import { Buffer } from 'buffer';
|
|
5
|
+
|
|
6
|
+
import { HYDRATION_SS58_PREFIX } from '../consts';
|
|
7
|
+
|
|
8
|
+
const ETH_PREFIX = 'ETH\0';
|
|
9
|
+
|
|
10
|
+
export function isEvmAccount(ss58addr: string) {
|
|
11
|
+
if (!ss58addr) return false;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const addressBytes = AccountId().enc(ss58addr);
|
|
15
|
+
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
16
|
+
return Buffer.from(addressBytes.subarray(0, prefixBytes.length)).equals(
|
|
17
|
+
prefixBytes
|
|
18
|
+
);
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isEvmAddress(address: string): boolean {
|
|
25
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isSs58Address(address: string): boolean {
|
|
32
|
+
try {
|
|
33
|
+
AccountId(HYDRATION_SS58_PREFIX).enc(address);
|
|
34
|
+
return true;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class H160 {
|
|
41
|
+
static toAccount = (address: string) => {
|
|
42
|
+
const addressBytes = Buffer.from(address.slice(2), 'hex');
|
|
43
|
+
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
44
|
+
const convertBytes = Uint8Array.from(
|
|
45
|
+
Buffer.concat([prefixBytes, addressBytes, Buffer.alloc(8)])
|
|
46
|
+
);
|
|
47
|
+
const addressHex = toHex(convertBytes);
|
|
48
|
+
return AccountId(HYDRATION_SS58_PREFIX).dec(addressHex);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
static fromAccount = (address: string) => {
|
|
52
|
+
const decodedBytes = AccountId().enc(address);
|
|
53
|
+
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
54
|
+
const addressBytes = decodedBytes.slice(prefixBytes.length, -8);
|
|
55
|
+
return '0x' + Buffer.from(addressBytes).toString('hex');
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
static fromSS58 = (address: string) => {
|
|
59
|
+
const decodedBytes = AccountId().enc(address);
|
|
60
|
+
const slicedBytes = decodedBytes.slice(0, 20);
|
|
61
|
+
return toHex(slicedBytes);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
static fromAny = (address: string) => {
|
|
65
|
+
if (isEvmAddress(address)) {
|
|
66
|
+
return address;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isEvmAccount(address)) {
|
|
70
|
+
return H160.fromAccount(address);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (isSs58Address(address)) {
|
|
74
|
+
return H160.fromSS58(address);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
throw new Error('Unknown address type');
|
|
78
|
+
};
|
|
79
|
+
}
|
package/src/utils/xc.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
|
|
3
|
+
export function convertToId(xcAddress: string): number {
|
|
4
|
+
const addressBuffer = Buffer.from(xcAddress.replace('0x', ''), 'hex');
|
|
5
|
+
const assetIdBuffer = addressBuffer.subarray(16);
|
|
6
|
+
return assetIdBuffer.readUIntBE(0, assetIdBuffer.length);
|
|
7
|
+
}
|