Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test("returns signature", async () => {
const url = "http://localhost:3000";
const smartContractScriptHash = "1234";
const mockExecute = jest.fn(() => ({
result: {
parameters: [1, 2]
}
}));
const mockGetContractState = jest.fn().mockImplementation(() => {
return {
execute: mockExecute
};
});
rpc.Query.getContractState = mockGetContractState;
const result = await common.getVerificationSignatureForSmartContract(
url,
smartContractScriptHash
);
expect(result).toEqual(
expect.objectContaining({
invocationScript: "0000",
verificationScript: ""
})
);
expect(mockGetContractState).toBeCalledWith(smartContractScriptHash);
expect(mockExecute).toBeCalledWith(url);
});
});
const tld = domain.split(".").reverse()[0];
const regExp = new RegExp(`.${tld}$`);
const subdomain = domain.replace(regExp, "");
const hashSubdomain = u.sha256(u.str2hexstring(subdomain));
const hashDomain = u.sha256(u.str2hexstring(tld));
const hashName = u.sha256(hashSubdomain.concat(hashDomain));
const parsedName = sc.ContractParam.byteArray(hashName, "name");
const args = [protocol, parsedName, empty];
const sb = new sc.ScriptBuilder();
const script = sb.emitAppCall(contract, operation, args).str;
const res = await rpc.Query.invokeScript(script).execute(url);
return rpc.StringParser(res.result.stack[0]);
}
export async function getVerificationSignatureForSmartContract(
url: string,
smartContractScriptHash: string
): Promise {
const contractState = await rpc.Query.getContractState(
smartContractScriptHash
).execute(url);
const { parameters } = contractState.result;
const witness = new tx.Witness({
invocationScript: "00".repeat(parameters.length),
verificationScript: ""
});
witness.scriptHash = smartContractScriptHash;
return witness;
}
export async function getUrl(net: string): Promise {
const orderedUrls = getUrls(net);
const slicedUrls = cutArray(orderedUrls);
var previousBlockCount = 0;
for (let i = 0; i < slicedUrls.length; i++) {
try {
const res = (await rpc.Query.getBlockCount().execute(slicedUrls[i])) as {
result: number;
};
const currentBlockCount = res.result;
if (currentBlockCount - previousBlockCount <= 5) {
return slicedUrls[i];
}
previousBlockCount = Math.max(currentBlockCount, previousBlockCount);
} catch (e) {
continue;
}
}
throw new Error("Exhausted all urls but found no available RPC");
}
export async function sendTx<
T extends ManagedApiBasicConfig
>(config: T): Promise {
checkProperty(config, "tx", "url");
const response = await rpc.Query.sendRawTransaction(config.tx!).execute(
config.url!
);
if (response.result === true) {
response.txid = config.tx!.hash;
} else {
log.error(
`Transaction failed for ${
config.account.address
}: ${config.tx!.serialize()}`
);
}
return Object.assign(config, { response });
}
url: string,
scriptHash: string,
address?: string
): Promise {
const parser = address ? parseTokenInfoAndBalance : parseTokenInfo;
const sb = new sc.ScriptBuilder();
abi.name(scriptHash)(sb);
abi.symbol(scriptHash)(sb);
abi.decimals(scriptHash)(sb);
abi.totalSupply(scriptHash)(sb);
if (address) {
abi.balanceOf(scriptHash, address)(sb);
}
const script = sb.str;
try {
const res = await rpc.Query.invokeScript(script)
.parseWith(parser)
.execute(url);
const result: TokenInfo = {
name: res[0],
symbol: res[1],
decimals: res[2],
totalSupply: res[3].div(Math.pow(10, 8 - res[2])).toNumber()
};
if (address) {
result.balance = res[4].div(Math.pow(10, 8 - res[2]));
}
return result;
} catch (err) {
log.error(`getToken failed with : ${err.message}`);
throw err;
}