@paradex/sdk 0.5.1 → 0.5.2
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/dist/account.d.ts +3 -1
- package/dist/account.js +5 -5
- package/dist/config.d.ts +4 -4
- package/dist/config.js +10 -10
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +4 -0
- package/dist/paraclear-provider.js +2 -2
- package/dist/starknet-signer.d.ts +2 -1
- package/dist/starknet-signer.js +28 -8
- package/package.json +1 -1
package/dist/account.d.ts
CHANGED
|
@@ -14,13 +14,15 @@ interface FromEthSignerParams {
|
|
|
14
14
|
*/
|
|
15
15
|
export declare function fromEthSigner({ provider, config, signer, }: FromEthSignerParams): Promise<Account>;
|
|
16
16
|
interface FromStarknetAccountParams {
|
|
17
|
+
/** Paradex chain provider */
|
|
17
18
|
readonly provider: Starknet.ProviderOptions | Starknet.ProviderInterface;
|
|
18
19
|
readonly config: ParadexConfig;
|
|
19
20
|
readonly account: Starknet.AccountInterface;
|
|
21
|
+
readonly starknetProvider?: Starknet.ProviderInterface;
|
|
20
22
|
}
|
|
21
23
|
/**
|
|
22
24
|
* Generates a Paradex account from a Starknet signer.
|
|
23
25
|
* @returns The generated Paradex account.
|
|
24
26
|
*/
|
|
25
|
-
export declare function fromStarknetAccount({ provider, config, account, }: FromStarknetAccountParams): Promise<Account>;
|
|
27
|
+
export declare function fromStarknetAccount({ provider, config, account, starknetProvider, }: FromStarknetAccountParams): Promise<Account>;
|
|
26
28
|
export {};
|
package/dist/account.js
CHANGED
|
@@ -7,7 +7,7 @@ import * as starknetSigner from './starknet-signer.js';
|
|
|
7
7
|
* @returns The generated Paradex account.
|
|
8
8
|
*/
|
|
9
9
|
export async function fromEthSigner({ provider, config, signer, }) {
|
|
10
|
-
const starkKeyTypedData = ethereumSigner.buildEthereumStarkKeyTypedData(config.
|
|
10
|
+
const starkKeyTypedData = ethereumSigner.buildEthereumStarkKeyTypedData(config.ethereumChainId);
|
|
11
11
|
const seed = await signer.signTypedData(starkKeyTypedData);
|
|
12
12
|
const privateKey = keyDerivation.getPrivateKeyFromEthSignature(seed);
|
|
13
13
|
const publicKey = keyDerivation.privateToStarkKey(privateKey);
|
|
@@ -22,10 +22,10 @@ export async function fromEthSigner({ provider, config, signer, }) {
|
|
|
22
22
|
* Generates a Paradex account from a Starknet signer.
|
|
23
23
|
* @returns The generated Paradex account.
|
|
24
24
|
*/
|
|
25
|
-
export async function fromStarknetAccount({ provider, config, account, }) {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
25
|
+
export async function fromStarknetAccount({ provider, config, account, starknetProvider, }) {
|
|
26
|
+
const starkKeyTypedData = starknetSigner.buildStarknetStarkKeyTypedData(config.starknetChainId);
|
|
27
|
+
const accountSupport = await starknetSigner.getAccountSupport(account, starknetProvider ??
|
|
28
|
+
starknetSigner.getPublicProvider(config.starknetChainId));
|
|
29
29
|
const signature = await account.signMessage(starkKeyTypedData);
|
|
30
30
|
const seed = accountSupport.getSeedFromSignature(signature);
|
|
31
31
|
const [privateKey, publicKey] = await starknetSigner.getStarkKeypairFromStarknetSignature(seed);
|
package/dist/config.d.ts
CHANGED
|
@@ -32,11 +32,11 @@ interface BridgedTokenConfig {
|
|
|
32
32
|
readonly l2BridgeAddress: Hex;
|
|
33
33
|
}
|
|
34
34
|
export interface ParadexConfig {
|
|
35
|
-
readonly
|
|
35
|
+
readonly paradexFullNodeRpcUrl: string;
|
|
36
|
+
readonly paradexChainId: string;
|
|
37
|
+
readonly ethereumChainId: string;
|
|
38
|
+
/** Derived from `ethereumChainId` */
|
|
36
39
|
readonly starknetChainId: string;
|
|
37
|
-
readonly l1ChainId: string;
|
|
38
|
-
/** Derived from `l1ChainId` */
|
|
39
|
-
readonly l2ChainId: string;
|
|
40
40
|
readonly paraclearAccountHash: Hex;
|
|
41
41
|
readonly paraclearAccountProxyHash: Hex;
|
|
42
42
|
readonly paraclearAddress: Hex;
|
package/dist/config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ETHEREUM_MAINNET_CHAIN_ID, ETHEREUM_TESTNET_CHAIN_ID, STARKNET_MAINNET_CHAIN_ID, STARKNET_TESTNET_CHAIN_ID, } from './constants.js';
|
|
2
2
|
/**
|
|
3
3
|
* Fetches the Paradex config for the given environment.
|
|
4
4
|
* This is required for account derivation.
|
|
@@ -36,10 +36,10 @@ export function buildConfig(rawConfig) {
|
|
|
36
36
|
},
|
|
37
37
|
]));
|
|
38
38
|
return {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
paradexFullNodeRpcUrl: rawConfig.starknet_fullnode_rpc_url,
|
|
40
|
+
paradexChainId: rawConfig.starknet_chain_id,
|
|
41
|
+
ethereumChainId: rawConfig.l1_chain_id,
|
|
42
|
+
starknetChainId: getStarknetChainId(rawConfig),
|
|
43
43
|
paraclearAccountHash: rawConfig.paraclear_account_hash,
|
|
44
44
|
paraclearAccountProxyHash: rawConfig.paraclear_account_proxy_hash,
|
|
45
45
|
paraclearAddress: rawConfig.paraclear_address,
|
|
@@ -47,12 +47,12 @@ export function buildConfig(rawConfig) {
|
|
|
47
47
|
bridgedTokens,
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
function getStarknetChainId(rawConfig) {
|
|
51
51
|
switch (rawConfig.l1_chain_id) {
|
|
52
|
-
case
|
|
53
|
-
return
|
|
54
|
-
case
|
|
52
|
+
case ETHEREUM_MAINNET_CHAIN_ID:
|
|
53
|
+
return STARKNET_MAINNET_CHAIN_ID;
|
|
54
|
+
case ETHEREUM_TESTNET_CHAIN_ID:
|
|
55
55
|
default:
|
|
56
|
-
return
|
|
56
|
+
return STARKNET_TESTNET_CHAIN_ID;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -2,8 +2,8 @@ import * as Starknet from 'starknet';
|
|
|
2
2
|
export class DefaultProvider extends Starknet.RpcProvider {
|
|
3
3
|
constructor(config) {
|
|
4
4
|
super({
|
|
5
|
-
nodeUrl: config.
|
|
6
|
-
chainId: config.
|
|
5
|
+
nodeUrl: config.paradexFullNodeRpcUrl,
|
|
6
|
+
chainId: Starknet.shortString.encodeShortString(config.paradexChainId),
|
|
7
7
|
});
|
|
8
8
|
}
|
|
9
9
|
}
|
|
@@ -11,4 +11,5 @@ type StarknetKeypair = [string, string];
|
|
|
11
11
|
* `grindKey()` along the Stark curve.
|
|
12
12
|
*/
|
|
13
13
|
export declare function getStarkKeypairFromStarknetSignature(signatureR: string): Promise<StarknetKeypair>;
|
|
14
|
-
export declare function getAccountSupport(account: Starknet.AccountInterface): Promise<AccountSupport>;
|
|
14
|
+
export declare function getAccountSupport(account: Starknet.AccountInterface, starknetProvider: Starknet.ProviderInterface): Promise<AccountSupport>;
|
|
15
|
+
export declare function getPublicProvider(chainId: string): Starknet.ProviderInterface;
|
package/dist/starknet-signer.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { keyDerivation } from '@starkware-industries/starkware-crypto-utils';
|
|
2
2
|
import * as Starknet from 'starknet';
|
|
3
|
+
import { STARKNET_MAINNET_CHAIN_ID } from './constants.js';
|
|
3
4
|
import { AccountSupport } from './starknet-account-support.js';
|
|
4
5
|
export function buildStarknetStarkKeyTypedData(starknetChainId) {
|
|
5
6
|
return {
|
|
@@ -38,9 +39,9 @@ export async function getStarkKeypairFromStarknetSignature(signatureR) {
|
|
|
38
39
|
const publicKey = keyDerivation.privateToStarkKey(privateKey);
|
|
39
40
|
return [privateKey, publicKey];
|
|
40
41
|
}
|
|
41
|
-
export async function getAccountSupport(account) {
|
|
42
|
-
const classHash = await getAccountClassHash(account);
|
|
43
|
-
const contract = await buildAccountContract(account);
|
|
42
|
+
export async function getAccountSupport(account, starknetProvider) {
|
|
43
|
+
const classHash = await getAccountClassHash(starknetProvider, account.address);
|
|
44
|
+
const contract = await buildAccountContract(starknetProvider, account.address);
|
|
44
45
|
const accountSupport = new AccountSupport(contract, classHash);
|
|
45
46
|
try {
|
|
46
47
|
const supportCheckResult = await accountSupport.check();
|
|
@@ -56,9 +57,28 @@ export async function getAccountSupport(account) {
|
|
|
56
57
|
}
|
|
57
58
|
return accountSupport;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
const RPC_NODES_MAINNET = [
|
|
61
|
+
'https://starknet-mainnet.public.blastapi.io',
|
|
62
|
+
'https://free-rpc.nethermind.io/mainnet-juno',
|
|
63
|
+
];
|
|
64
|
+
const RPC_NODES_TESTNET = [
|
|
65
|
+
'https://starknet-sepolia.public.blastapi.io',
|
|
66
|
+
'https://free-rpc.nethermind.io/sepolia-juno',
|
|
67
|
+
];
|
|
68
|
+
export function getPublicProvider(chainId) {
|
|
69
|
+
const nodes = chainId === STARKNET_MAINNET_CHAIN_ID
|
|
70
|
+
? RPC_NODES_MAINNET
|
|
71
|
+
: RPC_NODES_TESTNET;
|
|
72
|
+
const randIdx = Math.floor(Math.random() * nodes.length);
|
|
73
|
+
const node = nodes[randIdx];
|
|
74
|
+
if (node == null)
|
|
75
|
+
throw new Error('No public provider defined');
|
|
76
|
+
const provider = new Starknet.RpcProvider({ nodeUrl: node });
|
|
77
|
+
return provider;
|
|
78
|
+
}
|
|
79
|
+
async function getAccountClassHash(provider, accountAddress) {
|
|
60
80
|
try {
|
|
61
|
-
const classHash = await
|
|
81
|
+
const classHash = await provider.getClassHashAt(accountAddress);
|
|
62
82
|
return classHash;
|
|
63
83
|
}
|
|
64
84
|
catch (cause) {
|
|
@@ -67,8 +87,8 @@ async function getAccountClassHash(account) {
|
|
|
67
87
|
throw new Error(message);
|
|
68
88
|
}
|
|
69
89
|
}
|
|
70
|
-
async function buildAccountContract(
|
|
71
|
-
const accountClass = await
|
|
72
|
-
const contract = new Starknet.Contract(accountClass.abi,
|
|
90
|
+
async function buildAccountContract(provider, accountAddress) {
|
|
91
|
+
const accountClass = await provider.getClassAt(accountAddress);
|
|
92
|
+
const contract = new Starknet.Contract(accountClass.abi, accountAddress, provider);
|
|
73
93
|
return contract;
|
|
74
94
|
}
|