@drift-labs/common 1.0.54 → 1.0.56

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.
@@ -0,0 +1,17 @@
1
+ import { BigNum } from '@drift-labs/sdk';
2
+ export declare const isNotionalDust: (val: BigNum) => boolean;
3
+ export declare const isValidPublicKey: (str: string) => boolean;
4
+ export declare const BITCOIN_CHAIN_ID = "8253038";
5
+ export declare const TRON_CHAIN_ID = "728126428";
6
+ export declare const SOLANA_CHAIN_ID = "1151111081099710";
7
+ /**
8
+ * Validates a recipient address based on the destination chain.
9
+ * EVM chains use 0x-prefixed hex address format.
10
+ * Bitcoin, Tron, and Solana each have their own format.
11
+ */
12
+ export declare function isValidAddressForChain(address: string, chainId: string | null): boolean;
13
+ /**
14
+ * Returns a human-readable address format hint for the given chain.
15
+ */
16
+ export declare function getAddressFormatHint(chainId: string | null): string;
17
+ export declare function isEvmChain(chainId: string): boolean;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEvmChain = exports.getAddressFormatHint = exports.isValidAddressForChain = exports.SOLANA_CHAIN_ID = exports.TRON_CHAIN_ID = exports.BITCOIN_CHAIN_ID = exports.isValidPublicKey = exports.isNotionalDust = void 0;
4
+ const sdk_1 = require("@drift-labs/sdk");
5
+ const isNotionalDust = (val) => {
6
+ return !val.eqZero() && val.abs().toNum() < 0.01;
7
+ };
8
+ exports.isNotionalDust = isNotionalDust;
9
+ const isValidPublicKey = (str) => {
10
+ try {
11
+ new sdk_1.PublicKey(str);
12
+ return true;
13
+ }
14
+ catch {
15
+ return false;
16
+ }
17
+ };
18
+ exports.isValidPublicKey = isValidPublicKey;
19
+ // Chain ID constants
20
+ exports.BITCOIN_CHAIN_ID = '8253038';
21
+ exports.TRON_CHAIN_ID = '728126428';
22
+ exports.SOLANA_CHAIN_ID = '1151111081099710';
23
+ // EVM chains (all others default to EVM validation)
24
+ const NON_EVM_CHAIN_IDS = new Set([
25
+ exports.BITCOIN_CHAIN_ID,
26
+ exports.TRON_CHAIN_ID,
27
+ exports.SOLANA_CHAIN_ID,
28
+ ]);
29
+ function isValidEvmAddress(address) {
30
+ return /^0x[0-9a-fA-F]{40}$/.test(address);
31
+ }
32
+ function isValidBitcoinAddress(address) {
33
+ // P2PKH (1...), P2SH (3...)
34
+ if (/^[13]/.test(address)) {
35
+ return /^(1[1-9A-HJ-NP-Za-km-z]{25,34}|3[1-9A-HJ-NP-Za-km-z]{25,34})$/.test(address);
36
+ }
37
+ // Bech32 — must be single-case (lowercase or uppercase, not mixed)
38
+ if (/^bc1/.test(address)) {
39
+ return /^bc1[0-9a-z]{25,62}$/.test(address);
40
+ }
41
+ if (/^BC1/.test(address)) {
42
+ return /^BC1[0-9A-Z]{25,62}$/.test(address);
43
+ }
44
+ return false;
45
+ }
46
+ function isValidTronAddress(address) {
47
+ // Tron addresses start with T and are 34 chars, base58
48
+ return /^T[1-9A-HJ-NP-Za-km-z]{33}$/.test(address);
49
+ }
50
+ function isValidSolanaAddress(address) {
51
+ return (0, exports.isValidPublicKey)(address);
52
+ }
53
+ /**
54
+ * Validates a recipient address based on the destination chain.
55
+ * EVM chains use 0x-prefixed hex address format.
56
+ * Bitcoin, Tron, and Solana each have their own format.
57
+ */
58
+ function isValidAddressForChain(address, chainId) {
59
+ if (!address || !chainId)
60
+ return false;
61
+ if (chainId === exports.BITCOIN_CHAIN_ID)
62
+ return isValidBitcoinAddress(address);
63
+ if (chainId === exports.TRON_CHAIN_ID)
64
+ return isValidTronAddress(address);
65
+ if (chainId === exports.SOLANA_CHAIN_ID)
66
+ return isValidSolanaAddress(address);
67
+ return isValidEvmAddress(address);
68
+ }
69
+ exports.isValidAddressForChain = isValidAddressForChain;
70
+ /**
71
+ * Returns a human-readable address format hint for the given chain.
72
+ */
73
+ function getAddressFormatHint(chainId) {
74
+ if (!chainId)
75
+ return 'address';
76
+ if (chainId === exports.BITCOIN_CHAIN_ID)
77
+ return 'Bitcoin address (1.., 3.., bc1..)';
78
+ if (chainId === exports.TRON_CHAIN_ID)
79
+ return 'Tron address (T..)';
80
+ if (chainId === exports.SOLANA_CHAIN_ID)
81
+ return 'Solana address';
82
+ return 'EVM address (0x..)';
83
+ }
84
+ exports.getAddressFormatHint = getAddressFormatHint;
85
+ function isEvmChain(chainId) {
86
+ return !NON_EVM_CHAIN_IDS.has(chainId);
87
+ }
88
+ exports.isEvmChain = isEvmChain;
89
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":";;;AAAA,yCAAoD;AAE7C,MAAM,cAAc,GAAG,CAAC,GAAW,EAAE,EAAE;IAC7C,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC;AAClD,CAAC,CAAC;AAFW,QAAA,cAAc,kBAEzB;AAEK,MAAM,gBAAgB,GAAG,CAAC,GAAW,EAAW,EAAE;IACxD,IAAI,CAAC;QACJ,IAAI,eAAS,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,IAAI,CAAC;IACb,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,KAAK,CAAC;IACd,CAAC;AACF,CAAC,CAAC;AAPW,QAAA,gBAAgB,oBAO3B;AAEF,qBAAqB;AACR,QAAA,gBAAgB,GAAG,SAAS,CAAC;AAC7B,QAAA,aAAa,GAAG,WAAW,CAAC;AAC5B,QAAA,eAAe,GAAG,kBAAkB,CAAC;AAElD,oDAAoD;AACpD,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IACjC,wBAAgB;IAChB,qBAAa;IACb,uBAAe;CACf,CAAC,CAAC;AAEH,SAAS,iBAAiB,CAAC,OAAe;IACzC,OAAO,qBAAqB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,qBAAqB,CA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AE,OAAO,mCAAmC,CAAC;IAC7E,IAAI,OAAO,KAAK,qBAAa;QAAE,OAAO,oBAAoB,CAAC;IAC3D,IAAI,OAAO,KAAK,uBAAe;QAAE,OAAO,gBAAgB,CAAC;IACzD,OAAO,oBAAoB,CAAC;AAC7B,CAAC;AAND,oDAMC;AAED,SAAgB,UAAU,CAAC,OAAe;IACzC,OAAO,CAAC,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC;AAFD,gCAEC","sourcesContent":["import { BigNum, PublicKey } from '@drift-labs/sdk';\n\nexport const isNotionalDust = (val: BigNum) => {\n\treturn !val.eqZero() && val.abs().toNum() < 0.01;\n};\n\nexport const isValidPublicKey = (str: string): boolean => {\n\ttry {\n\t\tnew PublicKey(str);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\n// Chain ID constants\nexport const BITCOIN_CHAIN_ID = '8253038';\nexport const TRON_CHAIN_ID = '728126428';\nexport const SOLANA_CHAIN_ID = '1151111081099710';\n\n// EVM chains (all others default to EVM validation)\nconst NON_EVM_CHAIN_IDS = new Set([\n\tBITCOIN_CHAIN_ID,\n\tTRON_CHAIN_ID,\n\tSOLANA_CHAIN_ID,\n]);\n\nfunction isValidEvmAddress(address: string): boolean {\n\treturn /^0x[0-9a-fA-F]{40}$/.test(address);\n}\n\nfunction isValidBitcoinAddress(address: string): boolean {\n\t// P2PKH (1...), P2SH (3...)\n\tif (/^[13]/.test(address)) {\n\t\treturn /^(1[1-9A-HJ-NP-Za-km-z]{25,34}|3[1-9A-HJ-NP-Za-km-z]{25,34})$/.test(\n\t\t\taddress\n\t\t);\n\t}\n\t// Bech32 — must be single-case (lowercase or uppercase, not mixed)\n\tif (/^bc1/.test(address)) {\n\t\treturn /^bc1[0-9a-z]{25,62}$/.test(address);\n\t}\n\tif (/^BC1/.test(address)) {\n\t\treturn /^BC1[0-9A-Z]{25,62}$/.test(address);\n\t}\n\treturn false;\n}\n\nfunction isValidTronAddress(address: string): boolean {\n\t// Tron addresses start with T and are 34 chars, base58\n\treturn /^T[1-9A-HJ-NP-Za-km-z]{33}$/.test(address);\n}\n\nfunction isValidSolanaAddress(address: string): boolean {\n\treturn isValidPublicKey(address);\n}\n\n/**\n * Validates a recipient address based on the destination chain.\n * EVM chains use 0x-prefixed hex address format.\n * Bitcoin, Tron, and Solana each have their own format.\n */\nexport function isValidAddressForChain(\n\taddress: string,\n\tchainId: string | null\n): boolean {\n\tif (!address || !chainId) return false;\n\n\tif (chainId === BITCOIN_CHAIN_ID) return isValidBitcoinAddress(address);\n\tif (chainId === TRON_CHAIN_ID) return isValidTronAddress(address);\n\tif (chainId === SOLANA_CHAIN_ID) return isValidSolanaAddress(address);\n\n\treturn isValidEvmAddress(address);\n}\n\n/**\n * Returns a human-readable address format hint for the given chain.\n */\nexport function getAddressFormatHint(chainId: string | null): string {\n\tif (!chainId) return 'address';\n\tif (chainId === BITCOIN_CHAIN_ID) return 'Bitcoin address (1.., 3.., bc1..)';\n\tif (chainId === TRON_CHAIN_ID) return 'Tron address (T..)';\n\tif (chainId === SOLANA_CHAIN_ID) return 'Solana address';\n\treturn 'EVM address (0x..)';\n}\n\nexport function isEvmChain(chainId: string): boolean {\n\treturn !NON_EVM_CHAIN_IDS.has(chainId);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/common",
3
- "version": "1.0.54",
3
+ "version": "1.0.56",
4
4
  "description": "Common functions for Drift",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",