-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgariHelper.js
68 lines (57 loc) · 2.14 KB
/
gariHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const web3 = require('@solana/web3.js')
const { findThisPublicKey, } = require('./api.js')
const { sdkValidate } = require('./sdkInitialize.js')
/**
*
* @param {string} encodedTransction - base64 string of transaction instruction
* @returns
*/
function getDecodedTransction(encodedTransction) {
let encodedTransctionInBuffer = Buffer.from(encodedTransction, 'base64'); // get encoded buffer
return web3.Transaction.from(encodedTransctionInBuffer);
}
/**
*
* @param {string} transactionDetails - base64 string of transaction
* @param {string} privateKey - privateKey of user : it should be in hex format
* @param {feepayerWalletPrivateKey} feepayerWalletPrivateKey - feepayer wallet for final signature on transaction
* @returns
*/
function partialSign(transactionDetails, privateKey, feepayerWalletPrivateKey=undefined) {
// hex is used bcoz web3auth provides privatekey in hex format
if(privateKey) {
let signerWallet = web3.Keypair.fromSecretKey(Buffer.from(privateKey, "hex"));
transactionDetails.partialSign(...[signerWallet]);
}
if(feepayerWalletPrivateKey) {
let feepayerWallet = web3.Keypair.fromSecretKey(Buffer.from(feepayerWalletPrivateKey, "hex"));
transactionDetails.partialSign(...[feepayerWallet]);
}
const wireTransaction = transactionDetails.serialize({
requireAllSignatures: true,
verifySignatures: false,
});
return wireTransaction.toString('base64');
}
/**
*
* @param {string} publickey - publickey of the user which we need to verify
* @returns
*/
async function verifyPublicKey(publicKey, jwtToken) {
try {
const validate = sdkValidate()
if (!validate) {
throw new Error(`sdk not initialized`)
}
let receiverPublicKey;
const {data} = await findThisPublicKey(publicKey, jwtToken);
if(data.data && data.data.publicKey) {
receiverPublicKey = data.data.publicKey;
}
return receiverPublicKey;
} catch (error) {
console.log('error in verify publickey function', error);
}
}
module.exports = { partialSign, getDecodedTransction, verifyPublicKey }