Skip to content

Commit

Permalink
Merge pull request #1303 from kadena-community/feat/chainweaver-url
Browse files Browse the repository at this point in the history
feat: adds support for a custom Chainweaver url for signWithChainweaver
  • Loading branch information
Ashwin authored Jan 18, 2024
2 parents 4312da6 + 10b8c85 commit 0a0aec6
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 181 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-schools-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/client': minor
---

Adds support for a custom url to signWithChainweaver
3 changes: 2 additions & 1 deletion packages/libs/client-utils/src/example-contract/use-coin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { signWithChainweaver } from '@kadena/client';
import { createSignWithChainweaver } from '@kadena/client';

import { createAccount } from '../coin/create-account';

export async function consumer() {
const signWithChainweaver = createSignWithChainweaver();
const result = await createAccount(
{
account: 'javad',
Expand Down
12 changes: 11 additions & 1 deletion packages/libs/client/etc/client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export function createEckoWalletQuicksign(): IEckoSignFunction;
// @public
export function createEckoWalletSign(): IEckoSignSingleFunction;

// @public
export function createSignWithChainweaver(options?: {
host: string;
}): ISignFunction;

// @public
export const createSignWithKeypair: ICreateSignWithKeypair;

Expand Down Expand Up @@ -404,7 +409,12 @@ export type PactReturnType<T extends (...args: any[]) => any> = T extends (...ar
// @public
export const readKeyset: (key: string) => () => string;

// @public
// Warning: (ae-internal-missing-underscore) The name "signTransactions" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
export const signTransactions: (chainweaverUrl: string) => ISignFunction;

// @public @deprecated
export const signWithChainweaver: ISignFunction;

// @public
Expand Down
141 changes: 85 additions & 56 deletions packages/libs/client/src/signing/chainweaver/signWithChainweaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,104 @@ import { parseTransactionCommand } from '../utils/parseTransactionCommand';
const debug: Debugger = _debug('pactjs:signWithChainweaver');

/**
* Sign with chainweaver according to {@link https://github.com/kadena-io/KIPs/blob/master/kip-0015.md | sign-v1 API}
*
* @public
* @internal
*
*/
export const signWithChainweaver: ISignFunction = (async (
transactionList: IUnsignedCommand | Array<IUnsignedCommand | ICommand>,
) => {
if (transactionList === undefined) {
throw new Error('No transaction(s) to sign');
}
export const signTransactions = (chainweaverUrl: string) =>
(async (
transactionList: IUnsignedCommand | Array<IUnsignedCommand | ICommand>,
) => {
if (transactionList === undefined) {
throw new Error('No transaction(s) to sign');
}

const isList = Array.isArray(transactionList);
const transactions = isList ? transactionList : [transactionList];
const quickSignRequest: IQuickSignRequestBody = {
cmdSigDatas: transactions.map((t) => {
const parsedTransaction = parseTransactionCommand(t);
return {
cmd: t.cmd,
sigs: parsedTransaction.signers.map((signer, i) => {
return {
pubKey: signer.pubKey,
sig: t.sigs[i]?.sig ?? null,
};
}),
};
}),
};
const isList = Array.isArray(transactionList);
const transactions = isList ? transactionList : [transactionList];
const quickSignRequest: IQuickSignRequestBody = {
cmdSigDatas: transactions.map((t) => {
const parsedTransaction = parseTransactionCommand(t);
return {
cmd: t.cmd,
sigs: parsedTransaction.signers.map((signer, i) => {
return {
pubKey: signer.pubKey,
sig: t.sigs[i]?.sig ?? null,
};
}),
};
}),
};

const body: string = JSON.stringify(quickSignRequest);
const body: string = JSON.stringify(quickSignRequest);

debug('calling sign api:', body);
debug('calling sign api:', body);

const response = await fetch('http://127.0.0.1:9467/v1/quicksign', {
method: 'POST',
body,
headers: { 'Content-Type': 'application/json;charset=utf-8' },
});
const response = await fetch(`${chainweaverUrl}/v1/quicksign`, {
method: 'POST',
body,
headers: { 'Content-Type': 'application/json;charset=utf-8' },
});

const bodyText = await response.text();
const bodyText = await response.text();

// response is not JSON when not-ok, that's why we use try-catch
try {
const result = JSON.parse(bodyText) as IQuicksignResponse;
// response is not JSON when not-ok, that's why we use try-catch
try {
const result = JSON.parse(bodyText) as IQuicksignResponse;

if ('error' in result) {
if ('msg' in result.error) {
console.log('error in result', result.error.msg);
if ('error' in result) {
if ('msg' in result.error) {
console.log('error in result', result.error.msg);
}
throw new Error(JSON.stringify(result.error));
}
throw new Error(JSON.stringify(result.error));
}

result.responses.map((signedCommand, i) => {
transactions[i] = addSignatures(
transactions[i],
...signedCommand.commandSigData.sigs.filter(isASigner),
result.responses.map((signedCommand, i) => {
transactions[i] = addSignatures(
transactions[i],
...signedCommand.commandSigData.sigs.filter(isASigner),
);
});

return isList ? transactions : transactions[0];
} catch (error) {
throw new Error(
'An error occurred when adding signatures to the command' +
`\nResponse from v1/quicksign was \`${bodyText}\`. ` +
`\nCode: \`${response.status}\`` +
`\nText: \`${response.statusText}\` ` +
`${error}`,
);
});
}
}) as ISignFunction;

return isList ? transactions : transactions[0];
} catch (error) {
throw new Error(
'An error occurred when adding signatures to the command' +
`\nResponse from v1/quicksign was \`${bodyText}\`. ` +
`\nCode: \`${response.status}\`` +
`\nText: \`${response.statusText}\` ` +
`${error}`,
);
}
}) as ISignFunction;
/**
* * Lets you sign with chainweaver according to {@link https://github.com/kadena-io/KIPs/blob/master/kip-0015.md | sign-v1 API}
*
* @deprecated Use {@link createSignWithChainweaver} instead
* @public
*/
export const signWithChainweaver: ISignFunction = signTransactions(
'http://127.0.0.1:9467',
);

/**
* Creates the signWithChainweaver function with interface {@link ISignFunction}
* Lets you sign with Chainweaver according to {@link https://github.com/kadena-io/KIPs/blob/master/kip-0015.md | sign-v1 API}
*
* @param options - object to customize behaviour.
* - `host: string` - the host of the chainweaver instance to use. Defaults to `http://127.0.0.1:9467`
* @returns - {@link ISignFunction}
* @public
*/
export function createSignWithChainweaver(
options = { host: 'http://127.0.0.1:9467' },
): ISignFunction {
const { host } = options;
const signWithChainweaver = signTransactions(host);

return signWithChainweaver;
}

function isASigner(signer: IQuicksignSigner): signer is {
pubKey: string;
Expand Down
Loading

0 comments on commit 0a0aec6

Please sign in to comment.