-
Notifications
You must be signed in to change notification settings - Fork 1
/
starknet-hyper-account.ts
95 lines (87 loc) · 2.2 KB
/
starknet-hyper-account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import BN from 'bn.js';
import {
Abi,
Account,
Call,
ec,
json,
KeyPair,
number,
Provider,
} from 'starknet';
import axios from 'axios';
import { utils } from 'ethers';
export const EthAddress: string = process.env.NEXT_PUBLIC_ETH_ADDRESS || '';
export const PaymasterAddress: string = process.env.NEXT_PUBLIC_PAYMASTER_ADDRESS || '';
export const starknetProvider = new Provider({
sequencer: {
// TODO: just for testnet
network: 'goerli-alpha',
},
});
export function nomalizeEth(rawEth: BN) {
return utils.formatEther(rawEth.toString());
}
export function generateStarkKeyPair(hexString: string) {
const pwk = number.toFelt(hexString);
const keyPair = ec.getKeyPair(pwk);
return keyPair;
}
export async function createStarkKey(publicKey: string) {
const raw = await axios({
method: 'get',
headers: {
'Content-Type': 'text/plain',
},
transformResponse: (res) => {
return res
},
url: './Account.txt',
})
const AccountContractRaw = json.parse(raw.data)
const result = await starknetProvider.deployContract({
contract: AccountContractRaw,
constructorCalldata: [publicKey],
addressSalt: publicKey
});
const receipt = await starknetProvider.waitForTransaction(result.transaction_hash);
return result;
}
export async function zeroGasExecute(
starknetProvider: Provider,
paymasterAddress: string,
accountAddress: string,
keyPair: KeyPair,
calls: Call[],
abis: Abi[],
) {
const paymasterAccount = new Account(
starknetProvider,
paymasterAddress,
keyPair,
);
const account = new Account(
starknetProvider,
accountAddress,
keyPair,
);
const nonce = await account.getNonce();
const result = await paymasterAccount.execute([
{
contractAddress: accountAddress,
entrypoint: '__execute__',
},
...calls,
], [
[{
inputs: [],
name: '__execute__',
outputs: [],
type: 'function',
}],
...abis,
], {
nonce,
});
return result;
}