Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solana Platform SDK Refactoring #756

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/base/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function lazyInstantiate<T>(factory: () => T): () => T {
if (!instance)
instance = factory();
return instance;
}
};
}

export function onlyOnce<T extends []>(fn: (...args: T) => any, ...args: T): () => void {
Expand All @@ -14,7 +14,7 @@ export function onlyOnce<T extends []>(fn: (...args: T) => any, ...args: T): ()
called = true;
fn(...args);
}
}
};
}

export function throws(fn: () => any): boolean {
Expand All @@ -25,3 +25,17 @@ export function throws(fn: () => any): boolean {
return true;
}
}

/**
* Maps an object to another by applying the given function to every "leaf" property
* (_i.e._ neither object nor array).
*/
export function visitor(input: any, f: (property: any) => any): any {
if (Array.isArray(input)) {
return input.map((v) => visitor(v, f));
} else if (input && typeof input === "object") {
return Object.fromEntries(Object.entries(input).map(([k, v]) => [k, visitor(v, f)]));
} else {
return f(input);
}
}
15 changes: 15 additions & 0 deletions core/base/src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Camel case a string (from https://stackoverflow.com/a/2970667)
export function camelCase(str: string): string {
return (
str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
// Replace any spaces, hyphens, or underscores with an empty string
.replace(/[\s\-_]+/g, "")
);
}

export function upperFirst(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
17 changes: 8 additions & 9 deletions core/definitions/src/testing/mocks/guardian.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { Chain} from "@wormhole-foundation/sdk-base";
import { guardians } from "@wormhole-foundation/sdk-base";
import type { Chain } from "@wormhole-foundation/sdk-base";
import { guardians, encoding } from "@wormhole-foundation/sdk-base";
import type { PayloadLiteral, VAA } from "../../index.js";
import {
Signature,
SignatureUtils,
createVAA,
deserialize,
serialize,
} from "../../index.js";
import { Signature, SignatureUtils, createVAA, deserialize, serialize } from "../../index.js";
import type { UniversalAddress } from "../../universalAddress.js";
import { keccak256 } from "../../utils.js";
import { computeAddress } from "ethers";

interface Guardian {
index: number;
Expand All @@ -31,6 +26,10 @@ export class MockGuardians {
return this.signers.map((guardian) => SignatureUtils.toPubkey(guardian.key));
}

getAddresses() {
return this.getPublicKeys().map((key) => computeAddress(encoding.hex.encode(key)));
}

addSignatures<P extends PayloadLiteral>(
message: Uint8Array | VAA<P>,
guardianIndices?: number[],
Expand Down
Loading
Loading