Skip to content

Commit

Permalink
Egld KMS (#167)
Browse files Browse the repository at this point in the history
* Egld KMS

* Version

* Fix
  • Loading branch information
isra67 authored Oct 7, 2021
1 parent 7db64fa commit 69128f7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.27.1",
"version": "1.27.2",
"description": "Tatum API client allows browsers and Node.js clients to interact with Tatum API.",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
32 changes: 32 additions & 0 deletions src/model/request/egld/EgldTransferOffchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {IsInt, IsNotEmpty, IsOptional, IsUUID, Length, Max, Min, ValidateIf} from 'class-validator'
import {EgldSendTransaction} from './EgldSendTransaction'

export class EgldTransferOffchain extends EgldSendTransaction {
@Length(1, 500)
@ValidateIf(o => (o.mnemonic && o.index >= 0 && o.fromPrivateKey) || (o.index >= 0 && o.fromPrivateKey))
@IsNotEmpty()
public mnemonic?: string;

@ValidateIf(o => (o.mnemonic && o.index >= 0 && o.fromPrivateKey) || o.mnemonic)
@Min(0)
@IsOptional()
@IsInt()
@Max(2147483647)
public index?: number;

@ValidateIf(o => (o.mnemonic && o.index >= 0 && o.fromPrivateKey) || (!o.mnemonic && !o.index))
@Length(66, 66)
@IsNotEmpty()
public fromPrivateKey?: string;

@ValidateIf(o => o.signatureId)
@Length(34, 34)
@IsNotEmpty()
public from?: string;

@ValidateIf(o => !o.mnemonic && !o.fromPrivateKey)
@Length(36, 36)
@IsUUID('4')
@IsNotEmpty()
public signatureId?: string;
}
1 change: 1 addition & 0 deletions src/model/request/egld/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './EgldSendTransaction'
export * from './EgldEsdtTransaction'
export * from './EgldBasicTransaction'
export * from './EgldTransferOffchain'
export * from './EsdtToken'
export * from './EsdtIssue'
export * from './EsdtTransfer'
Expand Down
25 changes: 14 additions & 11 deletions src/offchain/egld.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import BigNumber from 'bignumber.js';
import {validateBody} from '../connector/tatum';
import {Currency, TransferEthOffchain} from '../model';
import {Currency, EgldTransferOffchain} from '../model';
import {prepareEgldSignedTransaction} from '../transaction';
import {generatePrivateKeyFromMnemonic} from '../wallet';
import {offchainBroadcast, offchainCancelWithdrawal, offchainStoreWithdrawal} from './common';
import { offchainTransferEgldKMS } from './kms'

/**
* Send EGLD transaction from Tatum Ledger account to the blockchain. This method broadcasts signed transaction to the blockchain.
Expand All @@ -13,25 +14,27 @@ import {offchainBroadcast, offchainCancelWithdrawal, offchainStoreWithdrawal} fr
* @param provider url of the EGLD Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain or id of the withdrawal, if it was not cancelled automatically
*/
export const sendEgldOffchainTransaction = async (testnet: boolean, body: TransferEthOffchain, provider?: string) => {
await validateBody(body, TransferEthOffchain)
export const sendEgldOffchainTransaction = async (testnet: boolean, body: EgldTransferOffchain, provider?: string) => {
if (body.signatureId) {
return offchainTransferEgldKMS(body)
}
await validateBody(body, EgldTransferOffchain)
const {
mnemonic, index, privateKey, gasLimit, gasPrice, nonce, ...withdrawal
mnemonic, index, fromPrivateKey, gasLimit, gasPrice, ...withdrawal
} = body
const {amount, address} = withdrawal
const {value, receiver} = withdrawal

const fromPriv = mnemonic && index !== undefined ? await generatePrivateKeyFromMnemonic(Currency.EGLD, testnet, mnemonic, index) : privateKey as string
const fromPriv = mnemonic && index !== undefined ? await generatePrivateKeyFromMnemonic(Currency.EGLD, testnet, mnemonic, index) : fromPrivateKey as string

const fee = {
gasLimit: gasLimit || '50000',
gasPrice: gasPrice || '1000000000',
gasLimit: `${gasLimit || '50000'}`,
gasPrice: `${gasPrice || '1000000000'}`,
}
const txData = await prepareEgldSignedTransaction({
amount,
amount: value,
fromPrivateKey: fromPriv,
fee,
to: address,
nonce
to: receiver
}, provider)
// @ts-ignore
withdrawal.fee = new BigNumber(fee.gasLimit).multipliedBy(fee.gasPrice).toString()
Expand Down
8 changes: 7 additions & 1 deletion src/offchain/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TransferBtcBasedOffchainKMS} from '../model/request/TransferBtcBasedOffc
import {TransferEthOffchainKMS} from '../model/request/TransferEthOffchainKMS'
import {TransferXlmOffchainKMS} from '../model/request/TransferXlmOffchainKMS'
import {TransferXrpOffchainKMS} from '../model/request/TransferXrpOffchainKMS'
import {EgldTransferOffchain} from '../model/request/egld/'

/**
* For more details, see <a href="https://tatum.io/apidoc#operation/BtcTransfer" target="_blank">Tatum API documentation</a>
Expand Down Expand Up @@ -83,9 +84,14 @@ export const offchainTransferPolygonKMS = async (body: TransferEthOffchainKMS):
export const offchainTransferTronKMS = async (body: TransferTrxOffchain): Promise<SignatureId> =>
post(`/v3/offchain/tron/transfer`, body, TransferTrxOffchain)


/**
* For more details, see <a href="https://tatum.io/apidoc#operation/XdcTransfer" target="_blank">Tatum API documentation</a>
*/
export const offchainTransferXdcKMS = async (body: TransferEthOffchainKMS): Promise<SignatureId> =>
post(`/v3/offchain/xdc/transfer`, body, TransferEthOffchainKMS)

/**
* For more details, see <a href="https://tatum.io/apidoc#operation/XdcTransfer" target="_blank">Tatum API documentation</a>
*/
export const offchainTransferEgldKMS = async (body: EgldTransferOffchain): Promise<SignatureId> =>
post(`/v3/offchain/egld/transfer`, body, EgldTransferOffchain)
4 changes: 4 additions & 0 deletions src/transaction/egld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ const prepareSignedTransactionAbstraction = async (

erdjsTransaction.setGasLimit(new GasLimit(await egldGetGasLimit(egldTx)))

if (signatureId) {
return JSON.stringify(erdjsTransaction)
}

return await signEgldTransaction(erdjsTransaction, fromPrivateKey as string)
}

Expand Down

0 comments on commit 69128f7

Please sign in to comment.