Skip to content

Commit

Permalink
make sure address is encoded using provided ss58Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuripetusko committed Mar 1, 2022
1 parent 1da5444 commit bafd461
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
14 changes: 10 additions & 4 deletions cli/consolidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ const getLiteDump = (result: ConsolidatorReturnType) => {
const getRemarks = (
inputData: any,
prefixes: string[],
collectionFilter?: string
collectionFilter?: string,
ss58Format?: number
): Remark[] => {
let blocks = inputData;
if (collectionFilter) {
blocks = filterBlocksByCollection(blocks, collectionFilter, prefixes);
blocks = filterBlocksByCollection(
blocks,
collectionFilter,
prefixes,
ss58Format
);
}
return getRemarksFromBlocks(blocks, prefixes);
return getRemarksFromBlocks(blocks, prefixes, ss58Format);
};

const consolidate = async () => {
Expand Down Expand Up @@ -103,7 +109,7 @@ const consolidate = async () => {

console.log(`Loaded ${rawdata.length} blocks with remark calls`);

const remarks = getRemarks(rawdata, prefixes, collectionFilter);
const remarks = getRemarks(rawdata, prefixes, collectionFilter, ss58Format);
const consolidator = new Consolidator(ss58Format);
let result = await consolidator.consolidate(remarks);

Expand Down
3 changes: 2 additions & 1 deletion cli/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const fetch = async () => {
extracted = filterBlocksByCollection(
extracted,
collectionFilter,
prefixToArray(args["--prefixes"] || "")
prefixToArray(args["--prefixes"] || ""),
ss58Format
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface IProps {
storageProvider?: IStorageProvider;
storageKey?: string;
loggerEnabled?: boolean;
ss58Format?: number;
}

export interface IStorageProvider {
Expand Down Expand Up @@ -63,6 +64,7 @@ export class RemarkListener {
private prefixes: string[];
private currentBlockNum: number;
private loggerEnabled: boolean;
private ss58Format: number;
public storageProvider: IStorageProvider;
private consolidateFunction: (
remarks: Remark[]
Expand All @@ -75,12 +77,14 @@ export class RemarkListener {
storageProvider,
storageKey,
loggerEnabled = false,
ss58Format = 2,
}: IProps) {
if (!polkadotApi) {
throw new Error(
`"providerInterface" is missing. Please provide polkadot.js provider interface (i.e. websocket)`
);
}
this.ss58Format = ss58Format;
this.currentBlockNum = 0;
this.apiPromise = polkadotApi;
this.missingBlockCalls = [];
Expand Down
9 changes: 7 additions & 2 deletions src/rmrk1.0.0/classes/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { validateCollection } from "../../tools/validate-remark";
import { getRemarkData } from "../../tools/utils";
import { OP_TYPES, VERSION } from "../../tools/constants";
import { Attribute } from "../../types";
import { encodeAddress } from "@polkadot/keyring";

export class Collection {
readonly block: number;
Expand Down Expand Up @@ -83,7 +84,11 @@ export class Collection {
);
}

static fromRemark(remark: string, block = 0): Collection | string {
static fromRemark(
remark: string,
block = 0,
ss58Format?: number
): Collection | string {
try {
validateCollection(remark);
const [prefix, op_type, version, dataString] = remark.split("::");
Expand All @@ -92,7 +97,7 @@ export class Collection {
block,
obj.name,
obj.max,
obj.issuer,
encodeAddress(obj.issuer, ss58Format),
obj.symbol,
obj.id,
obj.metadata
Expand Down
2 changes: 1 addition & 1 deletion src/tools/consolidator/consolidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Consolidator {

let collection;
try {
collection = getCollectionFromRemark(remark);
collection = getCollectionFromRemark(remark, this.ss58Format);
} catch (e: any) {
invalidate(remark.remark, e.message);
return true;
Expand Down
7 changes: 5 additions & 2 deletions src/tools/consolidator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ export const getChangeIssuerEntity = (
}
return changeIssuerEntity;
};
export const getCollectionFromRemark = (remark: Remark) => {
const collection = C100.fromRemark(remark.remark, remark.block);
export const getCollectionFromRemark = (
remark: Remark,
ss58Format?: number
) => {
const collection = C100.fromRemark(remark.remark, remark.block, ss58Format);
if (typeof collection === "string") {
throw new Error(
`[${OP_TYPES.MINT}] Dead before instantiation: ${collection}`
Expand Down
10 changes: 6 additions & 4 deletions src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ const validateDecode = (value: string) => {

export const getRemarksFromBlocks = (
blocks: Block[],
prefixes: string[]
prefixes: string[],
ss58Format = 2
): Remark[] => {
const remarks: Remark[] = [];
for (const row of blocks) {
Expand Down Expand Up @@ -160,7 +161,7 @@ export const getRemarksFromBlocks = (

const r: Remark = {
block: row.block,
caller: call.caller,
caller: encodeAddress(call.caller, ss58Format),
interaction_type: meta.type,
version: meta.version,
remark: remark,
Expand Down Expand Up @@ -354,10 +355,11 @@ export const getRemarkData = (dataString: string) => {
export const filterBlocksByCollection = (
blockCalls: BlockCalls[],
collectionFilter: string,
prefixes: string[]
prefixes: string[],
ss58Format?: number
): BlockCalls[] =>
blockCalls.filter((block) =>
getRemarksFromBlocks([block], prefixes).some((rmrk) =>
getRemarksFromBlocks([block], prefixes, ss58Format).some((rmrk) =>
rmrk.remark.includes(collectionFilter)
)
);

0 comments on commit bafd461

Please sign in to comment.