Skip to content

Commit

Permalink
feat(ren-js): updated LUNA support
Browse files Browse the repository at this point in the history
  • Loading branch information
0x31 committed Feb 26, 2021
1 parent 58fa091 commit eff4002
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/lib/chains/chains-filecoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/elliptic": "^6.4.12",
"@types/node": ">=10",
"bignumber.js": "^9.0.1",
"blakejs": "^1.1.0",
"cids": "^1.1.5",
"elliptic": "^6.5.3"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/lib/chains/chains-terra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"@terra-money/terra.js": "1.3.6",
"@types/node": ">=10",
"bech32": "^1.1.4",
"bignumber.js": "^9.0.1"
"bignumber.js": "^9.0.1",
"@types/elliptic": "^6.4.12",
"blakejs": "^1.1.0",
"elliptic": "^6.5.3"
},
"resolutions": {
"sha3": "^2.1.2"
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/chains/chains-terra/src/api/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface TerraAPI {
fetchDeposits: (
address: string,
network: TerraNetwork,
memo: string | undefined,
memo?: string | undefined,
page?: number,
) => Promise<TerraTransaction[]>;

Expand All @@ -32,8 +32,8 @@ export interface TerraAPI {
export type TerraAddress = {
address: string; // Terra address
asset?: string; // Asset tied to the pHash in the memo.
memo?: string; // Base64 string of the pHash.
};

export type TerraDeposit = {
transaction: TerraTransaction;
amount: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/chains/chains-terra/src/api/stakeId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ const fetchDeposits = async (
const chainHeight = filteredTxs.length > 0 ? await getHeight(network) : 0;
return filteredTxs
.map(extractDepositsFromTx(chainHeight))
.reduce(concat, []);
.reduce(concat, [])
.filter((msg) => msg.to === address);
};

const fetchDeposit = async (
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/chains/chains-terra/src/api/terraDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ const fetchDeposits = async (
const chainHeight = filteredTxs.length > 0 ? await getHeight(network) : 0;
return filteredTxs
.map(extractDepositsFromTx(chainHeight))
.reduce(concat, []);
.reduce(concat, [])
.filter((msg) => msg.to === address);
};

const fetchDeposit = async (
Expand Down
30 changes: 25 additions & 5 deletions packages/lib/chains/chains-terra/src/terra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
utilsWithChainNetwork,
} from "@renproject/utils";
import { AccAddress, Key } from "@terra-money/terra.js";
import { blake2b } from "blakejs";
import elliptic from "elliptic";

import {
TerraAddress,
Expand Down Expand Up @@ -172,7 +174,6 @@ export class TerraClass
const txs = await terraDev.fetchDeposits(
address.address,
this.chainNetwork,
address.memo,
);

await Promise.all(
Expand Down Expand Up @@ -208,19 +209,38 @@ export class TerraClass
compressedPublicKey: Buffer,
gHash: Buffer,
): Promise<TerraAddress> | TerraAddress => {
if (!this.chainNetwork) {
if (!this.renNetwork) {
throw new Error(`${this.name} object not initialized.`);
}
this.assertAssetIsSupported(asset);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ec = new elliptic.ec("secp256k1");

// Decode compressed RenVM public key.
const renVMPublicKey = ec.keyFromPublic(compressedPublicKey);

// Interpret gHash as a private key.
const gHashKey = ec.keyFromPrivate(gHash);

// If `NO_PARAMS_FLAG` is set, set renVM public key and gHash public key,
// and recreate key pair from resulting curve point.
const derivedPublicKey = ec.keyFromPublic(
(renVMPublicKey
.getPublic()
.add(gHashKey.getPublic()) as unknown) as elliptic.ec.KeyPair,
);

const newCompressedPublicKey: Buffer = Buffer.from(
derivedPublicKey.getPublic().encodeCompressed(),
);

// @ts-expect-error `Cannot create an instance of an abstract class`
const address: Key = new Key(compressedPublicKey);
const address: Key = new Key(newCompressedPublicKey);

return {
asset,
address: address.accAddress,
memo: toURLBase64(gHash),
// memo: toURLBase64(gHash),
};
};

Expand Down
1 change: 0 additions & 1 deletion packages/lib/chains/chains-terra/src/types/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
declare module "@openworklabs/filecoin-address";
declare module "@openworklabs/filecoin-address/dist/base32";
declare module "elliptic";
declare module "blakejs";
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module "wallet-address-validator";
declare module "bitcore-lib/lib/encoding/base58check";
declare module "bitcore-lib-zcash/lib/encoding/base58check";
declare module "elliptic";
declare module "blakejs";
declare module "@glif/filecoin-address";

0 comments on commit eff4002

Please sign in to comment.