@paradex/sdk 0.4.2 → 0.5.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.
- package/dist/account.d.ts +12 -2
- package/dist/account.js +20 -21
- package/dist/config.d.ts +2 -0
- package/dist/config.js +10 -0
- package/dist/ethereum-signer.d.ts +6 -0
- package/dist/ethereum-signer.js +21 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -1
- package/dist/starknet-account-support.d.ts +22 -0
- package/dist/starknet-account-support.js +197 -0
- package/dist/starknet-signer.d.ts +14 -0
- package/dist/starknet-signer.js +74 -0
- package/package.json +2 -1
package/dist/account.d.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import * as Starknet from 'starknet';
|
|
2
2
|
import type { ParadexConfig } from './config.js';
|
|
3
|
-
import
|
|
3
|
+
import * as ethereumSigner from './ethereum-signer.js';
|
|
4
4
|
export interface Account extends Starknet.Account {
|
|
5
5
|
}
|
|
6
6
|
interface FromEthSignerParams {
|
|
7
7
|
readonly provider: Starknet.ProviderOptions | Starknet.ProviderInterface;
|
|
8
8
|
readonly config: ParadexConfig;
|
|
9
|
-
readonly signer: EthereumSigner;
|
|
9
|
+
readonly signer: ethereumSigner.EthereumSigner;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* Generates a Paradex account from an Ethereum wallet.
|
|
13
13
|
* @returns The generated Paradex account.
|
|
14
14
|
*/
|
|
15
15
|
export declare function fromEthSigner({ provider, config, signer, }: FromEthSignerParams): Promise<Account>;
|
|
16
|
+
interface FromStarknetAccountParams {
|
|
17
|
+
readonly provider: Starknet.ProviderOptions | Starknet.ProviderInterface;
|
|
18
|
+
readonly config: ParadexConfig;
|
|
19
|
+
readonly account: Starknet.AccountInterface;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Generates a Paradex account from a Starknet signer.
|
|
23
|
+
* @returns The generated Paradex account.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fromStarknetAccount({ provider, config, account, }: FromStarknetAccountParams): Promise<Account>;
|
|
16
26
|
export {};
|
package/dist/account.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { keyDerivation } from '@starkware-industries/starkware-crypto-utils';
|
|
2
2
|
import * as Starknet from 'starknet';
|
|
3
|
+
import * as ethereumSigner from './ethereum-signer.js';
|
|
4
|
+
import * as starknetSigner from './starknet-signer.js';
|
|
3
5
|
/**
|
|
4
6
|
* Generates a Paradex account from an Ethereum wallet.
|
|
5
7
|
* @returns The generated Paradex account.
|
|
6
8
|
*/
|
|
7
9
|
export async function fromEthSigner({ provider, config, signer, }) {
|
|
8
|
-
const starkKeyTypedData =
|
|
9
|
-
const
|
|
10
|
-
const privateKey = keyDerivation.getPrivateKeyFromEthSignature(
|
|
10
|
+
const starkKeyTypedData = ethereumSigner.buildEthereumStarkKeyTypedData(config.l1ChainId);
|
|
11
|
+
const seed = await signer.signTypedData(starkKeyTypedData);
|
|
12
|
+
const privateKey = keyDerivation.getPrivateKeyFromEthSignature(seed);
|
|
11
13
|
const publicKey = keyDerivation.privateToStarkKey(privateKey);
|
|
12
14
|
const address = generateAccountAddress({
|
|
13
15
|
publicKey: `0x${publicKey}`,
|
|
@@ -17,25 +19,22 @@ export async function fromEthSigner({ provider, config, signer, }) {
|
|
|
17
19
|
return new Starknet.Account(provider, address, `0x${privateKey}`);
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @returns The typed data object.
|
|
22
|
+
* Generates a Paradex account from a Starknet signer.
|
|
23
|
+
* @returns The generated Paradex account.
|
|
23
24
|
*/
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
38
|
-
};
|
|
25
|
+
export async function fromStarknetAccount({ provider, config, account, }) {
|
|
26
|
+
const starknetChainId = config.l2ChainId;
|
|
27
|
+
const starkKeyTypedData = starknetSigner.buildStarknetStarkKeyTypedData(starknetChainId);
|
|
28
|
+
const accountSupport = await starknetSigner.getAccountSupport(account);
|
|
29
|
+
const signature = await account.signMessage(starkKeyTypedData);
|
|
30
|
+
const seed = accountSupport.getSeedFromSignature(signature);
|
|
31
|
+
const [privateKey, publicKey] = await starknetSigner.getStarkKeypairFromStarknetSignature(seed);
|
|
32
|
+
const address = generateAccountAddress({
|
|
33
|
+
publicKey: `0x${publicKey}`,
|
|
34
|
+
accountClassHash: config.paraclearAccountHash,
|
|
35
|
+
accountProxyClassHash: config.paraclearAccountProxyHash,
|
|
36
|
+
});
|
|
37
|
+
return new Starknet.Account(provider, address, `0x${privateKey}`);
|
|
39
38
|
}
|
|
40
39
|
/**
|
|
41
40
|
* Generates an account address based on the account contract and public key.
|
package/dist/config.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export interface ParadexConfig {
|
|
|
35
35
|
readonly starknetFullNodeRpcUrl: string;
|
|
36
36
|
readonly starknetChainId: string;
|
|
37
37
|
readonly l1ChainId: string;
|
|
38
|
+
/** Derived from `l1ChainId` */
|
|
39
|
+
readonly l2ChainId: string;
|
|
38
40
|
readonly paraclearAccountHash: Hex;
|
|
39
41
|
readonly paraclearAccountProxyHash: Hex;
|
|
40
42
|
readonly paraclearAddress: Hex;
|
package/dist/config.js
CHANGED
|
@@ -39,6 +39,7 @@ export function buildConfig(rawConfig) {
|
|
|
39
39
|
starknetFullNodeRpcUrl: rawConfig.starknet_fullnode_rpc_url,
|
|
40
40
|
starknetChainId: Starknet.shortString.encodeShortString(rawConfig.starknet_chain_id),
|
|
41
41
|
l1ChainId: rawConfig.l1_chain_id,
|
|
42
|
+
l2ChainId: getL2ChainId(rawConfig),
|
|
42
43
|
paraclearAccountHash: rawConfig.paraclear_account_hash,
|
|
43
44
|
paraclearAccountProxyHash: rawConfig.paraclear_account_proxy_hash,
|
|
44
45
|
paraclearAddress: rawConfig.paraclear_address,
|
|
@@ -46,3 +47,12 @@ export function buildConfig(rawConfig) {
|
|
|
46
47
|
bridgedTokens,
|
|
47
48
|
};
|
|
48
49
|
}
|
|
50
|
+
function getL2ChainId(rawConfig) {
|
|
51
|
+
switch (rawConfig.l1_chain_id) {
|
|
52
|
+
case '1':
|
|
53
|
+
return 'SN_MAIN';
|
|
54
|
+
case '11155111':
|
|
55
|
+
default:
|
|
56
|
+
return 'SN_SEPOLIA';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -16,3 +16,9 @@ export interface EthereumSigner {
|
|
|
16
16
|
readonly signTypedData: (typedData: TypedData) => Promise<string>;
|
|
17
17
|
}
|
|
18
18
|
export declare function ethersSignerAdapter(ethersSigner: ethers.Signer): EthereumSigner;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the typed data that needs to be signed by an Ethereum
|
|
21
|
+
* wallet in order to generate a Paradex account.
|
|
22
|
+
* @returns The typed data object.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildEthereumStarkKeyTypedData(ethereumChainId: string): TypedData;
|
package/dist/ethereum-signer.js
CHANGED
|
@@ -5,3 +5,24 @@ export function ethersSignerAdapter(ethersSigner) {
|
|
|
5
5
|
},
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Returns the typed data that needs to be signed by an Ethereum
|
|
10
|
+
* wallet in order to generate a Paradex account.
|
|
11
|
+
* @returns The typed data object.
|
|
12
|
+
*/
|
|
13
|
+
export function buildEthereumStarkKeyTypedData(ethereumChainId) {
|
|
14
|
+
return {
|
|
15
|
+
domain: {
|
|
16
|
+
name: 'Paradex',
|
|
17
|
+
chainId: ethereumChainId,
|
|
18
|
+
version: '1',
|
|
19
|
+
},
|
|
20
|
+
primaryType: 'Constant',
|
|
21
|
+
types: {
|
|
22
|
+
Constant: [{ name: 'action', type: 'string' }],
|
|
23
|
+
},
|
|
24
|
+
message: {
|
|
25
|
+
action: 'STARK Key',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare const Config: {
|
|
|
8
8
|
};
|
|
9
9
|
export declare const Account: {
|
|
10
10
|
fromEthSigner: typeof _Account.fromEthSigner;
|
|
11
|
+
fromStarknetAccount: typeof _Account.fromStarknetAccount;
|
|
11
12
|
};
|
|
12
13
|
export declare const Signer: {
|
|
13
14
|
ethersSignerAdapter: typeof _Signer.ethersSignerAdapter;
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,10 @@ import * as _Signer from './ethereum-signer.js';
|
|
|
4
4
|
import * as _ParaclearProvider from './paraclear-provider.js';
|
|
5
5
|
import * as _Paraclear from './paraclear.js';
|
|
6
6
|
export const Config = { fetchConfig: _Config.fetchConfig };
|
|
7
|
-
export const Account = {
|
|
7
|
+
export const Account = {
|
|
8
|
+
fromEthSigner: _Account.fromEthSigner,
|
|
9
|
+
fromStarknetAccount: _Account.fromStarknetAccount,
|
|
10
|
+
};
|
|
8
11
|
export const Signer = { ethersSignerAdapter: _Signer.ethersSignerAdapter };
|
|
9
12
|
export const ParaclearProvider = {
|
|
10
13
|
DefaultProvider: _ParaclearProvider.DefaultProvider,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as Starknet from 'starknet';
|
|
2
|
+
interface CheckResult {
|
|
3
|
+
readonly ok: boolean;
|
|
4
|
+
readonly reason?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class AccountSupport {
|
|
7
|
+
/** The contract instance of the account. */
|
|
8
|
+
private readonly contract;
|
|
9
|
+
/** The class hash of the account contract. */
|
|
10
|
+
private readonly classHash;
|
|
11
|
+
private signatureFormat;
|
|
12
|
+
constructor(
|
|
13
|
+
/** The contract instance of the account. */
|
|
14
|
+
contract: Starknet.Contract,
|
|
15
|
+
/** The class hash of the account contract. */
|
|
16
|
+
classHash: string);
|
|
17
|
+
private testClassHash;
|
|
18
|
+
private getFormat;
|
|
19
|
+
check(): Promise<CheckResult>;
|
|
20
|
+
getSeedFromSignature(signature: Starknet.Signature): string;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import * as Starknet from 'starknet';
|
|
2
|
+
export class AccountSupport {
|
|
3
|
+
constructor(
|
|
4
|
+
/** The contract instance of the account. */
|
|
5
|
+
contract,
|
|
6
|
+
/** The class hash of the account contract. */
|
|
7
|
+
classHash) {
|
|
8
|
+
this.contract = contract;
|
|
9
|
+
this.classHash = classHash;
|
|
10
|
+
this.signatureFormat = null;
|
|
11
|
+
this.classHash = Starknet.num.cleanHex(this.classHash);
|
|
12
|
+
}
|
|
13
|
+
testClassHash(classHash) {
|
|
14
|
+
return this.classHash === Starknet.num.cleanHex(classHash);
|
|
15
|
+
}
|
|
16
|
+
async getFormat() {
|
|
17
|
+
if (this.testClassHash('0x1a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003')) {
|
|
18
|
+
return 'argent-v0.3.0';
|
|
19
|
+
}
|
|
20
|
+
if (this.testClassHash('0x29927c8af6bccf3f6fda035981e765a7bdbf18a2dc0d630494f8758aa908e2b')) {
|
|
21
|
+
return 'argent-v0.3.1';
|
|
22
|
+
}
|
|
23
|
+
if (this.testClassHash('0x036078334509b514626504edc9fb252328d1a240e4e948bef8d0c08dff45927f')) {
|
|
24
|
+
const ownerTypeResp = (await this.contract.call('get_owner_type'));
|
|
25
|
+
const ownerType = ownerTypeResp.activeVariant();
|
|
26
|
+
switch (ownerType) {
|
|
27
|
+
case 'Starknet':
|
|
28
|
+
return 'argent-v0.4.0-starknet-signer';
|
|
29
|
+
case 'Secp256k1':
|
|
30
|
+
return 'argent-v0.4.0-secp256k1-signer';
|
|
31
|
+
case 'Secp256r1':
|
|
32
|
+
return 'argent-v0.4.0-secp256r1-signer';
|
|
33
|
+
case 'Eip191':
|
|
34
|
+
return 'argent-v0.4.0-eip191-signer';
|
|
35
|
+
case 'Webauthn':
|
|
36
|
+
return 'argent-v0.4.0-webauthn-signer';
|
|
37
|
+
default:
|
|
38
|
+
return 'unknown';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (this.testClassHash('0x0381f14e5e0db5889c981bf050fb034c0fbe0c4f070ee79346a05dbe2bf2af90')) {
|
|
42
|
+
return 'argent-multicall';
|
|
43
|
+
}
|
|
44
|
+
if (this.testClassHash('0x737ee2f87ce571a58c6c8da558ec18a07ceb64a6172d5ec46171fbc80077a48')) {
|
|
45
|
+
return 'argent-multisig-v0.1.0';
|
|
46
|
+
}
|
|
47
|
+
if (this.testClassHash('0x6e150953b26271a740bf2b6e9bca17cc52c68d765f761295de51ceb8526ee72')) {
|
|
48
|
+
return 'argent-multisig-v0.1.1';
|
|
49
|
+
}
|
|
50
|
+
if (this.testClassHash('0x07aeca3456816e3b833506d7cc5c1313d371fbdb0ae95ee70af72a4ddbf42594')) {
|
|
51
|
+
return 'argent-multisig-v0.2.0';
|
|
52
|
+
}
|
|
53
|
+
// https://github.com/myBraavos/braavos-cairo/blob/6efdfd597bb051e99c79a512fccd14ee2523c898/README_MOA.md
|
|
54
|
+
if (this.testClassHash('0x041bf1e71792aecb9df3e9d04e1540091c5e13122a731e02bec588f71dc1a5c3')) {
|
|
55
|
+
return 'braavos-multi-owner-v1.0.0';
|
|
56
|
+
}
|
|
57
|
+
// https://github.com/myBraavos/braavos-cairo/tree/v1.0.0
|
|
58
|
+
if (this.testClassHash('0x00816dd0297efc55dc1e7559020a3a825e81ef734b558f03c83325d4da7e6253')) {
|
|
59
|
+
const multiSigThreshold = (await this.contract.call('get_multisig_threshold'));
|
|
60
|
+
if (multiSigThreshold !== 0n) {
|
|
61
|
+
return 'braavos-v1.0.0-multisig';
|
|
62
|
+
}
|
|
63
|
+
const signers = (await this.contract.call('get_signers'));
|
|
64
|
+
if (signers.secp256r1 != null && signers.secp256r1.length > 0)
|
|
65
|
+
return 'braavos-v1.0.0-strong-signer';
|
|
66
|
+
if (signers.webauthn != null && signers.webauthn.length > 0)
|
|
67
|
+
return 'braavos-v1.0.0-strong-signer';
|
|
68
|
+
if (signers.stark != null && signers.stark.length === 1)
|
|
69
|
+
return 'braavos-v1.0.0-stark-signer';
|
|
70
|
+
return 'unknown';
|
|
71
|
+
}
|
|
72
|
+
return 'unknown';
|
|
73
|
+
}
|
|
74
|
+
async check() {
|
|
75
|
+
this.signatureFormat = await this.getFormat();
|
|
76
|
+
switch (this.signatureFormat) {
|
|
77
|
+
case 'argent-v0.3.0':
|
|
78
|
+
case 'argent-v0.3.1':
|
|
79
|
+
case 'argent-v0.4.0-starknet-signer':
|
|
80
|
+
case 'braavos-v1.0.0-stark-signer':
|
|
81
|
+
return { ok: true };
|
|
82
|
+
case 'argent-v0.4.0-secp256k1-signer':
|
|
83
|
+
return {
|
|
84
|
+
ok: false,
|
|
85
|
+
reason: 'Argent Secp256k1Signer is not supported',
|
|
86
|
+
};
|
|
87
|
+
case 'argent-v0.4.0-secp256r1-signer':
|
|
88
|
+
return {
|
|
89
|
+
ok: false,
|
|
90
|
+
reason: 'Argent Secp256r1Signer is not supported',
|
|
91
|
+
};
|
|
92
|
+
case 'argent-v0.4.0-eip191-signer':
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
reason: 'Argent Eip191Signer is not supported',
|
|
96
|
+
};
|
|
97
|
+
case 'argent-v0.4.0-webauthn-signer':
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
reason: 'Argent WebauthnSigner is not supported',
|
|
101
|
+
};
|
|
102
|
+
case 'argent-multicall':
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
reason: 'Argent multicall is not supported',
|
|
106
|
+
};
|
|
107
|
+
case 'argent-multisig-v0.1.0':
|
|
108
|
+
case 'argent-multisig-v0.1.1':
|
|
109
|
+
case 'argent-multisig-v0.2.0':
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
reason: 'Argent multisig is not supported',
|
|
113
|
+
};
|
|
114
|
+
case 'braavos-v1.0.0-strong-signer':
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
reason: 'Braavos strong signer is not supported',
|
|
118
|
+
};
|
|
119
|
+
case 'braavos-v1.0.0-multisig':
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
reason: 'Braavos multisig is not supported',
|
|
123
|
+
};
|
|
124
|
+
case 'braavos-multi-owner-v1.0.0':
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
reason: 'Braavos Multi Owner Account is not supported',
|
|
128
|
+
};
|
|
129
|
+
case 'unknown':
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
reason: 'Unsupported account contract',
|
|
133
|
+
};
|
|
134
|
+
// no default
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
getSeedFromSignature(signature) {
|
|
138
|
+
const segments = Starknet.stark.signatureToHexArray(signature);
|
|
139
|
+
if (this.signatureFormat == null) {
|
|
140
|
+
throw new Error('Check account contract support first');
|
|
141
|
+
}
|
|
142
|
+
switch (this.signatureFormat) {
|
|
143
|
+
case 'argent-v0.3.0':
|
|
144
|
+
case 'argent-v0.3.1': {
|
|
145
|
+
const [r, _s] = segments;
|
|
146
|
+
if (r == null)
|
|
147
|
+
throw new Error('Argent signature is missing R segment');
|
|
148
|
+
return r;
|
|
149
|
+
}
|
|
150
|
+
case 'argent-v0.4.0-starknet-signer': {
|
|
151
|
+
if (segments.length === 2) {
|
|
152
|
+
const [r, _s] = segments;
|
|
153
|
+
if (r == null)
|
|
154
|
+
throw new Error('Argent signature is missing R segment');
|
|
155
|
+
return r;
|
|
156
|
+
}
|
|
157
|
+
if (segments.length === 5) {
|
|
158
|
+
const [_numSignatures, _sigType, _pubKey, r, _s] = segments;
|
|
159
|
+
if (r == null)
|
|
160
|
+
throw new Error('Argent signature is missing R segment');
|
|
161
|
+
return r;
|
|
162
|
+
}
|
|
163
|
+
throw new Error('Unsupported Argent signature');
|
|
164
|
+
}
|
|
165
|
+
case 'braavos-v1.0.0-stark-signer': {
|
|
166
|
+
if (segments.length === 2) {
|
|
167
|
+
const [r, _s] = segments;
|
|
168
|
+
if (r == null)
|
|
169
|
+
throw new Error('Braavos signature is missing R segment');
|
|
170
|
+
return r;
|
|
171
|
+
}
|
|
172
|
+
if (segments.length === 3) {
|
|
173
|
+
const [_signerType, r, _s] = segments;
|
|
174
|
+
if (r == null)
|
|
175
|
+
throw new Error('Braavos signature is missing R segment');
|
|
176
|
+
return r;
|
|
177
|
+
}
|
|
178
|
+
throw new Error('Unsupported Braavos signature');
|
|
179
|
+
}
|
|
180
|
+
case 'argent-v0.4.0-secp256k1-signer':
|
|
181
|
+
case 'argent-v0.4.0-secp256r1-signer':
|
|
182
|
+
case 'argent-v0.4.0-eip191-signer':
|
|
183
|
+
case 'argent-v0.4.0-webauthn-signer':
|
|
184
|
+
case 'argent-multicall':
|
|
185
|
+
case 'argent-multisig-v0.1.0':
|
|
186
|
+
case 'argent-multisig-v0.1.1':
|
|
187
|
+
case 'argent-multisig-v0.2.0':
|
|
188
|
+
case 'braavos-v1.0.0-strong-signer':
|
|
189
|
+
case 'braavos-v1.0.0-multisig':
|
|
190
|
+
case 'braavos-multi-owner-v1.0.0':
|
|
191
|
+
throw new Error(`${this.signatureFormat} is not supported`);
|
|
192
|
+
case 'unknown':
|
|
193
|
+
throw new Error('Unsupported account contract');
|
|
194
|
+
// no default
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Signature, SignerInterface, TypedData } from 'starknet';
|
|
2
|
+
import * as Starknet from 'starknet';
|
|
3
|
+
import { AccountSupport } from './starknet-account-support.js';
|
|
4
|
+
export type { SignerInterface as Signer, TypedData, Signature };
|
|
5
|
+
export declare function buildStarknetStarkKeyTypedData(starknetChainId: string): TypedData;
|
|
6
|
+
type StarknetKeypair = [string, string];
|
|
7
|
+
/**
|
|
8
|
+
* This function borrows from starkware-crypto-utils's implementation
|
|
9
|
+
* of `getPrivateKeyFromEthSignature()` where a deterministic
|
|
10
|
+
* signature R segment, is hex encoded as the `keySeed` for
|
|
11
|
+
* `grindKey()` along the Stark curve.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getStarkKeypairFromStarknetSignature(signatureR: string): Promise<StarknetKeypair>;
|
|
14
|
+
export declare function getAccountSupport(account: Starknet.AccountInterface): Promise<AccountSupport>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { keyDerivation } from '@starkware-industries/starkware-crypto-utils';
|
|
2
|
+
import * as Starknet from 'starknet';
|
|
3
|
+
import { AccountSupport } from './starknet-account-support.js';
|
|
4
|
+
export function buildStarknetStarkKeyTypedData(starknetChainId) {
|
|
5
|
+
return {
|
|
6
|
+
domain: {
|
|
7
|
+
name: 'Paradex',
|
|
8
|
+
chainId: starknetChainId,
|
|
9
|
+
version: '1',
|
|
10
|
+
},
|
|
11
|
+
primaryType: 'Constant',
|
|
12
|
+
types: {
|
|
13
|
+
StarkNetDomain: [
|
|
14
|
+
{ name: 'name', type: 'felt' },
|
|
15
|
+
{ name: 'version', type: 'felt' },
|
|
16
|
+
{ name: 'chainId', type: 'felt' },
|
|
17
|
+
],
|
|
18
|
+
Constant: [{ name: 'action', type: 'felt' }],
|
|
19
|
+
},
|
|
20
|
+
message: {
|
|
21
|
+
action: 'STARK Key',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* This function borrows from starkware-crypto-utils's implementation
|
|
27
|
+
* of `getPrivateKeyFromEthSignature()` where a deterministic
|
|
28
|
+
* signature R segment, is hex encoded as the `keySeed` for
|
|
29
|
+
* `grindKey()` along the Stark curve.
|
|
30
|
+
*/
|
|
31
|
+
export async function getStarkKeypairFromStarknetSignature(signatureR) {
|
|
32
|
+
const curve = keyDerivation.StarkExEc;
|
|
33
|
+
if (curve == null) {
|
|
34
|
+
throw new Error('StarkExEc curve is not defined');
|
|
35
|
+
}
|
|
36
|
+
const r = signatureR.replace(/^0x/u, '');
|
|
37
|
+
const privateKey = keyDerivation.grindKey(r, curve);
|
|
38
|
+
const publicKey = keyDerivation.privateToStarkKey(privateKey);
|
|
39
|
+
return [privateKey, publicKey];
|
|
40
|
+
}
|
|
41
|
+
export async function getAccountSupport(account) {
|
|
42
|
+
const classHash = await getAccountClassHash(account);
|
|
43
|
+
const contract = await buildAccountContract(account);
|
|
44
|
+
const accountSupport = new AccountSupport(contract, classHash);
|
|
45
|
+
try {
|
|
46
|
+
const supportCheckResult = await accountSupport.check();
|
|
47
|
+
if (!supportCheckResult.ok) {
|
|
48
|
+
const message = supportCheckResult.reason ??
|
|
49
|
+
'Unspecified error checking account support';
|
|
50
|
+
throw new Error(message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (cause) {
|
|
54
|
+
const message = 'Error checking account support. Please try again.';
|
|
55
|
+
throw new Error(message);
|
|
56
|
+
}
|
|
57
|
+
return accountSupport;
|
|
58
|
+
}
|
|
59
|
+
async function getAccountClassHash(account) {
|
|
60
|
+
try {
|
|
61
|
+
const classHash = await account.getClassHashAt(account.address);
|
|
62
|
+
return classHash;
|
|
63
|
+
}
|
|
64
|
+
catch (cause) {
|
|
65
|
+
const message = 'Cannot determine account type. Make sure your' +
|
|
66
|
+
' account contract is deployed and try again.';
|
|
67
|
+
throw new Error(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function buildAccountContract(account) {
|
|
71
|
+
const accountClass = await account.getClassAt(account.address);
|
|
72
|
+
const contract = new Starknet.Contract(accountClass.abi, account.address, account);
|
|
73
|
+
return contract;
|
|
74
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paradex/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "JavaScript SDK for Paradex",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"ethers": "^6.11.1",
|
|
47
47
|
"husky": "^9.0.11",
|
|
48
48
|
"jest": "^29.7.0",
|
|
49
|
+
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
49
50
|
"lint-staged": "^15.2.2",
|
|
50
51
|
"prettier": "3.2.5",
|
|
51
52
|
"ts-jest": "^29.1.2",
|