Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Oct 24, 2020
1 parent db379f6 commit 1f62395
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
12 changes: 8 additions & 4 deletions src/Ada.js
Expand Up @@ -693,7 +693,7 @@ export default class Ada {

// we are done for every certificate except pool registration

if (type == CertificateTypes.STAKE_POOL_REGISTRATION) {
if (type === CertificateTypes.STAKE_POOL_REGISTRATION) {
if(!poolParams)
throw new Error("missing stake pool params in a pool registration certificate");

Expand Down Expand Up @@ -844,10 +844,14 @@ export default class Ada {
}
}
}
if (!witnessOwner) throw new Error("no owner given by path");

// a single witness for the pool owner given by path
witnessPaths.push(witnessOwner.stakingPath);
if (witnessOwner) {
// a single witness for the pool owner given by path
witnessPaths.push(witnessOwner.stakingPath);
} else {
throw new Error("no owner given by path");
}

} else {
// we collect required witnesses for inputs, certificates and withdrawals
// each path is included only once
Expand Down
33 changes: 7 additions & 26 deletions src/cardano.js
Expand Up @@ -8,26 +8,6 @@ const KEY_HASH_LENGTH = 28;
const POOL_REGISTRATION_OWNERS_MAX = 1000;
const POOL_REGISTRATION_RELAYS_MAX = 1000;

function parseBIP32Index(str: string, errMsg: ?str = null): number {
let base = 0;
if (str.endsWith("'")) {
str = str.slice(0, -1);
base = HARDENED;
}
const i = utils.safe_parseInt(str);
Precondition.check(i >= 0, errMsg);
Precondition.check(i < HARDENED, errMsg);
return base + i;
}

export function str_to_path(data: string): Array<number> {
const errMsg = "invalid bip32 path string";
Precondition.checkIsString(data, errMsg);
Precondition.check(data.length > 0, errMsg);

return data.split("/").map(parseBIP32Index);
}

export function serializeAddressParams(
addressTypeNibble: number,
networkIdOrProtocolMagic: number,
Expand Down Expand Up @@ -118,6 +98,9 @@ export function serializePoolInitialParams(
Precondition.checkIsUint64(marginNumerator, errMsg);
const marginDenominator = utils.safe_parseInt(params.margin.denominatorStr);
Precondition.checkIsUint64(marginDenominator, errMsg);
Precondition.check(marginNumerator >= 0, errMsg);
Precondition.check(marginDenominator > 0, errMsg);
Precondition.check(marginNumerator <= marginDenominator, errMsg);

errMsg = "invalid reward account";
Precondition.checkIsHexString(params.rewardAccountHex, errMsg);
Expand Down Expand Up @@ -254,17 +237,17 @@ export function serializePoolMetadataParams(
if (!params) {
// deal with null metadata
includeMetadataBuffer.writeUInt8(POOL_CERTIFICATE_METADATA_NO);
return buffer;
return includeMetadataBuffer;
} else {
includeMetadataBuffer.writeUInt8(POOL_CERTIFICATE_METADATA_YES);
}

const url = params.metadataUrl;
Precondition.checkIsString(url, "invalid pool metadata url");
Precondition.check(url.length <= 64, "pool metadata url too long");
Precondition.checkIsString(url, "invalid pool metadata: invalid url");
Precondition.check(url.length <= 64, "invalid pool metadata: url too long");

const hashHex = params.metadataHashHex;
const errMsg = "invalid pool metadata hash";
const errMsg = "invalid pool metadata: invalid hash";
Precondition.checkIsHexString(hashHex, errMsg);
Precondition.check(hashHex.length === 32 * 2, errMsg);

Expand All @@ -281,8 +264,6 @@ export function serializePoolMetadataParams(
export default {
HARDENED,

str_to_path,

serializeAddressParams,

serializePoolInitialParams,
Expand Down
30 changes: 29 additions & 1 deletion src/utils.js
@@ -1,7 +1,9 @@
//@flow
import basex from "base-x";
import bech32 from "bech32";
import {AddressTypeNibbles} from "./Ada"
import { AddressTypeNibbles } from "./Ada"
import cardano from "./cardano";


const BASE58_ALPHABET =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand Down Expand Up @@ -164,6 +166,28 @@ export function path_to_buf(path: Array<number>): Buffer {
return data;
}

function parseBIP32Index(str: string, errMsg: ?string = null): number {
let base = 0;
if (str.endsWith("'")) {
str = str.slice(0, -1);
base = cardano.HARDENED;
}
const i = safe_parseInt(str);
Precondition.check(i >= 0, errMsg);
Precondition.check(i < cardano.HARDENED, errMsg);
return base + i;
}

export function str_to_path(data: string): Array<number> {
const errMsg = "invalid bip32 path string ";
Precondition.checkIsString(data, errMsg);
Precondition.check(data.length > 0, errMsg);

return data.split("/").map(function (x: string): number {
return parseBIP32Index(x, errMsg + data + " because of " + x);
});
}

const sum = (arr: Array<number>) => arr.reduce((x, y) => x + y, 0);

export function chunkBy(data: Buffer, chunkLengths: Array<number>) {
Expand Down Expand Up @@ -287,6 +311,8 @@ export function safe_parseInt(str: string): number {


export default {
Assert,

hex_to_buf,
buf_to_hex,

Expand All @@ -302,6 +328,8 @@ export default {
// no pair for now
path_to_buf,

str_to_path,

safe_parseInt,

amount_to_buf,
Expand Down
4 changes: 3 additions & 1 deletion test/src/test_utils.js
Expand Up @@ -3,8 +3,10 @@ import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";

import Ada, { utils, cardano } from "../../lib/Ada";

export const Assert = utils.Assert;

export const serializeAddressInfo = cardano.serializeAddressInfo;
export const str_to_path = cardano.str_to_path;
export const str_to_path = utils.str_to_path;
export const hex_to_buf = utils.hex_to_buf;
export const pathToBuffer = str => utils.path_to_buf(str_to_path(str));
export const uint32_to_buf = utils.uint32_to_buf
Expand Down

0 comments on commit 1f62395

Please sign in to comment.