Closed doc-l closed 3 years ago
Just a thought - Have you looked in to using the Go SDK instead?
Thanks, no I have never used go before but have many years of JS/TS experience. I'd like to keep it in javascript or typescript
I got it working, I saw in the stellar source code (https://stellar.github.io/js-stellar-base/transaction.js.html) that the envelope can also be sent as string and will be converted by the stellar sdk. So I modified
const envelope = xdr.TransactionEnvelope.fromXDR(Buffer.from(reqBody.envelope_xdr, 'base64'));
to
const envelope = reqBody.envelope_xdr;
And reran and the transaction was successful.
For reference, the full code for non-express is as follows. It doesn't check the request, but that is easy enough to add in given the source code of the express code.
import {
Environment,
PrivateKey,
NetworkPasshrase,
} from '@kinecosystem/kin-sdk-v2';
import {
SignTransactionResponse,
} from '@kinecosystem/kin-sdk-v2/dist/webhook';
export default async (event) => {
const env = process.env.KIN_ENV === 'prod' ? Environment.Prod : Environment.Test;
let networkPassphrase;
switch (env) {
case Environment.Test:
networkPassphrase = NetworkPasshrase.Test;
break;
case Environment.Prod:
networkPassphrase = NetworkPasshrase.Prod;
break;
default:
throw (Error('No env'));
}
const reqBody = JSON.parse(event.body);
let signResponse;
const kinVersion = (reqBody.kin_version ? reqBody.kin_version : 3);
if (kinVersion === 4) {
if (!reqBody.solana_transaction || typeof reqBody.solana_transaction !== 'string') {
return {
statusCode: 400,
};
}
signResponse = new SignTransactionResponse();
} else {
if (!reqBody.envelope_xdr || typeof reqBody.envelope_xdr !== 'string') {
return {
statusCode: 400,
};
}
const envelope = reqBody.envelope_xdr;
signResponse = new SignTransactionResponse(envelope, networkPassphrase);
signResponse.sign(PrivateKey.fromString(process.env.HOT_WALLET_SECRET));
}
if (signResponse.isRejected() || (signResponse.envelope && !signResponse.signedEnvelope)) {
return {
statusCode: 403,
body: JSON.stringify({
invoice_errors: signResponse.invoiceErrors,
}),
};
}
if (signResponse.envelope) {
return {
statusCode: 200,
body: JSON.stringify({
envelope_xdr: signResponse.signedEnvelope.toXDR('base64'),
}),
};
}
return {
statusCode: 200,
};
};
I'll keep the issue open so someone could do something with this, or close it will
For kin 3 it's not enough to simply return a 200 when signing a webhook. I use serverless, so express is not possible and as the webhooks assume serverless, I cannot really use the webhook code. I am trying to build it myself using the transaction signer as example and have this now. I just want to get a transaction working, so I removed all the request stuff to get a minimum working example.
Note that I sign the transaction
signResponse.sign(PrivateKey.fromString(process.env.HOT_WALLET_SECRET));
.When I do that, I get the following error:
This is some stellar internal stuff which I have no idea what to do with.
How can I sign a transaction without using express?