@paradex/sdk 0.8.1 → 0.10.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 +211 -2
- package/dist/account.d.ts +79 -8
- package/dist/account.js +298 -12
- package/dist/client.d.ts +12 -4
- package/dist/client.js +11 -3
- package/dist/config.d.ts +8 -2
- package/dist/config.js +5 -3
- package/dist/determinism-guard.d.ts +102 -0
- package/dist/determinism-guard.js +62 -0
- package/dist/eip712.d.ts +40 -0
- package/dist/eip712.js +162 -0
- package/dist/ethereum-signer.d.ts +13 -2
- package/dist/ethereum-signer.js +14 -1
- package/dist/index.d.ts +61 -0
- package/dist/index.js +51 -0
- package/dist/key-derivation.d.ts +19 -0
- package/dist/key-derivation.js +83 -0
- package/dist/session-accounts.d.ts +206 -0
- package/dist/session-accounts.js +747 -0
- package/dist/session-auth.d.ts +31 -0
- package/dist/session-auth.js +115 -0
- package/dist/session-connector.d.ts +132 -0
- package/dist/session-connector.js +304 -0
- package/dist/session-key.d.ts +68 -0
- package/dist/session-key.js +187 -0
- package/dist/session-owner-signer.d.ts +68 -0
- package/dist/session-owner-signer.js +64 -0
- package/dist/session-transport.d.ts +48 -0
- package/dist/session-transport.js +56 -0
- package/dist/session-typed-data.d.ts +400 -0
- package/dist/session-typed-data.js +1035 -0
- package/dist/starknet-signer.d.ts +3 -4
- package/dist/starknet-signer.js +5 -10
- package/package.json +6 -6
package/dist/eip712.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Hex } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Minimal EIP-712 encoder covering the types the session typed data uses:
|
|
4
|
+
* `string`, `bool`, `uintN`, `address`, `bytes32`, structs, and arrays of any
|
|
5
|
+
* of these. It exists so hashing does not depend on a wallet library.
|
|
6
|
+
*/
|
|
7
|
+
export interface Eip712TypeField {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly type: string;
|
|
10
|
+
}
|
|
11
|
+
export type Eip712Types = Record<string, readonly Eip712TypeField[]>;
|
|
12
|
+
export interface Eip712Domain {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly version: string;
|
|
15
|
+
readonly chainId: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Typed data in the shape `ethers` and most signers take: `types` excludes
|
|
19
|
+
* `EIP712Domain`, which is implied by the domain fields.
|
|
20
|
+
*/
|
|
21
|
+
export interface Eip712TypedData<M extends Record<string, unknown> = Record<string, unknown>> {
|
|
22
|
+
readonly domain: Eip712Domain;
|
|
23
|
+
readonly primaryType: string;
|
|
24
|
+
readonly types: Eip712Types;
|
|
25
|
+
readonly message: M;
|
|
26
|
+
}
|
|
27
|
+
export declare const EIP712_DOMAIN_TYPE: readonly Eip712TypeField[];
|
|
28
|
+
export declare function encodeType(primaryType: string, types: Eip712Types): string;
|
|
29
|
+
export declare function typeHash(primaryType: string, types: Eip712Types): Uint8Array;
|
|
30
|
+
export declare function hashStruct(primaryType: string, data: Record<string, unknown>, types: Eip712Types): Uint8Array;
|
|
31
|
+
export declare function hashDomain(domain: Eip712Domain): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* The EIP-712 digest: `keccak256(0x1901 ‖ domainSeparator ‖ hashStruct(message))`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function hashTypedData(typedData: Eip712TypedData): Hex;
|
|
36
|
+
/**
|
|
37
|
+
* Serializes typed data for `eth_signTypedData_v4`, which needs the
|
|
38
|
+
* `EIP712Domain` type listed explicitly.
|
|
39
|
+
*/
|
|
40
|
+
export declare function toEthSignTypedDataV4(typedData: Eip712TypedData): string;
|
package/dist/eip712.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
|
|
2
|
+
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils';
|
|
3
|
+
export const EIP712_DOMAIN_TYPE = [
|
|
4
|
+
{ name: 'name', type: 'string' },
|
|
5
|
+
{ name: 'version', type: 'string' },
|
|
6
|
+
{ name: 'chainId', type: 'uint256' },
|
|
7
|
+
];
|
|
8
|
+
const textEncoder = new TextEncoder();
|
|
9
|
+
function keccak(data) {
|
|
10
|
+
return keccak256(data);
|
|
11
|
+
}
|
|
12
|
+
function findDependencies(primaryType, types, found = new Set()) {
|
|
13
|
+
if (found.has(primaryType) || types[primaryType] == null)
|
|
14
|
+
return found;
|
|
15
|
+
found.add(primaryType);
|
|
16
|
+
for (const field of types[primaryType] ?? []) {
|
|
17
|
+
findDependencies(baseType(field.type), types, found);
|
|
18
|
+
}
|
|
19
|
+
return found;
|
|
20
|
+
}
|
|
21
|
+
function baseType(type) {
|
|
22
|
+
const bracket = type.indexOf('[');
|
|
23
|
+
return bracket === -1 ? type : type.slice(0, bracket);
|
|
24
|
+
}
|
|
25
|
+
export function encodeType(primaryType, types) {
|
|
26
|
+
const deps = findDependencies(primaryType, types);
|
|
27
|
+
deps.delete(primaryType);
|
|
28
|
+
const ordered = [primaryType, ...[...deps].sort()];
|
|
29
|
+
return ordered
|
|
30
|
+
.map((name) => {
|
|
31
|
+
const fields = types[name];
|
|
32
|
+
if (fields == null)
|
|
33
|
+
throw new Error(`EIP-712: unknown type ${name}`);
|
|
34
|
+
return `${name}(${fields.map((f) => `${f.type} ${f.name}`).join(',')})`;
|
|
35
|
+
})
|
|
36
|
+
.join('');
|
|
37
|
+
}
|
|
38
|
+
export function typeHash(primaryType, types) {
|
|
39
|
+
return keccak(textEncoder.encode(encodeType(primaryType, types)));
|
|
40
|
+
}
|
|
41
|
+
function uintToWord(value, bits) {
|
|
42
|
+
if (value < 0n || value >= 1n << BigInt(bits)) {
|
|
43
|
+
throw new Error(`EIP-712: value out of range for uint${bits}`);
|
|
44
|
+
}
|
|
45
|
+
return hexToBytes(value.toString(16).padStart(64, '0'));
|
|
46
|
+
}
|
|
47
|
+
function toBigInt(value, type) {
|
|
48
|
+
if (typeof value === 'bigint')
|
|
49
|
+
return value;
|
|
50
|
+
if (typeof value === 'number') {
|
|
51
|
+
if (!Number.isSafeInteger(value)) {
|
|
52
|
+
throw new Error(`EIP-712: ${type} must be a safe integer`);
|
|
53
|
+
}
|
|
54
|
+
return BigInt(value);
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === 'string' && /^(0x[0-9a-fA-F]+|[0-9]+)$/.test(value)) {
|
|
57
|
+
return BigInt(value);
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`EIP-712: invalid ${type} value`);
|
|
60
|
+
}
|
|
61
|
+
function encodeValue(type, value, types) {
|
|
62
|
+
if (types[type] != null) {
|
|
63
|
+
if (value == null || typeof value !== 'object') {
|
|
64
|
+
throw new Error(`EIP-712: ${type} must be an object`);
|
|
65
|
+
}
|
|
66
|
+
return hashStruct(type, value, types);
|
|
67
|
+
}
|
|
68
|
+
if (type.endsWith(']')) {
|
|
69
|
+
if (!Array.isArray(value)) {
|
|
70
|
+
throw new Error(`EIP-712: ${type} must be an array`);
|
|
71
|
+
}
|
|
72
|
+
const open = type.lastIndexOf('[');
|
|
73
|
+
const itemType = type.slice(0, open);
|
|
74
|
+
const fixedLength = type.slice(open + 1, -1);
|
|
75
|
+
if (fixedLength !== '' && Number(fixedLength) !== value.length) {
|
|
76
|
+
throw new Error(`EIP-712: ${type} has the wrong length`);
|
|
77
|
+
}
|
|
78
|
+
const items = value.map((item) => encodeValue(itemType, item, types));
|
|
79
|
+
return keccak(concatBytes(...items));
|
|
80
|
+
}
|
|
81
|
+
if (type === 'string') {
|
|
82
|
+
if (typeof value !== 'string') {
|
|
83
|
+
throw new Error('EIP-712: string must be a string');
|
|
84
|
+
}
|
|
85
|
+
return keccak(textEncoder.encode(value));
|
|
86
|
+
}
|
|
87
|
+
if (type === 'bytes') {
|
|
88
|
+
if (typeof value !== 'string' || !/^0x([0-9a-fA-F]{2})*$/.test(value)) {
|
|
89
|
+
throw new Error('EIP-712: bytes must be 0x-prefixed hex');
|
|
90
|
+
}
|
|
91
|
+
return keccak(hexToBytes(value.slice(2)));
|
|
92
|
+
}
|
|
93
|
+
if (type === 'bool') {
|
|
94
|
+
if (typeof value !== 'boolean') {
|
|
95
|
+
throw new Error('EIP-712: bool must be a boolean');
|
|
96
|
+
}
|
|
97
|
+
return uintToWord(value ? 1n : 0n, 256);
|
|
98
|
+
}
|
|
99
|
+
if (type === 'address') {
|
|
100
|
+
if (typeof value !== 'string' || !/^0x[0-9a-fA-F]{40}$/.test(value)) {
|
|
101
|
+
throw new Error('EIP-712: invalid address');
|
|
102
|
+
}
|
|
103
|
+
return uintToWord(BigInt(value), 160);
|
|
104
|
+
}
|
|
105
|
+
if (type === 'bytes32') {
|
|
106
|
+
if (typeof value !== 'string' || !/^0x[0-9a-fA-F]{64}$/.test(value)) {
|
|
107
|
+
throw new Error('EIP-712: invalid bytes32');
|
|
108
|
+
}
|
|
109
|
+
return hexToBytes(value.slice(2));
|
|
110
|
+
}
|
|
111
|
+
const uintMatch = /^uint(\d+)$/.exec(type);
|
|
112
|
+
if (uintMatch?.[1] != null) {
|
|
113
|
+
const bits = Number(uintMatch[1]);
|
|
114
|
+
if (bits < 8 || bits > 256 || bits % 8 !== 0) {
|
|
115
|
+
throw new Error(`EIP-712: unsupported type ${type}`);
|
|
116
|
+
}
|
|
117
|
+
return uintToWord(toBigInt(value, type), bits);
|
|
118
|
+
}
|
|
119
|
+
throw new Error(`EIP-712: unsupported type ${type}`);
|
|
120
|
+
}
|
|
121
|
+
export function hashStruct(primaryType, data, types) {
|
|
122
|
+
const fields = types[primaryType];
|
|
123
|
+
if (fields == null)
|
|
124
|
+
throw new Error(`EIP-712: unknown type ${primaryType}`);
|
|
125
|
+
const known = new Set(fields.map((f) => f.name));
|
|
126
|
+
for (const key of Object.keys(data)) {
|
|
127
|
+
if (!known.has(key)) {
|
|
128
|
+
throw new Error(`EIP-712: unexpected field ${primaryType}.${key}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const encoded = fields.map((field) => {
|
|
132
|
+
if (!(field.name in data)) {
|
|
133
|
+
throw new Error(`EIP-712: missing field ${primaryType}.${field.name}`);
|
|
134
|
+
}
|
|
135
|
+
return encodeValue(field.type, data[field.name], types);
|
|
136
|
+
});
|
|
137
|
+
return keccak(concatBytes(typeHash(primaryType, types), ...encoded));
|
|
138
|
+
}
|
|
139
|
+
export function hashDomain(domain) {
|
|
140
|
+
return hashStruct('EIP712Domain', { ...domain }, {
|
|
141
|
+
EIP712Domain: EIP712_DOMAIN_TYPE,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The EIP-712 digest: `keccak256(0x1901 ‖ domainSeparator ‖ hashStruct(message))`.
|
|
146
|
+
*/
|
|
147
|
+
export function hashTypedData(typedData) {
|
|
148
|
+
const digest = keccak(concatBytes(new Uint8Array([0x19, 0x01]), hashDomain(typedData.domain), hashStruct(typedData.primaryType, typedData.message, typedData.types)));
|
|
149
|
+
return `0x${bytesToHex(digest)}`;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Serializes typed data for `eth_signTypedData_v4`, which needs the
|
|
153
|
+
* `EIP712Domain` type listed explicitly.
|
|
154
|
+
*/
|
|
155
|
+
export function toEthSignTypedDataV4(typedData) {
|
|
156
|
+
return JSON.stringify({
|
|
157
|
+
domain: typedData.domain,
|
|
158
|
+
primaryType: typedData.primaryType,
|
|
159
|
+
types: { EIP712Domain: EIP712_DOMAIN_TYPE, ...typedData.types },
|
|
160
|
+
message: typedData.message,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
@@ -3,10 +3,10 @@ export interface TypedData {
|
|
|
3
3
|
readonly domain: {
|
|
4
4
|
readonly name: string;
|
|
5
5
|
readonly version: string;
|
|
6
|
-
readonly chainId: string;
|
|
6
|
+
readonly chainId: string | number;
|
|
7
7
|
};
|
|
8
8
|
readonly primaryType: string;
|
|
9
|
-
readonly types: Record<string,
|
|
9
|
+
readonly types: Record<string, ReadonlyArray<{
|
|
10
10
|
readonly name: string;
|
|
11
11
|
readonly type: string;
|
|
12
12
|
}>>;
|
|
@@ -14,6 +14,17 @@ export interface TypedData {
|
|
|
14
14
|
}
|
|
15
15
|
export interface EthereumSigner {
|
|
16
16
|
readonly signTypedData: (typedData: TypedData) => Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* The signing wallet's address. Optional; the determinism guard needs it
|
|
19
|
+
* unless it is passed as `walletAddress`.
|
|
20
|
+
*/
|
|
21
|
+
readonly getAddress?: () => Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Reads an address's bytecode on the signer's chain. Optional; the
|
|
24
|
+
* determinism guard uses it to refuse contract wallets when no
|
|
25
|
+
* `codeProviders` are passed.
|
|
26
|
+
*/
|
|
27
|
+
readonly getCode?: (address: string) => Promise<string>;
|
|
17
28
|
}
|
|
18
29
|
export declare function ethersSignerAdapter(ethersSigner: ethers.Signer): EthereumSigner;
|
|
19
30
|
/**
|
package/dist/ethereum-signer.js
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
export function ethersSignerAdapter(ethersSigner) {
|
|
2
|
+
const provider = ethersSigner.provider;
|
|
2
3
|
return {
|
|
4
|
+
async getAddress() {
|
|
5
|
+
return await ethersSigner.getAddress();
|
|
6
|
+
},
|
|
7
|
+
...(provider != null
|
|
8
|
+
? {
|
|
9
|
+
async getCode(address) {
|
|
10
|
+
return await provider.getCode(address);
|
|
11
|
+
},
|
|
12
|
+
}
|
|
13
|
+
: {}),
|
|
3
14
|
async signTypedData(typedData) {
|
|
4
|
-
return await ethersSigner.signTypedData(typedData.domain,
|
|
15
|
+
return await ethersSigner.signTypedData(typedData.domain,
|
|
16
|
+
// ethers takes mutable arrays but does not modify them.
|
|
17
|
+
typedData.types, typedData.message);
|
|
5
18
|
},
|
|
6
19
|
};
|
|
7
20
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import * as _Account from './account.js';
|
|
1
2
|
import * as _Client from './client.js';
|
|
2
3
|
import * as _Config from './config.js';
|
|
3
4
|
import * as _Signer from './ethereum-signer.js';
|
|
5
|
+
import * as _SessionKey from './session-key.js';
|
|
6
|
+
import * as _SessionOwnerSigner from './session-owner-signer.js';
|
|
7
|
+
import * as _SessionTypedData from './session-typed-data.js';
|
|
4
8
|
export declare const Client: {
|
|
5
9
|
fromEthSigner: (params: _Client.CreateClientFromEthSignerParams) => Promise<_Client.ParadexClient>;
|
|
6
10
|
fromStarknetAccount: (params: _Client.CreateClientFromStarknetAccountParams) => Promise<_Client.ParadexClient>;
|
|
@@ -8,10 +12,67 @@ export declare const Client: {
|
|
|
8
12
|
export declare const Config: {
|
|
9
13
|
fetch: typeof _Config.fetchConfig;
|
|
10
14
|
};
|
|
15
|
+
export declare const Onboarding: {
|
|
16
|
+
fetch: typeof _Account.fetchOnboarding;
|
|
17
|
+
};
|
|
11
18
|
export declare const Signer: {
|
|
12
19
|
fromEthers: typeof _Signer.ethersSignerAdapter;
|
|
13
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Session grant typed data and hashing.
|
|
23
|
+
* @experimental The server side of sessions is not released yet.
|
|
24
|
+
*/
|
|
25
|
+
export declare const SessionTypedData: {
|
|
26
|
+
createGrant: typeof _SessionTypedData.createSessionGrant;
|
|
27
|
+
validateGrant: typeof _SessionTypedData.validateSessionGrant;
|
|
28
|
+
validatePasskeyGrant: typeof _SessionTypedData.validatePasskeyGrant;
|
|
29
|
+
validateMint: typeof _SessionTypedData.validateSessionMint;
|
|
30
|
+
buildGrantEip712: typeof _SessionTypedData.buildSessionGrantEip712;
|
|
31
|
+
buildGrantSnip12: typeof _SessionTypedData.buildSessionGrantSnip12;
|
|
32
|
+
buildPasskeyGrantEip712: typeof _SessionTypedData.buildPasskeyGrantEip712;
|
|
33
|
+
buildPasskeyGrantSnip12: typeof _SessionTypedData.buildPasskeyGrantSnip12;
|
|
34
|
+
buildMintEip712: typeof _SessionTypedData.buildSessionMintEip712;
|
|
35
|
+
buildMintSnip12: typeof _SessionTypedData.buildSessionMintSnip12;
|
|
36
|
+
hashEip712: typeof _SessionTypedData.hashEip712;
|
|
37
|
+
hashSnip12: typeof _SessionTypedData.hashSnip12;
|
|
38
|
+
generateNonce: typeof _SessionTypedData.generateSessionNonce;
|
|
39
|
+
mintChallenge: typeof _SessionTypedData.sessionMintChallenge;
|
|
40
|
+
grantAccounts: typeof _SessionTypedData.grantAccounts;
|
|
41
|
+
assertGrantActive: typeof _SessionTypedData.assertGrantActive;
|
|
42
|
+
assertMintWithinCeiling: typeof _SessionTypedData.assertMintWithinCeiling;
|
|
43
|
+
summarizeGrant: typeof _SessionTypedData.summarizeGrant;
|
|
44
|
+
lossSummary: typeof _SessionTypedData.lossSummary;
|
|
45
|
+
marketCovers: typeof _SessionTypedData.marketCovers;
|
|
46
|
+
maxSessionTtl: typeof _SessionTypedData.maxSessionTtl;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Session connector: open a session with one owner signature, then trade
|
|
50
|
+
* every account the grant lists with the session key.
|
|
51
|
+
* @experimental The shapes of `/v2/sessions` may change.
|
|
52
|
+
*/
|
|
53
|
+
export declare const Session: {
|
|
54
|
+
ownerSignerFromEip1193: typeof _SessionOwnerSigner.eip1193OwnerSigner;
|
|
55
|
+
ownerSignerFromExternal: typeof _SessionOwnerSigner.externalOwnerSigner;
|
|
56
|
+
memoryStorage: typeof _SessionKey.memorySessionStorage;
|
|
57
|
+
indexedDbStorage: typeof _SessionKey.indexedDbSessionStorage;
|
|
58
|
+
};
|
|
59
|
+
export { ALL_LIMITS, CEILING_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, LONGEST_SESSION, MAX_GRANT_SUBACCOUNTS, MINT_HASH_ACCOUNT, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
60
|
+
export { SessionConnector } from './session-connector.js';
|
|
61
|
+
export { DeterminismGuardError, isContractCode } from './determinism-guard.js';
|
|
62
|
+
export { SessionAccounts } from './session-accounts.js';
|
|
63
|
+
export { SessionApiError } from './session-transport.js';
|
|
64
|
+
export { SessionKey } from './session-key.js';
|
|
65
|
+
export type { AddressDerivationOptions, OnboardingInfo, OnboardingDerivationInfo, } from './account.js';
|
|
14
66
|
export type { ParadexClient, CreateClientFromEthSignerParams, CreateClientFromStarknetAccountParams, TokenBalance, SocializedLossFactor, ReceivableAmount, MaxWithdraw, WithdrawResult, } from './client.js';
|
|
15
67
|
export type { ParadexConfig } from './config.js';
|
|
16
68
|
export type { EthereumSigner } from './ethereum-signer.js';
|
|
69
|
+
export type { DeterminismGuardErrorCode, DeterminismGuardOptions, DeterminismGuardResult, EthCodeProvider, } from './determinism-guard.js';
|
|
17
70
|
export type { Hex } from './types.js';
|
|
71
|
+
export type { Eip712TypedData, Eip712Domain } from './eip712.js';
|
|
72
|
+
export type { AssertMintOptions, Builder, Ceiling, CreateSessionGrantParams, LimitCaps, LimitName, LimitOptions, Limits, Passkey, PasskeyGrant, SessionEnvironment, SessionGrant, SessionGrantErrorCode, SessionMint, SessionScope, SessionType, ValidatePasskeyGrantOptions, } from './session-typed-data.js';
|
|
73
|
+
export type { EvmSubaccount, OpenSessionParams, Session as RegisteredSession, SessionConnectorParams, SessionOnboardingParams, SessionUtm, } from './session-connector.js';
|
|
74
|
+
export type { GrantState, GrantStatus, RevokeOptions, SessionAccountsParams, SessionRequest, SubscriptionHandler, } from './session-accounts.js';
|
|
75
|
+
export type { OrderSide, OrderType, SessionOrder } from './session-auth.js';
|
|
76
|
+
export type { SessionStorage, StoredSession } from './session-key.js';
|
|
77
|
+
export type { Eip1193Provider, OwnerSignRequest, SessionOwnerSigner, } from './session-owner-signer.js';
|
|
78
|
+
export type { HttpRequest, HttpResponse, HttpTransport, KnownSessionApiErrorCode, SessionApiErrorCode, WebSocketFactory, WebSocketLike, } from './session-transport.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import * as _Account from './account.js';
|
|
1
2
|
import * as _Client from './client.js';
|
|
2
3
|
import * as _Config from './config.js';
|
|
3
4
|
import * as _Signer from './ethereum-signer.js';
|
|
5
|
+
import * as _SessionKey from './session-key.js';
|
|
6
|
+
import * as _SessionOwnerSigner from './session-owner-signer.js';
|
|
7
|
+
import * as _SessionTypedData from './session-typed-data.js';
|
|
4
8
|
// Recommended: High-level client interface
|
|
5
9
|
export const Client = {
|
|
6
10
|
fromEthSigner: async (params) => await _Client.ParadexClient.createFromEthSigner(params),
|
|
@@ -9,6 +13,53 @@ export const Client = {
|
|
|
9
13
|
export const Config = {
|
|
10
14
|
fetch: _Config.fetchConfig,
|
|
11
15
|
};
|
|
16
|
+
export const Onboarding = {
|
|
17
|
+
fetch: _Account.fetchOnboarding,
|
|
18
|
+
};
|
|
12
19
|
export const Signer = {
|
|
13
20
|
fromEthers: _Signer.ethersSignerAdapter,
|
|
14
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Session grant typed data and hashing.
|
|
24
|
+
* @experimental The server side of sessions is not released yet.
|
|
25
|
+
*/
|
|
26
|
+
export const SessionTypedData = {
|
|
27
|
+
createGrant: _SessionTypedData.createSessionGrant,
|
|
28
|
+
validateGrant: _SessionTypedData.validateSessionGrant,
|
|
29
|
+
validatePasskeyGrant: _SessionTypedData.validatePasskeyGrant,
|
|
30
|
+
validateMint: _SessionTypedData.validateSessionMint,
|
|
31
|
+
buildGrantEip712: _SessionTypedData.buildSessionGrantEip712,
|
|
32
|
+
buildGrantSnip12: _SessionTypedData.buildSessionGrantSnip12,
|
|
33
|
+
buildPasskeyGrantEip712: _SessionTypedData.buildPasskeyGrantEip712,
|
|
34
|
+
buildPasskeyGrantSnip12: _SessionTypedData.buildPasskeyGrantSnip12,
|
|
35
|
+
buildMintEip712: _SessionTypedData.buildSessionMintEip712,
|
|
36
|
+
buildMintSnip12: _SessionTypedData.buildSessionMintSnip12,
|
|
37
|
+
hashEip712: _SessionTypedData.hashEip712,
|
|
38
|
+
hashSnip12: _SessionTypedData.hashSnip12,
|
|
39
|
+
generateNonce: _SessionTypedData.generateSessionNonce,
|
|
40
|
+
mintChallenge: _SessionTypedData.sessionMintChallenge,
|
|
41
|
+
grantAccounts: _SessionTypedData.grantAccounts,
|
|
42
|
+
assertGrantActive: _SessionTypedData.assertGrantActive,
|
|
43
|
+
assertMintWithinCeiling: _SessionTypedData.assertMintWithinCeiling,
|
|
44
|
+
summarizeGrant: _SessionTypedData.summarizeGrant,
|
|
45
|
+
lossSummary: _SessionTypedData.lossSummary,
|
|
46
|
+
marketCovers: _SessionTypedData.marketCovers,
|
|
47
|
+
maxSessionTtl: _SessionTypedData.maxSessionTtl,
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Session connector: open a session with one owner signature, then trade
|
|
51
|
+
* every account the grant lists with the session key.
|
|
52
|
+
* @experimental The shapes of `/v2/sessions` may change.
|
|
53
|
+
*/
|
|
54
|
+
export const Session = {
|
|
55
|
+
ownerSignerFromEip1193: _SessionOwnerSigner.eip1193OwnerSigner,
|
|
56
|
+
ownerSignerFromExternal: _SessionOwnerSigner.externalOwnerSigner,
|
|
57
|
+
memoryStorage: _SessionKey.memorySessionStorage,
|
|
58
|
+
indexedDbStorage: _SessionKey.indexedDbSessionStorage,
|
|
59
|
+
};
|
|
60
|
+
export { ALL_LIMITS, CEILING_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, LONGEST_SESSION, MAX_GRANT_SUBACCOUNTS, MINT_HASH_ACCOUNT, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
61
|
+
export { SessionConnector } from './session-connector.js';
|
|
62
|
+
export { DeterminismGuardError, isContractCode } from './determinism-guard.js';
|
|
63
|
+
export { SessionAccounts } from './session-accounts.js';
|
|
64
|
+
export { SessionApiError } from './session-transport.js';
|
|
65
|
+
export { SessionKey } from './session-key.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministically derives a key below the Stark curve order from a hex seed,
|
|
3
|
+
* grinding (re-hashing with an increasing index) to avoid modulo bias.
|
|
4
|
+
* Returns unpadded lowercase hex without `0x`.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export declare function grindKey(keySeed: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a private Stark key from a 65-byte Ethereum signature,
|
|
10
|
+
* grinding the signature's `r` value.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export declare function getPrivateKeyFromEthSignature(ethSignature: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the public Stark key (x coordinate) for a private key,
|
|
16
|
+
* as unpadded lowercase hex without `0x`.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function privateToStarkKey(privateKey: string): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
2
|
+
import { ec } from 'starknet';
|
|
3
|
+
// Stark key derivation, ported from `@starkware-industries/starkware-crypto-utils`
|
|
4
|
+
// (Apache-2.0) to drop its `elliptic`/`bn.js` dependency tree.
|
|
5
|
+
//
|
|
6
|
+
// Outputs must stay byte-for-byte identical to the original implementation,
|
|
7
|
+
// including its quirks (unpadded hex output, odd-length seed handling),
|
|
8
|
+
// because accounts are derived from them.
|
|
9
|
+
const ETH_SIGNATURE_LENGTH = 130;
|
|
10
|
+
const STARK_PRIVATE_KEY_LENGTH = 63;
|
|
11
|
+
const MAX_GRIND_ITERATIONS = 100000;
|
|
12
|
+
const STARK_EC_ORDER = ec.starkCurve.CURVE.n;
|
|
13
|
+
function isHex(hex, length) {
|
|
14
|
+
if (length != null && hex.length !== length)
|
|
15
|
+
return false;
|
|
16
|
+
return /^[0-9a-fA-F]*$/u.test(hex);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Mirrors Node's `Buffer.from(hex, 'hex')`, which the original
|
|
20
|
+
* implementation used: a trailing odd nibble is silently dropped.
|
|
21
|
+
*/
|
|
22
|
+
function hexToBytesDroppingOddNibble(hex) {
|
|
23
|
+
const bytes = new Uint8Array(Math.floor(hex.length / 2));
|
|
24
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
25
|
+
bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
26
|
+
}
|
|
27
|
+
return bytes;
|
|
28
|
+
}
|
|
29
|
+
function hashKeyWithIndex(keySeed, index) {
|
|
30
|
+
let indexHex = index.toString(16);
|
|
31
|
+
if (indexHex.length % 2 !== 0)
|
|
32
|
+
indexHex = `0${indexHex}`;
|
|
33
|
+
const digest = sha256(hexToBytesDroppingOddNibble(keySeed + indexHex));
|
|
34
|
+
let value = 0n;
|
|
35
|
+
for (const byte of digest)
|
|
36
|
+
value = (value << 8n) | BigInt(byte);
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Deterministically derives a key below the Stark curve order from a hex seed,
|
|
41
|
+
* grinding (re-hashing with an increasing index) to avoid modulo bias.
|
|
42
|
+
* Returns unpadded lowercase hex without `0x`.
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
export function grindKey(keySeed) {
|
|
46
|
+
const seed = keySeed.replace(/^0x/u, '');
|
|
47
|
+
if (!isHex(seed))
|
|
48
|
+
throw new Error('Invalid key seed: expected hex string');
|
|
49
|
+
const sha256EcMaxDigest = 1n << 256n;
|
|
50
|
+
const maxAllowedVal = sha256EcMaxDigest - (sha256EcMaxDigest % STARK_EC_ORDER);
|
|
51
|
+
for (let i = 0; i < MAX_GRIND_ITERATIONS; i++) {
|
|
52
|
+
const key = hashKeyWithIndex(seed, i);
|
|
53
|
+
if (key < maxAllowedVal)
|
|
54
|
+
return (key % STARK_EC_ORDER).toString(16);
|
|
55
|
+
}
|
|
56
|
+
throw new Error('Unable to grind key from seed');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns a private Stark key from a 65-byte Ethereum signature,
|
|
60
|
+
* grinding the signature's `r` value.
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export function getPrivateKeyFromEthSignature(ethSignature) {
|
|
64
|
+
const signature = ethSignature.replace(/^0x/u, '');
|
|
65
|
+
if (!isHex(signature, ETH_SIGNATURE_LENGTH))
|
|
66
|
+
throw new Error('Invalid Ethereum signature');
|
|
67
|
+
return grindKey(signature.substring(0, 64));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the public Stark key (x coordinate) for a private key,
|
|
71
|
+
* as unpadded lowercase hex without `0x`.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export function privateToStarkKey(privateKey) {
|
|
75
|
+
const key = privateKey.replace(/^0x/u, '');
|
|
76
|
+
if (key.length > STARK_PRIVATE_KEY_LENGTH || !isHex(key))
|
|
77
|
+
throw new Error('Invalid private key');
|
|
78
|
+
const scalar = BigInt(`0x${key === '' ? '0' : key}`) % STARK_EC_ORDER;
|
|
79
|
+
if (scalar === 0n)
|
|
80
|
+
throw new Error('Invalid private key');
|
|
81
|
+
const publicKey = ec.starkCurve.getStarkKey(scalar.toString(16).padStart(64, '0'));
|
|
82
|
+
return BigInt(publicKey).toString(16);
|
|
83
|
+
}
|