Skip to content

Commit

Permalink
fix cardanowallet
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq committed Feb 26, 2021
1 parent 72d3b35 commit 856d502
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/services/cardanoWallet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { UtilEither } from "../utils";
import {assertNever, unsafe_poolBech32_to_hash, UtilEither} from "../utils";

import { Pool } from "pg";
import { Request, Response } from "express";
import * as utils from "../utils";
import axios from "axios";
import config from "config";
import {latestMetadataQuery, smashPoolLookUp} from "./poolInfo";
import {SmashLookUpResponse, smashPoolLookUp} from "./poolInfo";

const smashEndpoint: string = config.get("server.smashEndpoint");

const poolByRewards = `
select pool_id, cost, margin, pledge, saturation, non_myopic_member_rewards::int, produced_blocks::int, relative_stake
Expand All @@ -19,6 +15,7 @@ const poolByRewards = `

export interface CardanoWalletPool {
pool_id: string,
address_hash: string,
pool_info: any,
cost: string,
margin: string,
Expand All @@ -42,7 +39,7 @@ export const getCardanoWalletPools = async (pool: Pool, limit: number, offset: n
}

export const handleGetCardanoWalletPools = (pool: Pool) => async (req: Request, res: Response): Promise<void> => {
let limit = 100;
let limit = 50;
if (req.body.limit != null && req.body.limit < limit) {
limit = req.body.limit;
}
Expand All @@ -57,9 +54,17 @@ export const handleGetCardanoWalletPools = (pool: Pool) => async (req: Request,
switch (result.kind) {
case "ok": {
const promisesWithPoolInfo = result.value.map(async (walletPoolInfo) => {
const pool_info = await smashPoolLookUp(pool, walletPoolInfo.pool_id)
const addressHash = unsafe_poolBech32_to_hash(walletPoolInfo.pool_id)
let pool_info: SmashLookUpResponse = {
metadataHash: null,
smashInfo: null,
}
if (addressHash != null) {
pool_info = await smashPoolLookUp(pool, addressHash)
}
return {
...walletPoolInfo,
pool_hash: addressHash,
pool_info: pool_info.smashInfo,
}
})
Expand All @@ -74,6 +79,6 @@ export const handleGetCardanoWalletPools = (pool: Pool) => async (req: Request,
}

default:
return utils.assertNever(result);
return assertNever(result);
}
}
4 changes: 3 additions & 1 deletion src/services/poolInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export const smashPoolLookUp = async (p: Pool, hash: string): Promise<SmashLookU
console.log(`SMASH did not respond to user submitted hash: ${hash}`);
}
} catch(e) {
console.log(`SMASH did not respond with hash ${hash}, giving error ${e}`);
if (e.toString() !== 'Error: Request failed with status code 404') {
console.log(`SMASH did not respond with hash ${hash}, giving error ${e}`);
}
}

return {
Expand Down
1 change: 1 addition & 0 deletions src/utils/cip5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const Prefixes = Object.freeze({
STAKE: "stake",
STAKE_TEST: "stake_test",
PAYMENT_KEY_HASH: "addr_vkh",
POOL: "pool",
});
25 changes: 22 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
import { decode, fromWords } from "bech32";
import { Prefixes } from "./cip5";
import {Asset} from "../Transactions/types";
import axios, {AxiosPromise} from "axios";

export const contentTypeHeaders = { headers: {"Content-Type": "application/json"}};
import axios from "axios";

export const errMsgs = { noValue: "no value" };

Expand Down Expand Up @@ -198,6 +196,17 @@ export function validateRewardAddress(
return rewardAddr != null;
}

export function unsafe_poolBech32_to_hash(text: string): string | null {
try {
const bech32Info = decode(text, 1023);
const payload = fromWords(bech32Info.words);
return `${Buffer.from(payload).toString("hex")}`
} catch (e) {
// silently discard any non-valid Cardano addresses
}
return null
}

export function getAddressesByType(addresses: string[]): {
/**
* note: we keep track of explicit bech32 addresses
Expand All @@ -208,11 +217,13 @@ export function getAddressesByType(addresses: string[]): {
bech32: string[],
paymentCreds: string[],
stakingKeys: string[],
pools: string[],
} {
const legacyAddr = [];
const bech32 = [];
const paymentCreds = [];
const stakingKeys = [];
const pools = [];
for (const address of addresses) {
// 1) Check if it's a Byron-era address
if (ByronAddress.is_valid(address)) {
Expand All @@ -239,6 +250,13 @@ export function getAddressesByType(addresses: string[]): {
wasmBech32.free();
break;
}
case Prefixes.POOL: {
const payload = fromWords(bech32Info.words);
pools.push(
`\\x${Buffer.from(payload).toString("hex")}`
);
break;
}
case Prefixes.STAKE_TEST: {
const wasmBech32 = Address.from_bech32(address);
stakingKeys.push(
Expand Down Expand Up @@ -281,5 +299,6 @@ export function getAddressesByType(addresses: string[]): {
bech32,
paymentCreds,
stakingKeys,
pools,
};
}

0 comments on commit 856d502

Please sign in to comment.