@defuse-protocol/crosschain-assetid 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,141 @@
1
+ # 1cs_v1 — Unified Cross-Chain Asset Identifier
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install @defuse-protocol/crosschain-assetid
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```typescript
12
+ import {parse1cs, stringify1cs} from '@defuse-protocol/crosschain-assetid';
13
+
14
+ const assetid = stringify1cs({
15
+ version: "v1",
16
+ chain: "eth",
17
+ namespace: "erc20",
18
+ reference: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
19
+ })
20
+
21
+ const obj = parse1cs("1cs_v1:eth:erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
22
+ ```
23
+
24
+ ## Purpose
25
+
26
+ `1cs_v1` is a **URL-safe, compact, human-readable** identifier format for
27
+ representing **any asset** (crypto token, NFT, native coin, wrapped/bridged asset,
28
+ or even fiat) **across chains** in a consistent way.
29
+
30
+ It is designed to:
31
+
32
+ - Let apps **refer to assets unambiguously** without chain-specific parsing logic.
33
+ - Work **cross-chain and cross-standard** — one format for EVM, Solana, Cosmos, NEAR,
34
+ Stellar, Aptos, Sui, fiat, etc.
35
+ - Be **safe in URLs, filenames, and APIs** without extra escaping.
36
+ - Enable **simple parsing** into an object with predictable fields.
37
+ - Serve as a **unified key** for balances, swaps, bridges, quotes, and off-ramps.
38
+
39
+ ---
40
+
41
+ ## Format
42
+
43
+ ```
44
+ 1cs_v1:<chain>:<namespace>:<reference>[:<selector>]
45
+ ```
46
+
47
+ ### Components
48
+
49
+ | Part | Required? | Description |
50
+ |---------------|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------|
51
+ | `1cs_v1` | ✅ | Format prefix and version. 1cs stands for [1ClickSwap](https://docs.near-intents.org/near-intents/integration/distribution-channels/1click-api). |
52
+ | `<chain>` | ✅ | Lowercase slug (e.g. `eth`, `eth-sepolia`, `solana`, `near`, `polygon`), can encode testnet in slug. |
53
+ | `<namespace>` | ✅ | Asset standard/kind: `erc20`, `erc721`, `spl`, `near-nft`, `aptos-coin`, `fiat`, etc. |
54
+ | `<reference>` | ✅ | Contract/mint/account/denom/issuer — **URI-encoded** to safely hold `:`, `/`, spaces, unicode, etc. |
55
+ | `<selector>` | optional | Sub-asset selector: token ID, module struct, Stellar code, etc. — also **URI-encoded**. |
56
+
57
+ ---
58
+
59
+ ## Examples
60
+
61
+ ### Fungible tokens
62
+
63
+ ```
64
+ 1cs_v1:eth:erc20:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 # USDC on Ethereum
65
+ 1cs_v1:base:erc20:0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 # USDC on Base
66
+ 1cs_v1:fiat:iso4217:USD # US Dollar (fiat)
67
+ ```
68
+
69
+ ### NFTs
70
+
71
+ ```
72
+ 1cs_v1:eth:erc721:0x1234567890abcdef1234567890abcdef12345678:42
73
+ 1cs_v1:near:nep171:apes.coolnft.near:series%3A1%2Fblue%3A42
74
+ ```
75
+
76
+ ### Native coins
77
+
78
+ ```
79
+ 1cs_v1:eth:native:coin
80
+ 1cs_v1:zcash:native:coin
81
+ ```
82
+
83
+ ### Non-EVM standards
84
+
85
+ ```
86
+ 1cs_v1:solana:spl:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
87
+ 1cs_v1:aptos:aptos-coin:0x1%3A%3Aaptos_coin%3A%3AAptosCoin
88
+ ```
89
+
90
+ ---
91
+
92
+ ## Rationale & Design Choices
93
+
94
+ ### 1. URL safety
95
+
96
+ We encode `reference` and `selector` with `encodeURIComponent` so they are safe in URLs.
97
+
98
+ ### 2. Human-readable but unambiguous
99
+
100
+ - Prefix (`1cs_v1`) makes it clear what format is in use. Easy greppable across codebase.
101
+ - Chain slug + namespace covers most identification needs without extra lookups.
102
+ - Works for *both* blockchain and fiat.
103
+
104
+ ### 3. Multi-asset support
105
+
106
+ `selector` cleanly supports ERC-1155 IDs, Stellar asset codes, Cosmos token sub-denoms, Aptos/Sui struct names, etc.
107
+
108
+ ### 4. Bridged / wrapped assets
109
+
110
+ The ID identifies the *on-chain representation*. If you need to express the *origin*, bridge, issuer, or other
111
+ metadata — pass it as **query params** or in your object model, not in the core ID.
112
+
113
+ ---
114
+
115
+ ## Object Form
116
+
117
+ Parsing a `1cs_v1` string gives you:
118
+
119
+ ```ts
120
+ interface OneCsAsset {
121
+ version: "v1";
122
+ chain: string; // e.g. "eth", "near", "fiat"
123
+ namespace: string; // e.g. "erc20", "spl", "native"
124
+ reference: string; // decoded from URI
125
+ selector?: string; // decoded from URI if present
126
+ }
127
+ ```
128
+
129
+ Example:
130
+
131
+ ```
132
+ 1cs_v1:near:nep171:apes.coolnft.near:series%3A1%2Fblue%3A42
133
+
134
+ {
135
+ version: "v1",
136
+ chain: "near",
137
+ namespace: "nep171",
138
+ reference: "apes.coolnft.near",
139
+ selector: "series:1/blue:42"
140
+ }
141
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ fromUniswapToken: () => fromUniswapToken,
24
+ parse1cs: () => parse1cs,
25
+ stringify1cs: () => stringify1cs
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/parse.ts
30
+ function parse1cs(s) {
31
+ const parts = s.split(":");
32
+ if (parts.length < 4 || parts.length > 5 || parts[0] !== "1cs_v1") {
33
+ throw new Error("Invalid 1cs_v1 string");
34
+ }
35
+ const [_, chain, namespace, refEnc, selEnc] = parts;
36
+ const obj = {
37
+ version: "v1",
38
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
39
+ chain,
40
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
41
+ namespace,
42
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
43
+ reference: decodeURIComponent(refEnc)
44
+ };
45
+ if (selEnc !== void 0) obj.selector = decodeURIComponent(selEnc);
46
+ return obj;
47
+ }
48
+
49
+ // src/stringify.ts
50
+ function stringify1cs(o) {
51
+ const ref = encodeURIComponent(o.reference);
52
+ const sel = o.selector != null ? `:${encodeURIComponent(o.selector)}` : "";
53
+ return `1cs_v1:${o.chain}:${o.namespace}:${ref}${sel}`;
54
+ }
55
+
56
+ // src/caip2-slug-registry.json
57
+ var caip2_slug_registry_default = {
58
+ "eip155:1": "eth",
59
+ "eip155:10": "optimism",
60
+ "eip155:56": "bsc",
61
+ "eip155:100": "gnosis",
62
+ "eip155:137": "polygon",
63
+ "eip155:8453": "base",
64
+ "eip155:42161": "arbitrum",
65
+ "eip155:43114": "avalanche",
66
+ "eip155:80085": "berachain",
67
+ "bip122:000000000019d6689c085ae165831e93": "bitcoin",
68
+ "bip122:00040fe8ec8471911baa1db1266ea15d": "zcash",
69
+ "bip122:1a91e3dace36e2be3bf030a65679fe82": "doge",
70
+ "near:mainnet": "near",
71
+ "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp": "sol",
72
+ "tron:27Lqcw": "tron",
73
+ "xrpl:0": "xrpl",
74
+ "tvm:-239": "ton",
75
+ "sui:mainnet": "sui",
76
+ "aptos:mainnet": "aptos",
77
+ "stellar:pubnet": "stellar",
78
+ "cip34:1-764824073": "cardano"
79
+ };
80
+
81
+ // src/uniswap.ts
82
+ var evmSlugRegistry = caip2_slug_registry_default;
83
+ function fromUniswapToken(token, caip2slugMap) {
84
+ const caip2 = `eip155:${token.chainId}`;
85
+ const chain = caip2slugMap?.[caip2] ?? evmSlugRegistry[caip2];
86
+ if (chain == null) {
87
+ throw new Error(`Unsupported chainId = ${token.chainId}`);
88
+ }
89
+ const o = {
90
+ version: "v1",
91
+ chain,
92
+ namespace: "erc20",
93
+ reference: token.address
94
+ };
95
+ return stringify1cs(o);
96
+ }
97
+ // Annotate the CommonJS export names for ESM import in node:
98
+ 0 && (module.exports = {
99
+ fromUniswapToken,
100
+ parse1cs,
101
+ stringify1cs
102
+ });
@@ -0,0 +1,19 @@
1
+ interface OneCsAsset {
2
+ version: "v1";
3
+ chain: string;
4
+ namespace: string;
5
+ reference: string;
6
+ selector?: string;
7
+ }
8
+
9
+ declare function parse1cs(s: string): OneCsAsset;
10
+
11
+ declare function stringify1cs(o: OneCsAsset): string;
12
+
13
+ interface UniToken {
14
+ chainId: number;
15
+ address: string;
16
+ }
17
+ declare function fromUniswapToken<T extends UniToken>(token: T, caip2slugMap?: Record<string, string>): string;
18
+
19
+ export { type OneCsAsset, type UniToken, fromUniswapToken, parse1cs, stringify1cs };
@@ -0,0 +1,19 @@
1
+ interface OneCsAsset {
2
+ version: "v1";
3
+ chain: string;
4
+ namespace: string;
5
+ reference: string;
6
+ selector?: string;
7
+ }
8
+
9
+ declare function parse1cs(s: string): OneCsAsset;
10
+
11
+ declare function stringify1cs(o: OneCsAsset): string;
12
+
13
+ interface UniToken {
14
+ chainId: number;
15
+ address: string;
16
+ }
17
+ declare function fromUniswapToken<T extends UniToken>(token: T, caip2slugMap?: Record<string, string>): string;
18
+
19
+ export { type OneCsAsset, type UniToken, fromUniswapToken, parse1cs, stringify1cs };
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ // src/parse.ts
2
+ function parse1cs(s) {
3
+ const parts = s.split(":");
4
+ if (parts.length < 4 || parts.length > 5 || parts[0] !== "1cs_v1") {
5
+ throw new Error("Invalid 1cs_v1 string");
6
+ }
7
+ const [_, chain, namespace, refEnc, selEnc] = parts;
8
+ const obj = {
9
+ version: "v1",
10
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
11
+ chain,
12
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
13
+ namespace,
14
+ // biome-ignore lint/style/noNonNullAssertion: existence checked above
15
+ reference: decodeURIComponent(refEnc)
16
+ };
17
+ if (selEnc !== void 0) obj.selector = decodeURIComponent(selEnc);
18
+ return obj;
19
+ }
20
+
21
+ // src/stringify.ts
22
+ function stringify1cs(o) {
23
+ const ref = encodeURIComponent(o.reference);
24
+ const sel = o.selector != null ? `:${encodeURIComponent(o.selector)}` : "";
25
+ return `1cs_v1:${o.chain}:${o.namespace}:${ref}${sel}`;
26
+ }
27
+
28
+ // src/caip2-slug-registry.json
29
+ var caip2_slug_registry_default = {
30
+ "eip155:1": "eth",
31
+ "eip155:10": "optimism",
32
+ "eip155:56": "bsc",
33
+ "eip155:100": "gnosis",
34
+ "eip155:137": "polygon",
35
+ "eip155:8453": "base",
36
+ "eip155:42161": "arbitrum",
37
+ "eip155:43114": "avalanche",
38
+ "eip155:80085": "berachain",
39
+ "bip122:000000000019d6689c085ae165831e93": "bitcoin",
40
+ "bip122:00040fe8ec8471911baa1db1266ea15d": "zcash",
41
+ "bip122:1a91e3dace36e2be3bf030a65679fe82": "doge",
42
+ "near:mainnet": "near",
43
+ "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp": "sol",
44
+ "tron:27Lqcw": "tron",
45
+ "xrpl:0": "xrpl",
46
+ "tvm:-239": "ton",
47
+ "sui:mainnet": "sui",
48
+ "aptos:mainnet": "aptos",
49
+ "stellar:pubnet": "stellar",
50
+ "cip34:1-764824073": "cardano"
51
+ };
52
+
53
+ // src/uniswap.ts
54
+ var evmSlugRegistry = caip2_slug_registry_default;
55
+ function fromUniswapToken(token, caip2slugMap) {
56
+ const caip2 = `eip155:${token.chainId}`;
57
+ const chain = caip2slugMap?.[caip2] ?? evmSlugRegistry[caip2];
58
+ if (chain == null) {
59
+ throw new Error(`Unsupported chainId = ${token.chainId}`);
60
+ }
61
+ const o = {
62
+ version: "v1",
63
+ chain,
64
+ namespace: "erc20",
65
+ reference: token.address
66
+ };
67
+ return stringify1cs(o);
68
+ }
69
+ export {
70
+ fromUniswapToken,
71
+ parse1cs,
72
+ stringify1cs
73
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@defuse-protocol/crosschain-assetid",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "./schemas/1cs_v1-object.schema.json": "./schemas/1cs_v1-object.schema.json",
18
+ "./schemas/1cs_v1-string.schema.json": "./schemas/1cs_v1-string.schema.json"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "schemas"
23
+ ],
24
+ "devDependencies": {
25
+ "ajv": "^8.17.1",
26
+ "tsup": "^8.0.2",
27
+ "typescript": "^5.4.2"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "dev": "tsup --watch",
32
+ "lint": "biome check .",
33
+ "format": "biome format --write .",
34
+ "typecheck": "tsc --noEmit"
35
+ }
36
+ }
@@ -0,0 +1,52 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://example.com/1cs_v1-object.schema.json",
4
+ "title": "1cs_v1 asset (object form, encodeURIComponent on stringify)",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "properties": {
8
+ "version": {
9
+ "const": "v1"
10
+ },
11
+ "chain": {
12
+ "type": "string",
13
+ "pattern": "^[a-z0-9][a-z0-9-]*$"
14
+ },
15
+ "namespace": {
16
+ "type": "string",
17
+ "pattern": "^[a-z][a-z0-9-]*$"
18
+ },
19
+ "reference": {
20
+ "type": "string",
21
+ "minLength": 1,
22
+ "description": "RAW (decoded) value; will be encodeURIComponent()'d when stringified."
23
+ },
24
+ "selector": {
25
+ "type": "string",
26
+ "minLength": 1,
27
+ "description": "RAW (decoded) sub-id; will be encodeURIComponent()'d when stringified."
28
+ }
29
+ },
30
+ "required": ["version", "chain", "namespace", "reference"],
31
+ "examples": [
32
+ {
33
+ "version": "v1",
34
+ "chain": "eth",
35
+ "namespace": "erc20",
36
+ "reference": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
37
+ },
38
+ {
39
+ "version": "v1",
40
+ "chain": "near",
41
+ "namespace": "near-nft",
42
+ "reference": "apes.coolnft.near",
43
+ "selector": "series.1:blue/42"
44
+ },
45
+ {
46
+ "version": "v1",
47
+ "chain": "fiat",
48
+ "namespace": "iso4217",
49
+ "reference": "EUR"
50
+ }
51
+ ]
52
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://example.com/1cs_v1-string.schema.json",
4
+ "title": "1cs_v1 asset ID (string, URI-component encoding)",
5
+ "type": "string",
6
+ "$defs": {
7
+ "uriComponent": {
8
+ "type": "string",
9
+ "pattern": "^(?:[A-Za-z0-9\\-_.!~*'()]|%[0-9A-Fa-f]{2})+$"
10
+ },
11
+ "chain": {
12
+ "type": "string",
13
+ "pattern": "^[a-z0-9][a-z0-9-]*$"
14
+ },
15
+ "namespace": {
16
+ "type": "string",
17
+ "pattern": "^[a-z][a-z0-9-]*$"
18
+ }
19
+ },
20
+ "pattern": "^1cs_v1:[a-z0-9][a-z0-9-]*:[a-z][a-z0-9-]*:(?:[A-Za-z0-9\\-_.!~*'()]|%[0-9A-Fa-f]{2})+(?::(?:[A-Za-z0-9\\-_.!~*'()]|%[0-9A-Fa-f]{2})+)?$",
21
+ "examples": [
22
+ "1cs_v1:eth:erc20:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
23
+ "1cs_v1:eth-sepolia:erc721:0xAbcDEF1234567890abcdef1234567890ABCDef12:42",
24
+ "1cs_v1:near:near-nft:apes.coolnft.near:series.1%3Ablue%2F42",
25
+ "1cs_v1:aptos:aptos-coin:0x1%3A%3Aaptos_coin%3A%3AAptosCoin",
26
+ "1cs_v1:fiat:iso4217:EUR"
27
+ ]
28
+ }