Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _convertType (type: ContractABITypes): string {
if (isString(type)) {
return type;
} else if (Array.isArray(type)) {
return `(${type.map((type) => this._convertType(type)).join(',')})`;
} else if (type['Option']) {
return `Option<${this._convertType(type['Option'].T)}>`;
} else if (type['Result']) {
return `()`; // Result is not supported, but only applicable for returns
} else if (type['Vec']) {
return `Vec<${this._convertType(type['Vec'].T)}>`;
} else if (type['[T;n]']) {
return `[${this._convertType(type['[T;n]'].T)};${type['[T;n]'].n}]`;
}
throw new Error(`Unknown type specified ${JSON.stringify(type)}`);
}
const createMethod = (method: string, key: string, { documentation, type }: SubstrateMetadata): any => {
const creator = createFunction(
{
meta: {
documentation: new Vec(Text, documentation),
modifier: createType('StorageEntryModifierV7', 1), // default
type: new StorageEntryType(type, isString(type) ? 0 : 1),
toJSON: (): any => key
} as unknown as StorageEntryMetadata,
method,
prefix,
section
},
{ key, skipHashing: true }
);
return ((...args: any[]): Uint8Array =>
blake2AsU8a(creator(...args))
) as any;
};
public isAvailable(_address: Uint8Array | string): boolean {
const accountsValue = this.accounts.subject.getValue();
const addressesValue = this.addresses.subject.getValue();
const contractsValue = this.contracts.subject.getValue();
const address = isString(_address)
? _address
: this.encodeAddress(_address);
return !accountsValue[address] && !addressesValue[address] && !contractsValue[address];
}
public getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
const address = isString(_address)
? _address
: this.encodeAddress(_address);
const publicKey = this.decodeAddress(address);
const stores = type
? [this.stores[type]]
: Object.values(this.stores);
const info = stores.reduce((lastInfo, store): SingleAddress | undefined =>
(store().subject.getValue()[address] || lastInfo), undefined);
return info && {
address,
publicKey,
meta: info.json.meta
};
}
public getAddress(_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
const address = isString(_address)
? _address
: this.encodeAddress(_address);
const publicKey = this.decodeAddress(address);
const stores = type
? [this.stores[type]]
: Object.values(this.stores);
const info = stores.reduce((lastInfo, store): SingleAddress | undefined =>
(store().subject.getValue()[address] || lastInfo), undefined);
return info && {
address,
publicKey,
meta: info.json.meta,
};
}
public soft (value: number | BN | string | Uint8Array): DeriveJunction {
if (isNumber(value) || isBn(value)) {
return this.soft(bnToHex(value, BN_OPTIONS));
} else if (isString(value)) {
return isHex(value)
? this.soft(hexToU8a(value))
: this.soft(compactAddLength(stringToU8a(value)));
}
if (value.length > JUNCTION_ID_LEN) {
return this.soft(blake2AsU8a(value));
}
this._chainCode.fill(0);
this._chainCode.set(value, 0);
return this;
}
messages.forEach((method) => {
const unknownKeys = Object.keys(method).filter((key) => !['args', 'mutates', 'name', 'selector', 'return_type'].includes(key));
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for messages.${method.name}`);
assert(isString(method.name), `Expected name for messages.${method.name}`);
assert(isNumber(method.selector), `Expected selector for messages.${method.name}`);
assert(isNull(method.return_type) || isString(method.return_type) || isObject(method.return_type), `Expected return_type for messages.${method.name}`);
validateArgs(`messages.${method.name}`, method.args);
});
}
public isAvailable(_address: Uint8Array | string): boolean {
const accountsValue = this.accounts.subject.getValue();
const addressesValue = this.addresses.subject.getValue();
const contractsValue = this.contracts.subject.getValue();
const address = isString(_address)
? _address
: this.encodeAddress(_address);
return !accountsValue[address] && !addressesValue[address] && !contractsValue[address];
}
export function createImgSize (size?: string | number): Record {
if (!size) {
return {
height: 'auto',
width: '100%'
};
}
const height = isString(size)
? size
: `${size}px`;
return {
height,
width: height
};
}
public static decodeDate (value: CodecDate | Date | AnyNumber): Date {
if (value instanceof Date) {
return value;
} else if (isU8a(value)) {
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
} else if (isString(value)) {
value = new BN(value, 10, 'le');
}
return new Date(
bnToBn(value as BN).toNumber() * 1000
);
}