-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.ts
49 lines (44 loc) · 1.32 KB
/
utils.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
import {
CoinType,
ethAddressFromDelegated,
newDelegatedEthAddress,
} from "@glif/filecoin-address";
import RpcEngine from "@glif/filecoin-rpc-client";
import { SECP256K1KeyProvider } from "@glif/filecoin-wallet-provider";
const hexlify = (id: string) => {
const hexId = Number(id.slice(1)).toString(16);
return "0xff" + "0".repeat(38 - hexId.length) + hexId;
};
export const deriveAddrsFromPk = async (
pk: string,
apiAddress: string,
ethAddr: string
) => {
// dont pass 0x to the SECP key provider
const provider = new SECP256K1KeyProvider(pk.slice(2), "hex");
const [secpActor] = await provider.getAccounts(0, 1, CoinType.TEST);
const delegatedActor = newDelegatedEthAddress(
ethAddr,
CoinType.TEST
).toString();
let idActor = "";
let idActorHex = "";
try {
const filRpc = new RpcEngine({ apiAddress });
idActor = await filRpc.request("StateLookupID", delegatedActor, null);
idActorHex = hexlify(idActor);
} catch (err) {
console.log(
`Actor ${delegatedActor} does not yet exist on chain. Try sending it some funds first. Error: ${err}`
);
}
return { secpActor, idActor, idActorHex, delegatedActor };
};
export const toEthAddr = (addr: string): string => {
try {
const address = ethAddressFromDelegated(addr);
return address;
} catch {
return addr;
}
};