@shapeshiftoss/caip 4.0.0 → 5.0.1
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/README.md +43 -34
- package/dist/accountId/accountId.d.ts +16 -5
- package/dist/accountId/accountId.js +15 -14
- package/dist/adapters/banxa/index.js +3 -5
- package/dist/adapters/coincap/utils.d.ts +10 -10
- package/dist/adapters/coincap/utils.js +11 -57
- package/dist/adapters/coingecko/utils.d.ts +10 -10
- package/dist/adapters/coingecko/utils.js +11 -57
- package/dist/adapters/osmosis/utils.js +7 -7
- package/dist/adapters/yearn/utils.js +9 -5
- package/dist/assetId/assetId.d.ts +19 -16
- package/dist/assetId/assetId.js +65 -66
- package/dist/chainId/chainId.d.ts +7 -28
- package/dist/chainId/chainId.js +11 -222
- package/dist/constants.d.ts +42 -0
- package/dist/constants.js +64 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/typeGuards.d.ts +14 -0
- package/dist/typeGuards.js +48 -0
- package/dist/utils.d.ts +14 -9
- package/dist/utils.js +57 -18
- package/package.json +1 -1
package/dist/assetId/assetId.js
CHANGED
|
@@ -1,31 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromCAIP19 = exports.toCAIP19 = exports.fromAssetId = exports.toAssetId =
|
|
6
|
+
exports.fromCAIP19 = exports.toCAIP19 = exports.fromAssetId = exports.toAssetId = void 0;
|
|
4
7
|
// https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-19.md
|
|
5
|
-
const
|
|
8
|
+
const toLower_1 = __importDefault(require("lodash/toLower"));
|
|
6
9
|
const chainId_1 = require("../chainId/chainId");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
'erc20',
|
|
11
|
-
'erc721',
|
|
12
|
-
'slip44',
|
|
13
|
-
'native',
|
|
14
|
-
'ibc'
|
|
15
|
-
];
|
|
16
|
-
exports.ASSET_REFERENCE = {
|
|
17
|
-
Bitcoin: '0',
|
|
18
|
-
Ethereum: '60',
|
|
19
|
-
Cosmos: '118',
|
|
20
|
-
Osmosis: '118'
|
|
21
|
-
};
|
|
22
|
-
const validAssetNamespaces = Object.freeze({
|
|
23
|
-
[types_1.ChainTypes.Bitcoin]: ['slip44'],
|
|
24
|
-
[types_1.ChainTypes.Ethereum]: ['slip44', 'erc20', 'erc721'],
|
|
25
|
-
[types_1.ChainTypes.Cosmos]: ['cw20', 'cw721', 'ibc', 'native', 'slip44'],
|
|
26
|
-
[types_1.ChainTypes.Osmosis]: ['cw20', 'cw721', 'ibc', 'native', 'slip44']
|
|
27
|
-
});
|
|
28
|
-
const isAssetNamespace = (maybeAssetNamespace) => assetNamespaceStrings.some((s) => s === maybeAssetNamespace);
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
|
+
const typeGuards_1 = require("../typeGuards");
|
|
12
|
+
const utils_1 = require("../utils");
|
|
29
13
|
/**
|
|
30
14
|
* validate that a value is a string slip44 value
|
|
31
15
|
* @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
@@ -36,62 +20,77 @@ const isValidSlip44 = (value) => {
|
|
|
36
20
|
// slip44 has a max value of an unsigned 32-bit integer
|
|
37
21
|
return !isNaN(n) && n >= 0 && n < 4294967296;
|
|
38
22
|
};
|
|
23
|
+
const isToAssetIdWithChainIdArgs = (args) => !!args.chainId;
|
|
39
24
|
const toAssetId = (args) => {
|
|
40
|
-
const {
|
|
41
|
-
|
|
42
|
-
if (!chain)
|
|
43
|
-
throw new Error('toAssetId: No chain provided');
|
|
44
|
-
if (!network)
|
|
45
|
-
throw new Error('toAssetId: No chainReference Provided');
|
|
46
|
-
if (!assetNamespace)
|
|
47
|
-
throw new Error('toAssetId: No assetNamespace provided');
|
|
25
|
+
const { assetNamespace, assetReference } = args;
|
|
26
|
+
(0, typeGuards_1.assertIsAssetNamespace)(assetNamespace);
|
|
48
27
|
if (!assetReference)
|
|
49
28
|
throw new Error('toAssetId: No assetReference provided');
|
|
50
|
-
const chainId = (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
29
|
+
const { chainId, chainNamespace, chainReference } = (() => {
|
|
30
|
+
if (isToAssetIdWithChainIdArgs(args)) {
|
|
31
|
+
const fromChainIdResult = (0, chainId_1.fromChainId)(args.chainId);
|
|
32
|
+
return {
|
|
33
|
+
chainId: args.chainId,
|
|
34
|
+
chainNamespace: fromChainIdResult.chainNamespace,
|
|
35
|
+
chainReference: fromChainIdResult.chainReference
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
else
|
|
39
|
+
return {
|
|
40
|
+
chainId: (0, chainId_1.toChainId)({
|
|
41
|
+
chainNamespace: args.chainNamespace,
|
|
42
|
+
chainReference: args.chainReference
|
|
43
|
+
}),
|
|
44
|
+
chainNamespace: args.chainNamespace,
|
|
45
|
+
chainReference: args.chainReference
|
|
46
|
+
};
|
|
47
|
+
})();
|
|
48
|
+
const isContractAddress = Array('erc20', 'erc721').includes(assetNamespace);
|
|
49
|
+
(0, typeGuards_1.assertIsChainNamespace)(chainNamespace);
|
|
50
|
+
(0, typeGuards_1.assertIsChainReference)(chainReference);
|
|
51
|
+
(0, typeGuards_1.assertValidChainPartsPair)(chainNamespace, chainReference);
|
|
52
|
+
if (!constants_1.VALID_ASSET_NAMESPACE[chainNamespace].includes(assetNamespace) ||
|
|
53
|
+
!(0, typeGuards_1.isAssetNamespace)(assetNamespace))
|
|
54
|
+
throw new Error(`toAssetId: AssetNamespace ${assetNamespace} not supported for Chain Namespace ${chainNamespace}`);
|
|
54
55
|
if (assetNamespace === 'slip44' && !isValidSlip44(String(assetReference))) {
|
|
55
56
|
throw new Error(`Invalid reference for namespace slip44`);
|
|
56
57
|
}
|
|
57
|
-
if (
|
|
58
|
-
if (!assetReference.startsWith('0x'))
|
|
58
|
+
if (isContractAddress) {
|
|
59
|
+
if (!assetReference.startsWith('0x'))
|
|
59
60
|
throw new Error(`toAssetId: assetReference must start with 0x: ${assetReference}`);
|
|
60
|
-
|
|
61
|
-
if (assetReference.length !== 42) {
|
|
61
|
+
if (assetReference.length !== 42)
|
|
62
62
|
throw new Error(`toAssetId: assetReference length must be 42, length: ${assetReference.length}`);
|
|
63
|
-
}
|
|
64
|
-
// We make Eth contract addresses lower case to simplify comparisons
|
|
65
|
-
assetReference = assetReference.toLowerCase();
|
|
66
63
|
}
|
|
67
|
-
|
|
64
|
+
// We make Eth contract addresses lower case to simplify comparisons
|
|
65
|
+
const assetReferenceCaseCorrected = isContractAddress
|
|
66
|
+
? assetReference.toLowerCase()
|
|
67
|
+
: assetReference;
|
|
68
|
+
return `${chainId}/${assetNamespace}:${assetReferenceCaseCorrected}`;
|
|
68
69
|
};
|
|
69
70
|
exports.toAssetId = toAssetId;
|
|
70
|
-
const parseAssetIdRegExp = /([-a-z\d]{3,8}):([-a-zA-Z\d]{1,32})\/([-a-z\d]{3,8}):([-a-zA-Z\d]+)/;
|
|
71
71
|
const fromAssetId = (assetId) => {
|
|
72
72
|
var _a;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
73
|
+
if (!(0, typeGuards_1.isAssetId)(assetId))
|
|
74
|
+
throw new Error(`fromAssetId: invalid AssetId: ${assetId}`);
|
|
75
|
+
const matches = (_a = utils_1.parseAssetIdRegExp.exec(assetId)) !== null && _a !== void 0 ? _a : [];
|
|
76
|
+
// These should never throw because isAssetId() would have already caught it, but they help with type inference
|
|
77
|
+
(0, typeGuards_1.assertIsChainNamespace)(matches[1]);
|
|
78
|
+
(0, typeGuards_1.assertIsChainReference)(matches[2]);
|
|
79
|
+
(0, typeGuards_1.assertIsAssetNamespace)(matches[3]);
|
|
80
|
+
const chainNamespace = matches[1];
|
|
81
|
+
const chainReference = matches[2];
|
|
82
|
+
const assetNamespace = matches[3];
|
|
83
|
+
const shouldLowercaseAssetReference = assetNamespace && ['erc20', 'erc721'].includes(assetNamespace);
|
|
84
|
+
const assetReference = shouldLowercaseAssetReference ? (0, toLower_1.default)(matches[4]) : matches[4];
|
|
85
|
+
(0, typeGuards_1.assertIsChainReference)(chainReference);
|
|
86
|
+
(0, typeGuards_1.assertIsChainNamespace)(chainNamespace);
|
|
87
|
+
const chainId = (0, chainId_1.toChainId)({ chainNamespace, chainReference });
|
|
88
|
+
if (assetNamespace && assetReference && chainId) {
|
|
89
|
+
return { chainId, chainReference, chainNamespace, assetNamespace, assetReference };
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
throw new Error(`fromAssetId: invalid AssetId: ${assetId}`);
|
|
93
93
|
}
|
|
94
|
-
throw new Error(`fromAssetId: invalid AssetId: ${assetId}`);
|
|
95
94
|
};
|
|
96
95
|
exports.fromAssetId = fromAssetId;
|
|
97
96
|
exports.toCAIP19 = exports.toAssetId;
|
|
@@ -1,39 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CHAIN_NAMESPACE, CHAIN_REFERENCE } from '../constants';
|
|
2
2
|
export declare type ChainId = string;
|
|
3
|
-
export declare const CHAIN_NAMESPACE: {
|
|
4
|
-
readonly Ethereum: "eip155";
|
|
5
|
-
readonly Bitcoin: "bip122";
|
|
6
|
-
readonly Cosmos: "cosmos";
|
|
7
|
-
};
|
|
8
3
|
export declare type ChainNamespace = typeof CHAIN_NAMESPACE[keyof typeof CHAIN_NAMESPACE];
|
|
9
|
-
export declare const CHAIN_REFERENCE: {
|
|
10
|
-
readonly EthereumMainnet: "1";
|
|
11
|
-
readonly EthereumRopsten: "3";
|
|
12
|
-
readonly EthereumRinkeby: "4";
|
|
13
|
-
readonly BitcoinMainnet: "000000000019d6689c085ae165831e93";
|
|
14
|
-
readonly BitcoinTestnet: "000000000933ea01ad0ee984209779ba";
|
|
15
|
-
readonly CosmosHubMainnet: "cosmoshub-4";
|
|
16
|
-
readonly CosmosHubVega: "vega-testnet";
|
|
17
|
-
readonly OsmosisMainnet: "osmosis-1";
|
|
18
|
-
readonly OsmosisTestnet: "osmo-testnet-1";
|
|
19
|
-
};
|
|
20
4
|
export declare type ChainReference = typeof CHAIN_REFERENCE[keyof typeof CHAIN_REFERENCE];
|
|
21
5
|
declare type ToChainIdArgs = {
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
chainNamespace: ChainNamespace;
|
|
7
|
+
chainReference: ChainReference;
|
|
24
8
|
};
|
|
25
|
-
export declare const toChainId: (args: ToChainIdArgs) =>
|
|
9
|
+
export declare const toChainId: (args: ToChainIdArgs) => ChainId;
|
|
26
10
|
declare type FromChainIdReturn = {
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
chainNamespace: ChainNamespace;
|
|
12
|
+
chainReference: ChainReference;
|
|
29
13
|
};
|
|
30
14
|
declare type FromChainId = (chainId: string) => FromChainIdReturn;
|
|
31
15
|
export declare const fromChainId: FromChainId;
|
|
32
|
-
declare
|
|
33
|
-
export declare const isChainId: IsChainId;
|
|
34
|
-
export declare const chainReferenceToNetworkType: Record<ChainReference, NetworkTypes>;
|
|
35
|
-
export declare const networkTypeToChainReference: Record<NetworkTypes, ChainReference>;
|
|
36
|
-
export declare const chainNamespaceToChainType: Record<ChainNamespace, ChainTypes>;
|
|
37
|
-
export declare const toCAIP2: (args: ToChainIdArgs) => string;
|
|
16
|
+
export declare const toCAIP2: (args: ToChainIdArgs) => ChainId;
|
|
38
17
|
export declare const fromCAIP2: FromChainId;
|
|
39
18
|
export {};
|
package/dist/chainId/chainId.js
CHANGED
|
@@ -1,233 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.fromCAIP2 = exports.toCAIP2 = exports.
|
|
8
|
-
const
|
|
9
|
-
const invert_1 = __importDefault(require("lodash/invert"));
|
|
10
|
-
exports.CHAIN_NAMESPACE = {
|
|
11
|
-
Ethereum: 'eip155',
|
|
12
|
-
Bitcoin: 'bip122',
|
|
13
|
-
Cosmos: 'cosmos'
|
|
14
|
-
};
|
|
15
|
-
exports.CHAIN_REFERENCE = {
|
|
16
|
-
EthereumMainnet: '1',
|
|
17
|
-
EthereumRopsten: '3',
|
|
18
|
-
EthereumRinkeby: '4',
|
|
19
|
-
// EthereumKovan: '42', // currently unsupported by ShapeShift
|
|
20
|
-
// https://github.com/bitcoin/bips/blob/master/bip-0122.mediawiki#definition-of-chain-id
|
|
21
|
-
// chainId uses max length of 32 chars of the genesis block
|
|
22
|
-
BitcoinMainnet: '000000000019d6689c085ae165831e93',
|
|
23
|
-
BitcoinTestnet: '000000000933ea01ad0ee984209779ba',
|
|
24
|
-
CosmosHubMainnet: 'cosmoshub-4',
|
|
25
|
-
CosmosHubVega: 'vega-testnet',
|
|
26
|
-
OsmosisMainnet: 'osmosis-1',
|
|
27
|
-
OsmosisTestnet: 'osmo-testnet-1'
|
|
28
|
-
};
|
|
29
|
-
const shapeShiftToChainId = Object.freeze({
|
|
30
|
-
[types_1.ChainTypes.Ethereum]: {
|
|
31
|
-
namespace: exports.CHAIN_NAMESPACE.Ethereum,
|
|
32
|
-
reference: {
|
|
33
|
-
[types_1.NetworkTypes.MAINNET]: exports.CHAIN_REFERENCE.EthereumMainnet,
|
|
34
|
-
[types_1.NetworkTypes.ETH_ROPSTEN]: exports.CHAIN_REFERENCE.EthereumRopsten,
|
|
35
|
-
[types_1.NetworkTypes.ETH_RINKEBY]: exports.CHAIN_REFERENCE.EthereumRinkeby
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
[types_1.ChainTypes.Bitcoin]: {
|
|
39
|
-
namespace: exports.CHAIN_NAMESPACE.Bitcoin,
|
|
40
|
-
reference: {
|
|
41
|
-
[types_1.NetworkTypes.MAINNET]: exports.CHAIN_REFERENCE.BitcoinMainnet,
|
|
42
|
-
[types_1.NetworkTypes.TESTNET]: exports.CHAIN_REFERENCE.BitcoinTestnet
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
[types_1.ChainTypes.Cosmos]: {
|
|
46
|
-
namespace: exports.CHAIN_NAMESPACE.Cosmos,
|
|
47
|
-
reference: {
|
|
48
|
-
[types_1.NetworkTypes.COSMOSHUB_MAINNET]: exports.CHAIN_REFERENCE.CosmosHubMainnet,
|
|
49
|
-
[types_1.NetworkTypes.COSMOSHUB_VEGA]: exports.CHAIN_REFERENCE.CosmosHubVega
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
[types_1.ChainTypes.Osmosis]: {
|
|
53
|
-
namespace: exports.CHAIN_NAMESPACE.Cosmos,
|
|
54
|
-
reference: {
|
|
55
|
-
[types_1.NetworkTypes.OSMOSIS_MAINNET]: exports.CHAIN_REFERENCE.OsmosisMainnet,
|
|
56
|
-
[types_1.NetworkTypes.OSMOSIS_TESTNET]: exports.CHAIN_REFERENCE.OsmosisTestnet
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
4
|
+
exports.fromCAIP2 = exports.toCAIP2 = exports.fromChainId = exports.toChainId = void 0;
|
|
5
|
+
const typeGuards_1 = require("../typeGuards");
|
|
60
6
|
const toChainId = (args) => {
|
|
61
|
-
const {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const referenceMap = shapeShiftToChainId[chain].reference;
|
|
66
|
-
switch (network) {
|
|
67
|
-
case types_1.NetworkTypes.MAINNET:
|
|
68
|
-
case types_1.NetworkTypes.ETH_ROPSTEN:
|
|
69
|
-
case types_1.NetworkTypes.ETH_RINKEBY: {
|
|
70
|
-
const reference = referenceMap[network];
|
|
71
|
-
return `${namespace}:${reference}`;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
case types_1.ChainTypes.Bitcoin: {
|
|
77
|
-
const referenceMap = shapeShiftToChainId[chain].reference;
|
|
78
|
-
switch (network) {
|
|
79
|
-
case types_1.NetworkTypes.MAINNET:
|
|
80
|
-
case types_1.NetworkTypes.TESTNET: {
|
|
81
|
-
const reference = referenceMap[network];
|
|
82
|
-
return `${namespace}:${reference}`;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case types_1.ChainTypes.Cosmos: {
|
|
88
|
-
const referenceMap = shapeShiftToChainId[chain].reference;
|
|
89
|
-
switch (network) {
|
|
90
|
-
case types_1.NetworkTypes.COSMOSHUB_MAINNET:
|
|
91
|
-
case types_1.NetworkTypes.COSMOSHUB_VEGA: {
|
|
92
|
-
const reference = referenceMap[network];
|
|
93
|
-
return `${namespace}:${reference}`;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
case types_1.ChainTypes.Osmosis: {
|
|
99
|
-
const referenceMap = shapeShiftToChainId[chain].reference;
|
|
100
|
-
switch (network) {
|
|
101
|
-
case types_1.NetworkTypes.OSMOSIS_MAINNET:
|
|
102
|
-
case types_1.NetworkTypes.OSMOSIS_TESTNET: {
|
|
103
|
-
const reference = referenceMap[network];
|
|
104
|
-
return `${namespace}:${reference}`;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
throw new Error(`toChainId: unsupported ${chain} network: ${network}`);
|
|
7
|
+
const { chainNamespace, chainReference } = args;
|
|
8
|
+
const maybeChainId = `${chainNamespace}:${chainReference}`;
|
|
9
|
+
(0, typeGuards_1.assertIsChainId)(maybeChainId);
|
|
10
|
+
return maybeChainId;
|
|
111
11
|
};
|
|
112
12
|
exports.toChainId = toChainId;
|
|
113
13
|
const fromChainId = (chainId) => {
|
|
114
|
-
const [
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
case exports.CHAIN_NAMESPACE.Cosmos: {
|
|
120
|
-
switch (n) {
|
|
121
|
-
case exports.CHAIN_REFERENCE.CosmosHubMainnet: {
|
|
122
|
-
return { chain: types_1.ChainTypes.Cosmos, network: types_1.NetworkTypes.COSMOSHUB_MAINNET };
|
|
123
|
-
}
|
|
124
|
-
case exports.CHAIN_REFERENCE.CosmosHubVega: {
|
|
125
|
-
return { chain: types_1.ChainTypes.Cosmos, network: types_1.NetworkTypes.COSMOSHUB_VEGA };
|
|
126
|
-
}
|
|
127
|
-
case exports.CHAIN_REFERENCE.OsmosisMainnet: {
|
|
128
|
-
return { chain: types_1.ChainTypes.Osmosis, network: types_1.NetworkTypes.OSMOSIS_MAINNET };
|
|
129
|
-
}
|
|
130
|
-
case exports.CHAIN_REFERENCE.OsmosisTestnet: {
|
|
131
|
-
return { chain: types_1.ChainTypes.Osmosis, network: types_1.NetworkTypes.OSMOSIS_TESTNET };
|
|
132
|
-
}
|
|
133
|
-
default: {
|
|
134
|
-
throw new Error(`fromChainId: unsupported ${c} network: ${n}`);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
case exports.CHAIN_NAMESPACE.Ethereum: {
|
|
139
|
-
const chain = types_1.ChainTypes.Ethereum;
|
|
140
|
-
switch (n) {
|
|
141
|
-
case exports.CHAIN_REFERENCE.EthereumMainnet: {
|
|
142
|
-
const network = types_1.NetworkTypes.MAINNET;
|
|
143
|
-
return { chain, network };
|
|
144
|
-
}
|
|
145
|
-
case exports.CHAIN_REFERENCE.EthereumRopsten: {
|
|
146
|
-
const network = types_1.NetworkTypes.ETH_ROPSTEN;
|
|
147
|
-
return { chain, network };
|
|
148
|
-
}
|
|
149
|
-
case exports.CHAIN_REFERENCE.EthereumRinkeby: {
|
|
150
|
-
const network = types_1.NetworkTypes.ETH_RINKEBY;
|
|
151
|
-
return { chain, network };
|
|
152
|
-
}
|
|
153
|
-
default: {
|
|
154
|
-
throw new Error(`fromChainId: unsupported ${c} network: ${n}`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
case exports.CHAIN_NAMESPACE.Bitcoin: {
|
|
159
|
-
const chain = types_1.ChainTypes.Bitcoin;
|
|
160
|
-
switch (n) {
|
|
161
|
-
case exports.CHAIN_REFERENCE.BitcoinMainnet: {
|
|
162
|
-
const network = types_1.NetworkTypes.MAINNET;
|
|
163
|
-
return { chain, network };
|
|
164
|
-
}
|
|
165
|
-
case exports.CHAIN_REFERENCE.BitcoinTestnet: {
|
|
166
|
-
const network = types_1.NetworkTypes.TESTNET;
|
|
167
|
-
return { chain, network };
|
|
168
|
-
}
|
|
169
|
-
default: {
|
|
170
|
-
throw new Error(`fromChainId: unsupported ${c} network: ${n}`);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
throw new Error(`fromChainId: unsupported chain: ${c}`);
|
|
14
|
+
const [chainNamespace, chainReference] = chainId.split(':');
|
|
15
|
+
(0, typeGuards_1.assertIsChainNamespace)(chainNamespace);
|
|
16
|
+
(0, typeGuards_1.assertIsChainReference)(chainReference);
|
|
17
|
+
(0, typeGuards_1.assertValidChainPartsPair)(chainNamespace, chainReference);
|
|
18
|
+
return { chainNamespace, chainReference };
|
|
176
19
|
};
|
|
177
20
|
exports.fromChainId = fromChainId;
|
|
178
|
-
const isChainId = (chainId) => {
|
|
179
|
-
const [c, n] = chainId.split(':');
|
|
180
|
-
if (!(c && n)) {
|
|
181
|
-
throw new Error(`isChainId: error parsing chainId, chain: ${c}, network: ${n}`);
|
|
182
|
-
}
|
|
183
|
-
switch (c) {
|
|
184
|
-
case exports.CHAIN_NAMESPACE.Cosmos: {
|
|
185
|
-
switch (n) {
|
|
186
|
-
case exports.CHAIN_REFERENCE.CosmosHubMainnet:
|
|
187
|
-
case exports.CHAIN_REFERENCE.CosmosHubVega:
|
|
188
|
-
case exports.CHAIN_REFERENCE.OsmosisMainnet:
|
|
189
|
-
case exports.CHAIN_REFERENCE.OsmosisTestnet:
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
case exports.CHAIN_NAMESPACE.Ethereum: {
|
|
195
|
-
switch (n) {
|
|
196
|
-
case exports.CHAIN_REFERENCE.EthereumMainnet:
|
|
197
|
-
case exports.CHAIN_REFERENCE.EthereumRopsten:
|
|
198
|
-
case exports.CHAIN_REFERENCE.EthereumRinkeby:
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
case exports.CHAIN_NAMESPACE.Bitcoin: {
|
|
204
|
-
switch (n) {
|
|
205
|
-
case exports.CHAIN_REFERENCE.BitcoinMainnet:
|
|
206
|
-
case exports.CHAIN_REFERENCE.BitcoinTestnet:
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
throw new Error(`isChainId: invalid ChainId ${chainId}`);
|
|
213
|
-
};
|
|
214
|
-
exports.isChainId = isChainId;
|
|
215
|
-
exports.chainReferenceToNetworkType = Object.freeze({
|
|
216
|
-
[exports.CHAIN_REFERENCE.BitcoinMainnet]: types_1.NetworkTypes.MAINNET,
|
|
217
|
-
[exports.CHAIN_REFERENCE.BitcoinTestnet]: types_1.NetworkTypes.TESTNET,
|
|
218
|
-
[exports.CHAIN_REFERENCE.EthereumMainnet]: types_1.NetworkTypes.MAINNET,
|
|
219
|
-
[exports.CHAIN_REFERENCE.EthereumRopsten]: types_1.NetworkTypes.ETH_ROPSTEN,
|
|
220
|
-
[exports.CHAIN_REFERENCE.EthereumRinkeby]: types_1.NetworkTypes.ETH_RINKEBY,
|
|
221
|
-
[exports.CHAIN_REFERENCE.CosmosHubMainnet]: types_1.NetworkTypes.COSMOSHUB_MAINNET,
|
|
222
|
-
[exports.CHAIN_REFERENCE.CosmosHubVega]: types_1.NetworkTypes.COSMOSHUB_VEGA,
|
|
223
|
-
[exports.CHAIN_REFERENCE.OsmosisMainnet]: types_1.NetworkTypes.OSMOSIS_MAINNET,
|
|
224
|
-
[exports.CHAIN_REFERENCE.OsmosisTestnet]: types_1.NetworkTypes.OSMOSIS_TESTNET
|
|
225
|
-
});
|
|
226
|
-
exports.networkTypeToChainReference = (0, invert_1.default)(exports.chainReferenceToNetworkType);
|
|
227
|
-
exports.chainNamespaceToChainType = Object.freeze({
|
|
228
|
-
[exports.CHAIN_NAMESPACE.Bitcoin]: types_1.ChainTypes.Bitcoin,
|
|
229
|
-
[exports.CHAIN_NAMESPACE.Ethereum]: types_1.ChainTypes.Ethereum,
|
|
230
|
-
[exports.CHAIN_NAMESPACE.Cosmos]: types_1.ChainTypes.Cosmos
|
|
231
|
-
});
|
|
232
21
|
exports.toCAIP2 = exports.toChainId;
|
|
233
22
|
exports.fromCAIP2 = exports.fromChainId;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AssetNamespace } from './assetId/assetId';
|
|
2
|
+
import { ChainNamespace, ChainReference } from './chainId/chainId';
|
|
3
|
+
export declare const btcAssetId = "bip122:000000000019d6689c085ae165831e93/slip44:0";
|
|
4
|
+
export declare const ethAssetId = "eip155:1/slip44:60";
|
|
5
|
+
export declare const cosmosAssetId = "cosmos:cosmoshub-4/slip44:118";
|
|
6
|
+
export declare const osmosisAssetId = "cosmos:osmosis-1/slip44:118";
|
|
7
|
+
export declare const ethChainId = "eip155:1";
|
|
8
|
+
export declare const btcChainId = "bip122:000000000019d6689c085ae165831e93";
|
|
9
|
+
export declare const cosmosChainId = "cosmos:cosmoshub-4";
|
|
10
|
+
export declare const osmosisChainId = "cosmos:osmosis-1";
|
|
11
|
+
export declare const CHAIN_NAMESPACE: {
|
|
12
|
+
readonly Ethereum: "eip155";
|
|
13
|
+
readonly Bitcoin: "bip122";
|
|
14
|
+
readonly Cosmos: "cosmos";
|
|
15
|
+
};
|
|
16
|
+
declare type ValidChainMap = {
|
|
17
|
+
[k in ChainNamespace]: ChainReference[];
|
|
18
|
+
};
|
|
19
|
+
export declare const CHAIN_REFERENCE: {
|
|
20
|
+
readonly EthereumMainnet: "1";
|
|
21
|
+
readonly EthereumRopsten: "3";
|
|
22
|
+
readonly EthereumRinkeby: "4";
|
|
23
|
+
readonly BitcoinMainnet: "000000000019d6689c085ae165831e93";
|
|
24
|
+
readonly BitcoinTestnet: "000000000933ea01ad0ee984209779ba";
|
|
25
|
+
readonly CosmosHubMainnet: "cosmoshub-4";
|
|
26
|
+
readonly CosmosHubVega: "vega-testnet";
|
|
27
|
+
readonly OsmosisMainnet: "osmosis-1";
|
|
28
|
+
readonly OsmosisTestnet: "osmo-testnet-1";
|
|
29
|
+
};
|
|
30
|
+
export declare const VALID_CHAIN_IDS: ValidChainMap;
|
|
31
|
+
declare type ValidAssetNamespace = {
|
|
32
|
+
[k in ChainNamespace]: AssetNamespace[];
|
|
33
|
+
};
|
|
34
|
+
export declare const VALID_ASSET_NAMESPACE: ValidAssetNamespace;
|
|
35
|
+
export declare const ASSET_NAMESPACE_STRINGS: readonly ["cw20", "cw721", "erc20", "erc721", "slip44", "native", "ibc"];
|
|
36
|
+
export declare const ASSET_REFERENCE: {
|
|
37
|
+
readonly Bitcoin: "0";
|
|
38
|
+
readonly Ethereum: "60";
|
|
39
|
+
readonly Cosmos: "118";
|
|
40
|
+
readonly Osmosis: "118";
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ASSET_REFERENCE = exports.ASSET_NAMESPACE_STRINGS = exports.VALID_ASSET_NAMESPACE = exports.VALID_CHAIN_IDS = exports.CHAIN_REFERENCE = exports.CHAIN_NAMESPACE = exports.osmosisChainId = exports.cosmosChainId = exports.btcChainId = exports.ethChainId = exports.osmosisAssetId = exports.cosmosAssetId = exports.ethAssetId = exports.btcAssetId = void 0;
|
|
4
|
+
exports.btcAssetId = 'bip122:000000000019d6689c085ae165831e93/slip44:0';
|
|
5
|
+
exports.ethAssetId = 'eip155:1/slip44:60';
|
|
6
|
+
exports.cosmosAssetId = 'cosmos:cosmoshub-4/slip44:118';
|
|
7
|
+
exports.osmosisAssetId = 'cosmos:osmosis-1/slip44:118';
|
|
8
|
+
exports.ethChainId = 'eip155:1';
|
|
9
|
+
exports.btcChainId = 'bip122:000000000019d6689c085ae165831e93';
|
|
10
|
+
exports.cosmosChainId = 'cosmos:cosmoshub-4';
|
|
11
|
+
exports.osmosisChainId = 'cosmos:osmosis-1';
|
|
12
|
+
exports.CHAIN_NAMESPACE = {
|
|
13
|
+
Ethereum: 'eip155',
|
|
14
|
+
Bitcoin: 'bip122',
|
|
15
|
+
Cosmos: 'cosmos'
|
|
16
|
+
};
|
|
17
|
+
exports.CHAIN_REFERENCE = {
|
|
18
|
+
EthereumMainnet: '1',
|
|
19
|
+
EthereumRopsten: '3',
|
|
20
|
+
EthereumRinkeby: '4',
|
|
21
|
+
// EthereumKovan: '42', // currently unsupported by ShapeShift
|
|
22
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0122.mediawiki#definition-of-chain-id
|
|
23
|
+
// chainId uses max length of 32 chars of the genesis block
|
|
24
|
+
BitcoinMainnet: '000000000019d6689c085ae165831e93',
|
|
25
|
+
BitcoinTestnet: '000000000933ea01ad0ee984209779ba',
|
|
26
|
+
CosmosHubMainnet: 'cosmoshub-4',
|
|
27
|
+
CosmosHubVega: 'vega-testnet',
|
|
28
|
+
OsmosisMainnet: 'osmosis-1',
|
|
29
|
+
OsmosisTestnet: 'osmo-testnet-1'
|
|
30
|
+
};
|
|
31
|
+
exports.VALID_CHAIN_IDS = Object.freeze({
|
|
32
|
+
[exports.CHAIN_NAMESPACE.Bitcoin]: [exports.CHAIN_REFERENCE.BitcoinMainnet, exports.CHAIN_REFERENCE.BitcoinTestnet],
|
|
33
|
+
[exports.CHAIN_NAMESPACE.Ethereum]: [
|
|
34
|
+
exports.CHAIN_REFERENCE.EthereumMainnet,
|
|
35
|
+
exports.CHAIN_REFERENCE.EthereumRopsten,
|
|
36
|
+
exports.CHAIN_REFERENCE.EthereumRinkeby
|
|
37
|
+
],
|
|
38
|
+
[exports.CHAIN_NAMESPACE.Cosmos]: [
|
|
39
|
+
exports.CHAIN_REFERENCE.CosmosHubMainnet,
|
|
40
|
+
exports.CHAIN_REFERENCE.CosmosHubVega,
|
|
41
|
+
exports.CHAIN_REFERENCE.OsmosisMainnet,
|
|
42
|
+
exports.CHAIN_REFERENCE.OsmosisTestnet
|
|
43
|
+
]
|
|
44
|
+
});
|
|
45
|
+
exports.VALID_ASSET_NAMESPACE = Object.freeze({
|
|
46
|
+
[exports.CHAIN_NAMESPACE.Bitcoin]: ['slip44'],
|
|
47
|
+
[exports.CHAIN_NAMESPACE.Ethereum]: ['slip44', 'erc20', 'erc721'],
|
|
48
|
+
[exports.CHAIN_NAMESPACE.Cosmos]: ['cw20', 'cw721', 'ibc', 'native', 'slip44']
|
|
49
|
+
});
|
|
50
|
+
exports.ASSET_NAMESPACE_STRINGS = [
|
|
51
|
+
'cw20',
|
|
52
|
+
'cw721',
|
|
53
|
+
'erc20',
|
|
54
|
+
'erc721',
|
|
55
|
+
'slip44',
|
|
56
|
+
'native',
|
|
57
|
+
'ibc'
|
|
58
|
+
];
|
|
59
|
+
exports.ASSET_REFERENCE = {
|
|
60
|
+
Bitcoin: '0',
|
|
61
|
+
Ethereum: '60',
|
|
62
|
+
Cosmos: '118',
|
|
63
|
+
Osmosis: '118'
|
|
64
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -31,3 +31,6 @@ exports.adapters = __importStar(require("./adapters"));
|
|
|
31
31
|
__exportStar(require("./chainId/chainId"), exports);
|
|
32
32
|
__exportStar(require("./accountId/accountId"), exports);
|
|
33
33
|
__exportStar(require("./assetId/assetId"), exports);
|
|
34
|
+
__exportStar(require("./utils"), exports);
|
|
35
|
+
__exportStar(require("./constants"), exports);
|
|
36
|
+
__exportStar(require("./typeGuards"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AssetId, AssetNamespace, AssetReference } from './assetId/assetId';
|
|
2
|
+
import { ChainId, ChainNamespace, ChainReference } from './chainId/chainId';
|
|
3
|
+
export declare const isChainNamespace: (maybeChainNamespace: ChainNamespace | string) => maybeChainNamespace is ChainNamespace;
|
|
4
|
+
export declare const isChainReference: (maybeChainReference: ChainReference | string) => maybeChainReference is ChainReference;
|
|
5
|
+
export declare const isAssetNamespace: (maybeAssetNamespace: AssetNamespace | string) => maybeAssetNamespace is "cw20" | "cw721" | "erc20" | "erc721" | "slip44" | "native" | "ibc";
|
|
6
|
+
export declare const isAssetReference: (maybeAssetReference: AssetReference | string) => maybeAssetReference is AssetReference;
|
|
7
|
+
export declare const isAssetId: (maybeAssetId: AssetId | string) => maybeAssetId is AssetReference;
|
|
8
|
+
export declare const isChainId: (maybeChainId: ChainId | string) => maybeChainId is string;
|
|
9
|
+
export declare const assertIsChainId: (value: ChainId | string | undefined) => asserts value is ChainId;
|
|
10
|
+
export declare const assertIsChainNamespace: (value: ChainNamespace | string | undefined) => asserts value is ChainNamespace;
|
|
11
|
+
export declare const assertIsChainReference: (value: ChainReference | string | undefined) => asserts value is ChainReference;
|
|
12
|
+
export declare const assertIsAssetNamespace: (value: AssetNamespace | string | undefined) => asserts value is AssetNamespace;
|
|
13
|
+
export declare const assertIsAssetReference: (value: AssetReference | string | undefined) => asserts value is AssetReference;
|
|
14
|
+
export declare const assertValidChainPartsPair: (chainNamespace: ChainNamespace, chainReference: ChainReference) => void;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertValidChainPartsPair = exports.assertIsAssetReference = exports.assertIsAssetNamespace = exports.assertIsChainReference = exports.assertIsChainNamespace = exports.assertIsChainId = exports.isChainId = exports.isAssetId = exports.isAssetReference = exports.isAssetNamespace = exports.isChainReference = exports.isChainNamespace = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const isChainNamespace = (maybeChainNamespace) => Object.values(constants_1.CHAIN_NAMESPACE).some((s) => s === maybeChainNamespace);
|
|
7
|
+
exports.isChainNamespace = isChainNamespace;
|
|
8
|
+
const isChainReference = (maybeChainReference) => Object.values(constants_1.CHAIN_REFERENCE).some((s) => s === maybeChainReference);
|
|
9
|
+
exports.isChainReference = isChainReference;
|
|
10
|
+
const isAssetNamespace = (maybeAssetNamespace) => constants_1.ASSET_NAMESPACE_STRINGS.some((s) => s === maybeAssetNamespace);
|
|
11
|
+
exports.isAssetNamespace = isAssetNamespace;
|
|
12
|
+
const isAssetReference = (maybeAssetReference) => Object.values(constants_1.ASSET_REFERENCE).some((s) => s === maybeAssetReference);
|
|
13
|
+
exports.isAssetReference = isAssetReference;
|
|
14
|
+
const isAssetId = (maybeAssetId) => {
|
|
15
|
+
var _a;
|
|
16
|
+
const matches = (_a = utils_1.parseAssetIdRegExp.exec(maybeAssetId)) === null || _a === void 0 ? void 0 : _a.groups;
|
|
17
|
+
return (!!matches &&
|
|
18
|
+
(0, exports.isChainNamespace)(matches.chainNamespace) &&
|
|
19
|
+
(0, exports.isChainReference)(matches.chainReference) &&
|
|
20
|
+
(0, exports.isAssetNamespace)(matches.assetNamespace));
|
|
21
|
+
};
|
|
22
|
+
exports.isAssetId = isAssetId;
|
|
23
|
+
const isChainId = (maybeChainId) => {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
// https://regex101.com/r/iCqlyB/1
|
|
26
|
+
const chainIdRegExp = /(?<chainNamespace>[-a-z\d]{3,8}):(?<chainReference>[-a-zA-Z\d]{1,32})/;
|
|
27
|
+
const [maybeChainNamespace, maybeChainReference] = (_b = (_a = chainIdRegExp.exec(maybeChainId)) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : [];
|
|
28
|
+
return ((0, exports.isChainNamespace)(maybeChainNamespace) &&
|
|
29
|
+
(0, exports.isChainReference)(maybeChainReference) &&
|
|
30
|
+
(0, utils_1.isValidChainPartsPair)(maybeChainNamespace, maybeChainReference));
|
|
31
|
+
};
|
|
32
|
+
exports.isChainId = isChainId;
|
|
33
|
+
const getTypeGuardAssertion = (typeGuard, message) => {
|
|
34
|
+
return (value) => {
|
|
35
|
+
if ((value && !typeGuard(value)) || !value)
|
|
36
|
+
throw new Error(`${message}: ${value}`);
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
exports.assertIsChainId = getTypeGuardAssertion(exports.isChainId, 'assertIsChainId: unsupported ChainId');
|
|
40
|
+
exports.assertIsChainNamespace = getTypeGuardAssertion(exports.isChainNamespace, 'assertIsChainNamespace: unsupported ChainNamespace');
|
|
41
|
+
exports.assertIsChainReference = getTypeGuardAssertion(exports.isChainReference, 'assertIsChainReference: unsupported ChainReference');
|
|
42
|
+
exports.assertIsAssetNamespace = getTypeGuardAssertion(exports.isAssetNamespace, 'assertIsAssetNamespace: unsupported AssetNamespace');
|
|
43
|
+
exports.assertIsAssetReference = getTypeGuardAssertion(exports.isAssetReference, 'assertIsAssetReference: unsupported AssetReference');
|
|
44
|
+
const assertValidChainPartsPair = (chainNamespace, chainReference) => {
|
|
45
|
+
if (!(0, utils_1.isValidChainPartsPair)(chainNamespace, chainReference))
|
|
46
|
+
throw new Error(`toAssetId: Chain Reference ${chainReference} not supported for Chain Namespace ${chainNamespace}`);
|
|
47
|
+
};
|
|
48
|
+
exports.assertValidChainPartsPair = assertValidChainPartsPair;
|