@wormhole-foundation/sdk-algorand
Version:
SDK for Algorand, used in conjunction with @wormhole-foundation/sdk
71 lines • 2.61 kB
JavaScript
import { UniversalAddress, encoding, registerNative } from "@wormhole-foundation/sdk-connect";
import { decodeAddress, encodeAddress, isValidAddress } from "algosdk";
import { _platform, safeBigIntToNumber } from "./types.js";
export const AlgorandZeroAddress = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ";
// Note: for ASA/App IDs we encode them as 8 bytes at the start of
// the 32 byte address bytes.
export class AlgorandAddress {
static byteSize = 32;
static platform = _platform;
type = "Native";
address;
constructor(address) {
if (AlgorandAddress.instanceof(address)) {
this.address = address.address;
}
else if (UniversalAddress.instanceof(address)) {
this.address = address.toUint8Array();
}
else if (address instanceof Uint8Array && address.byteLength === AlgorandAddress.byteSize) {
this.address = address;
}
else if (typeof address === "string" && isValidAddress(address)) {
this.address = decodeAddress(address).publicKey;
}
else if (typeof address === "string" && !isNaN(parseInt(address))) {
this.address = encoding.bytes.zpad(encoding.bignum.toBytes(BigInt(address), 8), AlgorandAddress.byteSize);
}
else if (typeof address === "bigint") {
this.address = encoding.bytes.zpad(encoding.bignum.toBytes(address, 8), AlgorandAddress.byteSize);
}
else if (address instanceof Uint8Array && address.byteLength === 8) {
this.address = encoding.bytes.zpad(address, AlgorandAddress.byteSize);
}
else
throw new Error(`Invalid Algorand address or ASA ID: ${address}`);
}
unwrap() {
return this.toString();
}
toString() {
return encodeAddress(this.address);
}
toNative() {
return this;
}
toUint8Array() {
return this.address;
}
toUniversalAddress() {
return new UniversalAddress(this.toUint8Array());
}
toBigInt() {
return encoding.bignum.decode(this.toUint8Array().slice(24, 32));
}
toInt() {
return safeBigIntToNumber(this.toBigInt());
}
equals(other) {
if (AlgorandAddress.instanceof(other)) {
return other.address === this.address;
}
else {
return this.toUniversalAddress().equals(other);
}
}
static instanceof(address) {
return address.constructor.platform === _platform;
}
}
registerNative(_platform, AlgorandAddress);
//# sourceMappingURL=address.js.map