Skip to content

Commit

Permalink
Normalize aptos address
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaStreamflow committed Jan 4, 2024
1 parent bbd68ea commit a4c2c9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/stream/aptos/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BN from "bn.js";
import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils";
import { Stream, StreamType } from "../common/types";
import { getNumberFromBN } from "../common/utils";
import { normalizeAptosAddress } from "./utils";

export interface ICreateStreamAptosExt {
senderWallet: WalletContextState | AptosAccount;
Expand Down Expand Up @@ -163,10 +164,10 @@ export class Contract implements Stream {
this.canceledAt = parseInt(stream.canceled_at);
this.end = parseInt(stream.end);
this.lastWithdrawnAt = parseInt(stream.last_withdrawn_at);
this.sender = stream.sender;
this.senderTokens = stream.sender;
this.recipient = stream.recipient;
this.recipientTokens = stream.recipient;
this.sender = normalizeAptosAddress(stream.sender);
this.senderTokens = normalizeAptosAddress(stream.sender);
this.recipient = normalizeAptosAddress(stream.recipient);
this.recipientTokens = normalizeAptosAddress(stream.recipient);
this.mint = tokenId;
this.escrowTokens = "";
this.streamflowTreasury = "";
Expand Down
22 changes: 22 additions & 0 deletions packages/stream/aptos/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@ export function extractAptosErrorCode(errorText: string): string | null {

return match[2];
}

function isAddressSpecial(address: string): boolean {
if (address.length === 3) {
return true;
}
return false;
}

// Per this: https://github.com/aptos-labs/aptos-ts-sdk/blob/main/src/core/accountAddress.ts#L115
export function normalizeAptosAddress(address: string): string {
if (isAddressSpecial(address)) {
return address;
}

const length = address.length;
if (length === 64) {
return address;
}

const missingZeros = 64 - length;
return address.slice(0, 2) + "0".repeat(missingZeros) + address.slice(2);
}

0 comments on commit a4c2c9a

Please sign in to comment.