Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getPrelimState (address: string, options: Partial): Observable<[Index, Header | null]> {
return combineLatest([
// if we have a nonce already, don't retrieve the latest, use what is there
isUndefined(options.nonce)
? api.query.system.accountNonce(address)
: of(createType('Index', options.nonce)),
// if we have an era provided already or eraLength is <= 0 (immortal)
// don't get the latest block, just pass null, handle in mergeMap
(isUndefined(options.era) || (isNumber(options.era) && options.era > 0))
? api.rpc.chain.getHeader()
: of(null)
]);
}
function decodeFromValue (registry: Registry, def: TypesDef, value?: any): Decoded {
if (isU8a(value)) {
return createFromValue(registry, def, value[0], value.subarray(1));
} else if (isNumber(value)) {
return createFromValue(registry, def, value);
} else if (isString(value)) {
return decodeFromString(registry, def, value.toString());
} else if (isObject(value)) {
const key = Object.keys(value)[0];
return decodeFromJSON(registry, def, key, value[key]);
}
// Worst-case scenario, return the first with default
return createFromValue(registry, def, 0);
}
function parseSelector (registry: Registry, fnname: string, input: ContractABIMessageCommon['selector']): u32 {
if (isNumber(input)) {
return createType(registry, 'u32', input);
} else if (isHex(input)) {
return createType(registry, 'u32', hexToU8a(input));
} else if (typeof input === 'string') {
try {
const array = JSON.parse(input);
assert(array.length === 4, `${fnname}: Invalid selector length`);
return createType(registry, 'u32', Uint8Array.from(
// the as number[] is here to pacify TS, it doesn't quite know how to handle the cb
(array as number[]).map((value: string | number): number =>
isHex(value)
? hexToNumber(value.toString())
: value
)
private static decodeViaValue (def: TypesDef, value?: any): Decoded {
if (value instanceof Enum) {
return Enum.createValue(def, value._index, value.raw);
} else if (isU8a(value)) {
return Enum.createValue(def, value[0], value.subarray(1));
} else if (isNumber(value)) {
return Enum.createValue(def, value);
} else if (isString(value)) {
const _str = value.toString();
return isHex(_str)
? Enum.decodeViaValue(def, hexToU8a(_str))
: Enum.createViaJSON(def, _str);
} else if (isObject(value)) {
const key = Object.keys(value)[0];
return Enum.createViaJSON(def, key, value[key]);
}
// Worst-case scenario, return the first with default
return Enum.createValue(def, 0);
}
public static decodeAddress (registry: Registry, value: AnyAddress): AccountId | AccountIndex {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof Address) {
return value.raw;
} else if (isBn(value) || isNumber(value)) {
return createType(registry, 'AccountIndex', value);
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
return Address.decodeU8a(registry, u8aToU8a(value));
}
return Address.decodeString(registry, value);
}
constructor (options: KeyringOptions = {}) {
options.type = options.type || 'ed25519';
options.ss58Format = isNumber(options.ss58Format)
? options.addressPrefix
: options.ss58Format;
assert(options && ['ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519' or 'sr25519', found '${options.type}`);
this._pairs = new Pairs();
this._type = options.type;
if (isNumber(options.ss58Format)) {
setSS58Format(options.ss58Format);
}
}
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;
}
export default function validateRequest (id: number, jsonrpc: string): void {
assert(jsonrpc === '2.0', 'Invalid jsonrpc field, expected \'2.0\'');
assert(isNumber(id), 'Expected a numeric id');
}
messages.forEach((message): void => {
const unknownKeys = Object.keys(message).filter((key): boolean => !['args', 'docs', 'mutates', 'name', 'selector', 'return_type'].includes(key));
const fnname = `messages.${message.name}`;
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for messages.${message.name}`);
const { name, selector, return_type: returnType } = message;
assert(isNumber(name) && isString(this.stringAt(name)), `Expected name for ${fnname}`);
assert(isNull(returnType) || (isNumber(returnType.ty) && isObject(this.typeDefAt(returnType.ty))), `Expected return_type for ${fnname}`);
parseSelector(this.registry, fnname, selector);
this.validateArgs(fnname, message.args);
});
}
messages.forEach((method): void => {
const unknownKeys = Object.keys(method).filter((key): boolean => !['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);
});
}