@rabby-wallet/hyperliquid-sdk 1.0.0-beta.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.
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.prepareMasterSignData = exports.signL1AgentAction = exports.signTypedDataV4 = exports.splitSig = exports.EIP712_DOMAIN_TYPES = void 0;
37
+ const ethUtil = __importStar(require("@ethereumjs/util"));
38
+ const sigUtil = __importStar(require("@metamask/eth-sig-util"));
39
+ const keccak_1 = require("ethereum-cryptography/keccak");
40
+ const msgpack_1 = require("@msgpack/msgpack");
41
+ const constants_1 = require("../types/constants");
42
+ exports.EIP712_DOMAIN_TYPES = [
43
+ { name: 'name', type: 'string' },
44
+ { name: 'version', type: 'string' },
45
+ { name: 'chainId', type: 'uint256' },
46
+ { name: 'verifyingContract', type: 'address' },
47
+ ];
48
+ const normalizePrivateKeyHex = (privateKey) => {
49
+ const hex = privateKey.startsWith('0x') ? privateKey : ethUtil.addHexPrefix(privateKey);
50
+ return ethUtil.stripHexPrefix(hex);
51
+ };
52
+ const splitSig = (sig) => {
53
+ const pureSig = sig.replace('0x', '');
54
+ const r = `0x${pureSig.substring(0, 64)}`;
55
+ const s = `0x${pureSig.substring(64, 128)}`;
56
+ let v = parseInt(pureSig.substring(128, 130), 16);
57
+ if (v < 27) {
58
+ v += 27;
59
+ }
60
+ return { r, s, v };
61
+ };
62
+ exports.splitSig = splitSig;
63
+ const signTypedDataV4 = ({ privateKey, domain, primaryType, payloadTypes, message, }) => {
64
+ const types = {
65
+ EIP712Domain: exports.EIP712_DOMAIN_TYPES,
66
+ [primaryType]: payloadTypes,
67
+ };
68
+ const typedData = {
69
+ domain,
70
+ types,
71
+ primaryType,
72
+ message,
73
+ };
74
+ const privKeyHex = normalizePrivateKeyHex(privateKey);
75
+ const signature = sigUtil.signTypedData({
76
+ privateKey: Buffer.from(privKeyHex, 'hex'),
77
+ data: typedData,
78
+ version: sigUtil.SignTypedDataVersion.V4,
79
+ });
80
+ return (0, exports.splitSig)(signature);
81
+ };
82
+ exports.signTypedDataV4 = signTypedDataV4;
83
+ const generateActionHash = (action, nonce) => {
84
+ const msgPackBytes = (0, msgpack_1.encode)(action);
85
+ const additionalBytesLength = 9;
86
+ const data = new Uint8Array(msgPackBytes.length + additionalBytesLength);
87
+ data.set(msgPackBytes);
88
+ const view = new DataView(data.buffer);
89
+ view.setBigUint64(msgPackBytes.length, BigInt(nonce), false);
90
+ view.setUint8(msgPackBytes.length + 8, 0);
91
+ const hash = (0, keccak_1.keccak256)(data);
92
+ return ethUtil.addHexPrefix(ethUtil.bytesToHex(hash));
93
+ };
94
+ const signL1AgentAction = (privateKey, action, isTestnet, nonce) => {
95
+ const domain = {
96
+ name: 'Exchange',
97
+ version: '1',
98
+ chainId: constants_1.CHAIN_IDS.HYPERLIQUID_MAINNET,
99
+ verifyingContract: '0x0000000000000000000000000000000000000000',
100
+ };
101
+ const payloadTypes = [
102
+ { name: 'source', type: 'string' },
103
+ { name: 'connectionId', type: 'bytes32' },
104
+ ];
105
+ const message = {
106
+ source: !isTestnet ? 'a' : 'b',
107
+ connectionId: generateActionHash(action, nonce),
108
+ };
109
+ return (0, exports.signTypedDataV4)({ privateKey, domain, primaryType: 'Agent', payloadTypes, message });
110
+ };
111
+ exports.signL1AgentAction = signL1AgentAction;
112
+ /**
113
+ * Prepare some action sign must by master wallet so we need to prepare the typed data
114
+ * Returns typed data that needs to be signed by the main wallet
115
+ */
116
+ const prepareMasterSignData = ({ action, payloadTypes, primaryType, isMainnet, }) => {
117
+ const domain = {
118
+ name: 'HyperliquidSignTransaction',
119
+ version: '1',
120
+ chainId: isMainnet ? constants_1.CHAIN_IDS.ARBITRUM_MAINNET : constants_1.CHAIN_IDS.ARBITRUM_TESTNET,
121
+ verifyingContract: '0x0000000000000000000000000000000000000000',
122
+ };
123
+ const types = {
124
+ EIP712Domain: exports.EIP712_DOMAIN_TYPES,
125
+ [primaryType]: payloadTypes,
126
+ };
127
+ return {
128
+ domain,
129
+ types,
130
+ primaryType,
131
+ message: action,
132
+ };
133
+ };
134
+ exports.prepareMasterSignData = prepareMasterSignData;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@rabby-wallet/hyperliquid-sdk",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "build": "rimraf dist && tsc",
15
+ "preversion": "npm run build",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "author": "",
19
+ "license": "UNLICENSED",
20
+ "dependencies": {
21
+ "@ethereumjs/util": "^9.0.0",
22
+ "@metamask/eth-sig-util": "5.1.0",
23
+ "@msgpack/msgpack": "^3.1.2",
24
+ "ethereum-cryptography": "^2.1.0",
25
+ "ws": "^8.18.2"
26
+ },
27
+ "devDependencies": {
28
+ "@types/ws": "^8.5.10",
29
+ "prettier": "^2.7.1",
30
+ "rimraf": "^6.0.0",
31
+ "typescript": "^5.0.0"
32
+ },
33
+ "engines": {
34
+ "node": ">=16.0.0"
35
+ }
36
+ }