@shapeshiftoss/caip 1.0.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/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # CAIPs
2
+
3
+ This package is ShapeShift's partial implementation of [CAIPs](https://github.com/ChainAgnostic/CAIPs) - Chain Agnostic Improvement Protocols.
4
+
5
+ It is not exhaustive and is currently only used internally.
6
+
7
+ ## CAIP-2 - Blockchain ID Specification
8
+
9
+ https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md
10
+
11
+ Usage
12
+
13
+ ### `toCAIP2`
14
+
15
+ ```ts
16
+ const chain = ChainTypes.Ethereum
17
+ const network = NetworkTypes.MAINNET
18
+ const result = toCAIP2({ chain, network })
19
+ expect(result).toEqual('eip155:1')
20
+ ```
21
+
22
+ ### `fromCAIP2`
23
+
24
+ ```ts
25
+ const ethCaip2 = 'eip155:1'
26
+ const { chain, network } = fromCAIP2(ethCaip2)
27
+ expect(chain).toEqual(ChainTypes.Ethereum)
28
+ expect(network).toEqual(NetworkTypes.MAINNET)
29
+ ```
30
+
31
+ ## CAIP-19 - Asset Type and Asset ID Specification
32
+
33
+ https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-19.md
34
+
35
+ Usage
36
+
37
+ ### `toCAIP19`
38
+
39
+ Ether
40
+
41
+ ```ts
42
+ const chain = ChainTypes.Ethereum
43
+ const network = NetworkTypes.MAINNET
44
+ const result = toCAIP19({ chain, network })
45
+ expect(result).toEqual('eip155:1/slip44:60')
46
+ ```
47
+
48
+ ERC20 token
49
+
50
+ ```ts
51
+ const chain = ChainTypes.Ethereum
52
+ const network = NetworkTypes.MAINNET
53
+ const contractType = ContractTypes.ERC20
54
+ const tokenId = '0xc770eefad204b5180df6a14ee197d99d808ee52d'
55
+ const result = toCAIP19({ chain, network, contractType, tokenId })
56
+ expect(result).toEqual('eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d')
57
+ ```
58
+
59
+ ### `fromCAIP19`
60
+
61
+ Ether
62
+
63
+ ```ts
64
+ const caip19 = 'eip155:1/slip44:60'
65
+ const { chain, network, contractType, tokenId } = fromCAIP19(caip19)
66
+ expect(chain).toEqual(ChainTypes.Ethereum)
67
+ expect(network).toEqual(NetworkTypes.MAINNET)
68
+ expect(contractType).toBeUndefined()
69
+ expect(tokenId).toBeUndefined()
70
+ ```
71
+
72
+ ERC20 token
73
+
74
+ ```ts
75
+ const caip19 = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
76
+ const { chain, network, contractType, tokenId } = fromCAIP19(caip19)
77
+ expect(chain).toEqual(ChainTypes.Ethereum)
78
+ expect(network).toEqual(NetworkTypes.MAINNET)
79
+ expect(contractType).toEqual(ContractTypes.ERC20)
80
+ expect(tokenId).toEqual('0xc770eefad204b5180df6a14ee197d99d808ee52d')
81
+ ```
@@ -0,0 +1,27 @@
1
+ import { ChainTypes, ContractTypes, NetworkTypes } from '@shapeshiftoss/types';
2
+ export declare enum AssetNamespace {
3
+ ERC20 = "erc20",
4
+ ERC721 = "erc721",
5
+ Slip44 = "slip44"
6
+ }
7
+ export declare enum AssetReference {
8
+ Bitcoin = 0,
9
+ Ethereum = 60
10
+ }
11
+ declare type ToCAIP19Args = {
12
+ chain: ChainTypes;
13
+ network: NetworkTypes;
14
+ contractType?: ContractTypes;
15
+ tokenId?: string;
16
+ };
17
+ declare type ToCAIP19 = (args: ToCAIP19Args) => string;
18
+ export declare const toCAIP19: ToCAIP19;
19
+ declare type FromCAIP19Return = {
20
+ chain: ChainTypes;
21
+ network: NetworkTypes;
22
+ contractType?: ContractTypes;
23
+ tokenId?: string;
24
+ };
25
+ declare type FromCAIP19 = (caip19: string) => FromCAIP19Return;
26
+ export declare const fromCAIP19: FromCAIP19;
27
+ export {};
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromCAIP19 = exports.toCAIP19 = exports.AssetReference = exports.AssetNamespace = void 0;
4
+ const caip2_1 = require("./../caip2/caip2");
5
+ // https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-19.md
6
+ const types_1 = require("@shapeshiftoss/types");
7
+ var AssetNamespace;
8
+ (function (AssetNamespace) {
9
+ AssetNamespace["ERC20"] = "erc20";
10
+ AssetNamespace["ERC721"] = "erc721";
11
+ AssetNamespace["Slip44"] = "slip44";
12
+ })(AssetNamespace = exports.AssetNamespace || (exports.AssetNamespace = {}));
13
+ var AssetReference;
14
+ (function (AssetReference) {
15
+ AssetReference[AssetReference["Bitcoin"] = 0] = "Bitcoin";
16
+ AssetReference[AssetReference["Ethereum"] = 60] = "Ethereum";
17
+ })(AssetReference = exports.AssetReference || (exports.AssetReference = {}));
18
+ const toCAIP19 = ({ chain, network, contractType, tokenId }) => {
19
+ const caip2 = (0, caip2_1.toCAIP2)({ chain, network });
20
+ switch (chain) {
21
+ case types_1.ChainTypes.Ethereum: {
22
+ if (contractType) {
23
+ if (!tokenId) {
24
+ throw new Error(`toCAIP19: no tokenId provided with contract type ${contractType}`);
25
+ }
26
+ const shapeShiftToCAIP19Namespace = {
27
+ [types_1.ChainTypes.Ethereum]: {
28
+ [types_1.ContractTypes.ERC20]: AssetNamespace.ERC20,
29
+ [types_1.ContractTypes.ERC721]: AssetNamespace.ERC721
30
+ }
31
+ };
32
+ switch (contractType) {
33
+ case types_1.ContractTypes.ERC20:
34
+ case types_1.ContractTypes.ERC721: {
35
+ const namespace = shapeShiftToCAIP19Namespace[chain][contractType];
36
+ if (!tokenId.startsWith('0x')) {
37
+ throw new Error(`toCAIP19: tokenId must start with 0x: ${tokenId}`);
38
+ }
39
+ if (tokenId.length !== 42) {
40
+ throw new Error(`toCAIP19: tokenId length must be 42, length: ${tokenId.length}`);
41
+ }
42
+ return `${caip2}/${namespace}:${tokenId}`;
43
+ }
44
+ default: {
45
+ throw new Error(`toCAIP19: unsupported contractType ${contractType} on chain ${chain}`);
46
+ }
47
+ }
48
+ }
49
+ else {
50
+ if (tokenId) {
51
+ throw new Error(`toCAIP19: tokenId provided without contract type`);
52
+ }
53
+ return `${caip2}/${AssetNamespace.Slip44}:${AssetReference.Ethereum}`;
54
+ }
55
+ }
56
+ case types_1.ChainTypes.Bitcoin: {
57
+ return `${caip2}/${AssetNamespace.Slip44}:${AssetReference.Bitcoin}`;
58
+ }
59
+ }
60
+ };
61
+ exports.toCAIP19 = toCAIP19;
62
+ const fromCAIP19 = (caip19) => {
63
+ const [caip2, namespaceAndReference] = caip19.split('/');
64
+ if (!(caip2 && namespaceAndReference)) {
65
+ throw new Error(`fromCAIP19: error parsing caip19, caip2: ${caip2}, namespaceAndReference: ${namespaceAndReference}`);
66
+ }
67
+ const [namespace, referenceString] = namespaceAndReference.split(':');
68
+ if (!(namespace && referenceString)) {
69
+ throw new Error(`fromCAIP19: error parsing namespace and reference, namespace: ${namespace}, reference: ${referenceString}`);
70
+ }
71
+ const reference = Number(referenceString);
72
+ const { chain, network } = (0, caip2_1.fromCAIP2)(caip2);
73
+ switch (chain) {
74
+ case types_1.ChainTypes.Bitcoin: {
75
+ switch (namespace) {
76
+ case AssetNamespace.Slip44: {
77
+ switch (reference) {
78
+ case AssetReference.Bitcoin: {
79
+ return { chain, network };
80
+ }
81
+ case AssetReference.Ethereum:
82
+ default: {
83
+ throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
84
+ }
85
+ }
86
+ }
87
+ case AssetNamespace.ERC20:
88
+ case AssetNamespace.ERC721:
89
+ default: {
90
+ throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
91
+ }
92
+ }
93
+ }
94
+ case types_1.ChainTypes.Ethereum: {
95
+ switch (namespace) {
96
+ case AssetNamespace.Slip44: {
97
+ switch (reference) {
98
+ case AssetReference.Ethereum: {
99
+ return { chain, network };
100
+ }
101
+ case AssetReference.Bitcoin:
102
+ default: {
103
+ throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
104
+ }
105
+ }
106
+ }
107
+ case AssetNamespace.ERC20: {
108
+ const contractType = types_1.ContractTypes.ERC20;
109
+ const tokenId = referenceString;
110
+ return { chain, network, contractType, tokenId };
111
+ }
112
+ case AssetNamespace.ERC721: {
113
+ const contractType = types_1.ContractTypes.ERC721;
114
+ const tokenId = referenceString;
115
+ return { chain, network, contractType, tokenId };
116
+ }
117
+ }
118
+ }
119
+ }
120
+ throw new Error(`fromCAIP19: error parsing caip19: ${caip19}`);
121
+ };
122
+ exports.fromCAIP19 = fromCAIP19;
@@ -0,0 +1,25 @@
1
+ import { ChainTypes, NetworkTypes } from '@shapeshiftoss/types';
2
+ export declare enum ChainNamespace {
3
+ Ethereum = "eip155",
4
+ Bitcoin = "bip122"
5
+ }
6
+ export declare enum ChainReference {
7
+ EthereumMainnet = "1",
8
+ EthereumRopsten = "3",
9
+ EthereumRinkeby = "4",
10
+ BitcoinMainnet = "000000000019d6689c085ae165831e93",
11
+ BitcoinTestnet = "000000000933ea01ad0ee984209779ba"
12
+ }
13
+ declare type ToCAIP2Args = {
14
+ chain: ChainTypes;
15
+ network: NetworkTypes;
16
+ };
17
+ declare type ToCAIP2 = (args: ToCAIP2Args) => string;
18
+ export declare const toCAIP2: ToCAIP2;
19
+ declare type FromCAIP2Return = {
20
+ chain: ChainTypes;
21
+ network: NetworkTypes;
22
+ };
23
+ declare type FromCAIP2 = (caip2: string) => FromCAIP2Return;
24
+ export declare const fromCAIP2: FromCAIP2;
25
+ export {};
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ // https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.fromCAIP2 = exports.toCAIP2 = exports.ChainReference = exports.ChainNamespace = void 0;
5
+ const types_1 = require("@shapeshiftoss/types");
6
+ var ChainNamespace;
7
+ (function (ChainNamespace) {
8
+ ChainNamespace["Ethereum"] = "eip155";
9
+ ChainNamespace["Bitcoin"] = "bip122";
10
+ })(ChainNamespace = exports.ChainNamespace || (exports.ChainNamespace = {}));
11
+ var ChainReference;
12
+ (function (ChainReference) {
13
+ ChainReference["EthereumMainnet"] = "1";
14
+ ChainReference["EthereumRopsten"] = "3";
15
+ ChainReference["EthereumRinkeby"] = "4";
16
+ // EthereumKovan = '42', // currently unsupported by shapeshift
17
+ // https://github.com/bitcoin/bips/blob/master/bip-0122.mediawiki#definition-of-chain-id
18
+ // caip2 uses max length of 32 chars of the genesis block
19
+ ChainReference["BitcoinMainnet"] = "000000000019d6689c085ae165831e93";
20
+ ChainReference["BitcoinTestnet"] = "000000000933ea01ad0ee984209779ba";
21
+ })(ChainReference = exports.ChainReference || (exports.ChainReference = {}));
22
+ const toCAIP2 = ({ chain, network }) => {
23
+ const shapeShiftToCAIP2 = {
24
+ [types_1.ChainTypes.Ethereum]: {
25
+ namespace: ChainNamespace.Ethereum,
26
+ reference: {
27
+ [types_1.NetworkTypes.MAINNET]: ChainReference.EthereumMainnet,
28
+ [types_1.NetworkTypes.ETH_ROPSTEN]: ChainReference.EthereumRopsten,
29
+ [types_1.NetworkTypes.ETH_RINKEBY]: ChainReference.EthereumRinkeby
30
+ }
31
+ },
32
+ [types_1.ChainTypes.Bitcoin]: {
33
+ namespace: ChainNamespace.Bitcoin,
34
+ reference: {
35
+ [types_1.NetworkTypes.MAINNET]: ChainReference.BitcoinMainnet,
36
+ [types_1.NetworkTypes.TESTNET]: ChainReference.BitcoinTestnet
37
+ }
38
+ }
39
+ };
40
+ const namespace = shapeShiftToCAIP2[chain].namespace;
41
+ switch (chain) {
42
+ case types_1.ChainTypes.Ethereum: {
43
+ const referenceMap = shapeShiftToCAIP2[chain].reference;
44
+ switch (network) {
45
+ case types_1.NetworkTypes.MAINNET:
46
+ case types_1.NetworkTypes.ETH_ROPSTEN:
47
+ case types_1.NetworkTypes.ETH_RINKEBY: {
48
+ const reference = referenceMap[network];
49
+ const caip2 = `${namespace}:${reference}`;
50
+ return caip2;
51
+ }
52
+ }
53
+ break;
54
+ }
55
+ case types_1.ChainTypes.Bitcoin: {
56
+ const referenceMap = shapeShiftToCAIP2[chain].reference;
57
+ switch (network) {
58
+ case types_1.NetworkTypes.MAINNET:
59
+ case types_1.NetworkTypes.TESTNET: {
60
+ const reference = referenceMap[network];
61
+ const caip2 = `${namespace}:${reference}`;
62
+ return caip2;
63
+ }
64
+ }
65
+ }
66
+ }
67
+ throw new Error(`toCAIP2: unsupported ${chain} network: ${network}`);
68
+ };
69
+ exports.toCAIP2 = toCAIP2;
70
+ const fromCAIP2 = (caip2) => {
71
+ const [c, n] = caip2.split(':');
72
+ if (!(c && n)) {
73
+ throw new Error(`fromCAIP19: error parsing caip19, chain: ${c}, network: ${n}`);
74
+ }
75
+ switch (c) {
76
+ case 'eip155': {
77
+ const chain = types_1.ChainTypes.Ethereum;
78
+ switch (n) {
79
+ case ChainReference.EthereumMainnet: {
80
+ const network = types_1.NetworkTypes.MAINNET;
81
+ return { chain, network };
82
+ }
83
+ case ChainReference.EthereumRopsten: {
84
+ const network = types_1.NetworkTypes.ETH_ROPSTEN;
85
+ return { chain, network };
86
+ }
87
+ case ChainReference.EthereumRinkeby: {
88
+ const network = types_1.NetworkTypes.ETH_RINKEBY;
89
+ return { chain, network };
90
+ }
91
+ default: {
92
+ throw new Error(`fromCAIP19: unsupported ${c} network: ${n}`);
93
+ }
94
+ }
95
+ }
96
+ case 'bip122': {
97
+ const chain = types_1.ChainTypes.Bitcoin;
98
+ switch (n) {
99
+ case ChainReference.BitcoinMainnet: {
100
+ const network = types_1.NetworkTypes.MAINNET;
101
+ return { chain, network };
102
+ }
103
+ case ChainReference.BitcoinTestnet: {
104
+ const network = types_1.NetworkTypes.TESTNET;
105
+ return { chain, network };
106
+ }
107
+ default: {
108
+ throw new Error(`fromCAIP19: unsupported ${c} network: ${n}`);
109
+ }
110
+ }
111
+ }
112
+ }
113
+ throw new Error(`fromCAIP19: unsupported chain: ${c}`);
114
+ };
115
+ exports.fromCAIP2 = fromCAIP2;
@@ -0,0 +1,3 @@
1
+ import * as caip2 from './caip2/caip2';
2
+ import * as caip19 from './caip19/caip19';
3
+ export { caip2, caip19 };
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.caip19 = exports.caip2 = void 0;
23
+ const caip2 = __importStar(require("./caip2/caip2"));
24
+ exports.caip2 = caip2;
25
+ const caip19 = __importStar(require("./caip19/caip19"));
26
+ exports.caip19 = caip19;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@shapeshiftoss/caip",
3
+ "version": "1.0.0",
4
+ "description": "CAIP Implementation",
5
+ "homepage": "",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/shapeshift/lib"
18
+ },
19
+ "scripts": {
20
+ "build": "yarn clean && tsc --project tsconfig.build.json",
21
+ "clean": "rm -rf dist",
22
+ "dev": "tsc --watch",
23
+ "test": "jest test",
24
+ "type-check": "tsc --project ./tsconfig.json --noEmit"
25
+ },
26
+ "dependencies": {
27
+ "@shapeshiftoss/types": "^1.4.0"
28
+ }
29
+ }