import { ApiClient } from "./api-client";
import * as AWS from "aws-sdk";
// Create instance of KMS for key signing
const kms = new AWS.KMS({ apiVersion: "2014-11-01", region: "eu-west-1" });
// Get the public Key (This will need to be your own custom code to pull the PublicKey from the AWS KMS publicKey which is a DER-encoded X.509 public key, also known as SubjectPublicKeyInfo (SPKI),
// Sample code: https://github.com/Trustology/trustvault-nodejs-sdk/blob/master/src/ts/sign-examples/aws-kms.ts
// https://docs.aws.amazon.com/kms/latest/APIReference/API_GetPublicKey.html
const newPublicKey = getUncompressedPublicKeyInHex(await this.kms.getPublicKey({KeyId: "my-key"}).promise())
// For demonstration purposes only. Do not hard code your API key, store it somewhere safe.
const userApiKey = "test-api-key";
// Production
const prodUrl = "https://tapi-production.trustology-prod.com/graphql";
// API reflects the postman query written in JavaScript
const apiClient = new ApiClient(userApiKey, prodUrl);
/**
* Sends an ethereum transaction with the given parameters
* 1. Creates the ethereum transaction using the given parameters
* 2. Validates the returned ethereum transaction is what is expected
* 3. Signs and submits the key signature pairs
* 2. Polls the request status to see if it has been successfully submitted to the network
*
* @param {string} from - the address where the ethereum transaction will be sent from (use the User Wallets Query to get your list of ethereum wallet addresses)
* @param {string} to - the address where the ethereum transaction that will be sent to
* @param {string} value - the amount to send in wei (integer string)
* @param {string} assetSymbol - the symbol of the asset to be sent (ETH|RX|HOT|DAI2|BAT|DAI|EMR|VXV|MKR|OMG|PPT|REP|USDT|LINK|CBE)
* @param {string} speed - "FAST" | "MEDIUM" | "SLOW"
* @returns {string} requestId - the unique identifier of the created ethereum transaction request needed for transaction tracking
*/
async function sendEthereum(from, to, value, assetSymbol, speed) {
// call createEthereumTransaction mutation with the parameters to get a well formed ethereum transaction
const result = await apiClient.createEthereumTransaction(from, to, value, assetSymbol, speed);
if (!result.signData || !result.requestId) {
console.error(`Failed to create ethereum transaction ${JSON.stringify(result)}`);
throw new Error("Failed to create ethereum transaction");
}
// IMPORTANT: PRODUCTION users are highly recommended to verify the ethereum transaction is what is expected (toAddress, amount, assetSymbol and digests are correct)
verifyEthereumTransaction(result.signData, from, to, value, assetSymbol);
// IMPORTANT: PRODUCTION users are highly recommended to NOT use the unverifiedDigestData but instead recreate the digests
// If your signing solution requires the pre-image data then use the `result.signData.unverifiedDigestData.signData`.
const signDigest = result.signData.unverifiedDigestData.shaSignData;
// using you private key pair secured in KMS, sign the digest.
// custom function. Use the sample code "sign" function and pull out the r,s values: https://github.com/Trustology/trustvault-nodejs-sdk/blob/master/src/ts/sign-examples/aws-kms.ts
const { r, s } = signAndReturnRandSSignature(kms, signDigest);
// create the signRequests payload
const signRequests = [
{
publicKeySignaturePairs: [
{
publicKey: newPublicKey, // should be in hex string format
signature: r.toString("hex", 64) + s.toString("hex", 64), // convert the r, s bytes signature to hex format
},
],
},
];
// submit the addSignature payload and receive back the requestId of your ethereum transaction request
const requestId = await apiClient.addSignature({
requestId: result.requestId,
signRequests,
});
// Check that your transaction was successfully submitted to the network
const expectedStatus = "SUBMITTED";
const status = await pollRequestStatus(requestId, expectedStatus);
console.info(`request (${requestId}) - status: ${status}`);
return requestId;
}