Skip to content

Commit

Permalink
more fixes, improve address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Dec 29, 2023
1 parent 02cb67d commit 1957821
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 120 deletions.
10 changes: 6 additions & 4 deletions core/base/src/utils/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const bignum = {
if (prefix) return "0x" + str;
return str;
},
toBytes: (input: bigint, length?: number) => {
const b = hex.decode(bignum.toString(input));
toBytes: (input: bigint | number, length?: number) => {
const b = hex.decode(bignum.toString(typeof input === "number" ? BigInt(input) : input));
if (!length) return b;
return bytes.zpad(b, length);
},
Expand All @@ -53,8 +53,10 @@ export const bytes = {
decode: (value: Uint8Array): string => new TextDecoder().decode(value),
equals: (lhs: Uint8Array, rhs: Uint8Array): boolean =>
lhs.length === rhs.length && lhs.every((v, i) => v === rhs[i]),
zpad: (arr: Uint8Array, length: number): Uint8Array =>
bytes.concat(new Uint8Array(length - arr.length), arr),
zpad: (arr: Uint8Array, length: number, padStart: boolean = true): Uint8Array =>
padStart
? bytes.concat(new Uint8Array(length - arr.length), arr)
: bytes.concat(arr, new Uint8Array(length - arr.length)),
concat: (...args: Uint8Array[]): Uint8Array => {
const length = args.reduce((acc, curr) => acc + curr.length, 0);
const result = new Uint8Array(length);
Expand Down
9 changes: 6 additions & 3 deletions core/definitions/src/universalAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const algorandAppIdLayout = [

export class UniversalAddress implements Address {
static readonly byteSize = 32;
readonly type: string = "Universal";
static readonly type: string = "Universal";

private readonly address: Uint8Array;

Expand Down Expand Up @@ -50,9 +50,12 @@ export class UniversalAddress implements Address {
return !throws(() => UniversalAddress.stringToUint8Array(address, format));
}

//TODO isn't this quite the code smell? - why would we have to test an any?
static instanceof(address: any): address is UniversalAddress {
return typeof address === "object" && "type" in address && address.type === "Universal";
return (
typeof address === "object" &&
"constructor" in address &&
address.constructor.type === "Universal"
);
}

private static stringToUint8Array(address: string, format: PlatformAddressFormat): Uint8Array {
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"sideEffects": false,
"scripts": {
"algo": "cd ../platforms/algorand/protocols/core && npm run build && cd - && tsx src/algoTokenBridge.ts",
"algo": "cd ../platforms/algorand/protocols/tokenBridge && npm run build && cd - && tsx src/algoTokenBridge.ts",
"tb": "tsx src/tokenBridge.ts",
"cctp": "tsx src/cctp.ts",
"demo": "tsx src/index.ts",
Expand Down
12 changes: 8 additions & 4 deletions examples/src/algoTokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TokenId,
TokenTransfer,
TransferState,
UniversalAddress,
Wormhole,
isTokenId,
normalizeAmount,
Expand All @@ -14,10 +15,12 @@ import { TransferStuff, getStuff } from "./helpers";
// Import the platform specific packages
import { AlgorandPlatform } from "@wormhole-foundation/connect-sdk-algorand";
import { EvmPlatform } from "@wormhole-foundation/connect-sdk-evm";
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";

// Register the protocols
import "@wormhole-foundation/connect-sdk-algorand-tokenbridge";
import "@wormhole-foundation/connect-sdk-evm-tokenbridge";
import "@wormhole-foundation/connect-sdk-solana-tokenbridge";

/*
1. Algorand native to other chain
Expand All @@ -33,16 +36,16 @@ import "@wormhole-foundation/connect-sdk-evm-tokenbridge";
(async function () {
// Init Wormhole object, passing config for which network
// to use (e.g. Mainnet/Testnet) and what Platforms to support
const wh = new Wormhole("Testnet", [AlgorandPlatform, EvmPlatform]);
const wh = new Wormhole("Testnet", [AlgorandPlatform, EvmPlatform, SolanaPlatform]);

// Grab chain Contexts -- these hold a reference to a cached rpc client
const sendChain = wh.getChain("Algorand");
const rcvChain = wh.getChain("Avalanche");
const rcvChain = wh.getChain("Solana");

// Shortcut to allow transferring native gas token
//const token: TokenId | "native" = "native";
const token: TokenId | "native" = "native";

const token = Wormhole.chainAddress("Algorand", "86783266");
// const token = Wormhole.chainAddress("Algorand", "86783266");

// Normalized given token decimals later but can just pass bigints as base units
// Note: The Token bridge will dedust past 8 decimals
Expand Down Expand Up @@ -77,6 +80,7 @@ import "@wormhole-foundation/connect-sdk-evm-tokenbridge";
// Set this to the transfer txid of the initiating transaction to recover a token transfer
// and attempt to fetch details about its progress.
let recoverTxid = undefined;
// recoverTxid = "BCAAZRCXAVTKKPIOTUE32GH6LQCW3CSCR3HAEM3I65KNH7PUUPKA";

// Finally create and perform the transfer given the parameters set above
const xfer = !recoverTxid
Expand Down
10 changes: 5 additions & 5 deletions examples/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge";
const wh = new Wormhole("Testnet", [EvmPlatform, SolanaPlatform]);

// Grab chain Contexts -- these hold a reference to a cached rpc client
const sendChain = wh.getChain("Avalanche");
const rcvChain = wh.getChain("Solana");
const sendChain = wh.getChain("Solana");
const rcvChain = wh.getChain("Avalanche");

// shortcut to allow transferring native gas token
// const token: TokenId<"Avalanche"> | "native" = "native";
const token: TokenId | "native" = "native";

// A TokenId is just a `{chain, address}` pair and an alias for ChainAddress
// The `address` field must be a parsed address.
// You can get a TokenId (or ChainAddress) prepared for you
// by calling the static `chainAddress` method on the Wormhole class.
// e.g.
const token = Wormhole.chainAddress("Avalanche", "0xd00ae08403B9bbb9124bB305C09058E32C39A48c"); // TokenId<"Avalanche">
// const token = Wormhole.chainAddress("Avalanche", "0xd00ae08403B9bbb9124bB305C09058E32C39A48c"); // TokenId<"Avalanche">

// Normalized given token decimals later but can just pass bigints as base units
// Note: The Token bridge will dedust past 8 decimals
// this means any amount specified past that point will be returned
// to the caller
const amount = "0.15";
const amount = "0.001";

// With automatic set to true, perform an automatic transfer. This will invoke a relayer
// contract intermediary that knows to pick up the transfers
Expand Down
Loading

0 comments on commit 1957821

Please sign in to comment.