Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cryptoapis-kms

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cryptoapis-kms

Crypto APIs - Key Management System

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Crypto APIs - Key Management System Tool (KMS)

Crypto APIs KMS (Key Management System) is an open-source Node.js library. It gives companies full custody of master private keys, master seeds, and mnemonics. The library allows businesses to create HD wallets (xPubs) and sign transactions locally without a network connection (offline). It can be used in combination with Crypto APIs product suite for syncing xPub, deriving wallet addresses, listing wallet addresses, getting fee recommendations, preparing the transaction with the right data, broadcasting locally signed transactions. The KMS is perfect for B2C companies, including hardware wallets and digital wallets, as well as custodial or non-custodial exchanges. By using Crypto API's open-source library, they can easily scale to satisfy the demand and create wallets for millions of users. The businesses can decide whether to hold custody of their clients' master keys, master seed, and mnemonic or give them to their customers instead.

  • Package version: 0.2.0
  • For more information, please visit https://cryptoapis.io
  • minimum requirement NodeJS >= 14.0

Installation

For Node.js

npm

Then install it via:

npm install cryptoapis-kms

Usage

generateAddress

This method generates and returns a new public and private key pair, and the associated address with it.

Example

 const { Enumerations, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;

 const addressService = new Services.AddressService(blockchain, network)
 const address = addressService.generateAddress();
 console.dir('New address generated successfully. Returned data:');
 console.dir(address)
 console.dir(address.address)
 console.dir(address.privateKey)
 console.dir(address.publicKey)

Return type

AddressDTO

createHDWallet (xPub, yPub, zPub)

This method generates a new HD Wallet for a specific blockchain and network. The response from the endpoint should be stored, otherwise the data is lost and cannot be recovered.

Example

 const { Enumerations, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;

 (async () => {
   const walletService = new Services.WalletService(blockchain, network);
   const wallet = await walletService.createHDWallet().then((data) => {
         console.dir('HD Wallet created successfully. Returned data:');
         console.dir(data);
         console.dir(data.xPub.accountXpriv);
         console.dir(data.xPub.accountXpub);
   }, (error) => {
      console.log(error)
   });
 })();

Parameters

NameTypeDescriptionNotes
mnemonicWordsCountNumberMnemonic words count. Possible values are 12(default), 18 or 24.[optional]

Return type

WalletDTO

syncNewHDWallet (xPub, yPub, zPub)

After initial sync we keep updating the synced xpub all the time.

Example

 const {Enumerations, Client, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';

 client.syncNewHDWallet(xPub).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
    console.log(error);
 });

Parameters

NameTypeDescriptionNotes
xPubStringDefines the account extended publicly known key which is used to derive all child public keys.
contextStringIn batch situations the user can use the context to correlate responses with requests. This property is present regardless of whether the response was successful or returned as an error. `context` is specified by the user.[optional]

Return type

HDWalletDTO

Authorization

ApiKey

deriveAndSyncNewChangeAddresses

Through this endpoint users can derive 100 change addresses, starting from the last index we have data for, which are then added to the xPub, subscribed for syncing, and start recording data. If no data is available, it will start from index 0.

Example

 const {Enumerations, Client, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';

 client.deriveAndSyncNewChangeAddresses(xPub).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
    console.log(error);
 });

Parameters

NameTypeDescriptionNotes
xPubStringDefines the account extended publicly known key which is used to derive all child public keys.
contextStringIn batch situations the user can use the context to correlate responses with requests. This property is present regardless of whether the response was successful or returned as an error. `context` is specified by the user.[optional]

Return type

HDWalletDTO

Authorization

ApiKey

deriveAndSyncNewReceivingAddresses

Through this endpoint users can derive 100 receiving addresses, starting from the last index we have data for, which are then added to the xPub, subscribed for syncing, and start recording data. If no data is available, it will start from index 0.

Example

 const {Enumerations, Client, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';

 client.deriveAndSyncNewReceivingAddresses(xPub).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
    console.log(error);
 });

Parameters

NameTypeDescriptionNotes
xPubStringDefines the account extended publicly known key which is used to derive all child public keys.
contextStringIn batch situations the user can use the context to correlate responses with requests. This property is present regardless of whether the response was successful or returned as an error. `context` is specified by the user.[optional]

Return type

HDWalletDTO

Authorization

ApiKey

listSyncedAddresses

Through this endpoint users can list all addresses that Crypto APIs has synced for a specific xPub. This includes previous and current/new xPubs, what addresses we’ve synced for them, etc.

Example

 const {Enumerations, Client, Services } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';
 const opts = {
            context: 'yourExampleString',
            addressFormat: "P2WPKH",
            isChangeAddress: true,
            limit: 15,
       };
 client.listSyncedAddresses(xPub, opts).then((data) => {
      console.dir('API called successfully. Returned data:');
      console.dir(data);
 }, (error) => {
      console.log(error);
 });

Parameters

NameTypeDescriptionNotes
xPubStringDefines the account extended publicly known key which is used to derive all child public keys.
optsObjectOptional parameters[optional]
opts.contextStringIn batch situations the user can use the context to correlate responses with requests. This property is present regardless of whether the response was successful or returned as an error. `context` is specified by the user.[optional]
opts.addressFormatStringDefines if the address is change addres or not. (default to true)[optional]
opts.isChangeAddressStringRepresents the format of the address[optional]
opts.limitStringDefines how many items should be returned in the response per page basis.[optional]
opts.offsetStringThe starting index of the response items, i.e. where the response should start listing the returned items[optional]

Return type

ListSyncedAddressesDTO

Authorization

ApiKey

prepareUTXOBasedTransactionFromHDWallet (xPub, yPub, zPub)

Through the “Prepare a UTXO-based transaction from HD Wallet” endpoint users can prepare a transaction for signing from all synced with Crypto APIs addresses for the specific xPub. This is based on the selectionStrategy and the addresses’ balances. In the case a user has an address not synced with Crypto APIs, it will not be included. This endpoint applies to all supported UTXO-based blockchain protocols, e.g. Bitcoin, Litecoin, etc.

Example

 const {Enumerations, Client, Services, Models } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = "xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB"
 const feeOptions = new Models.UTXOBasedFeeOptionsModel({
    prepareStrategy: Enumerations.PrepareStrategies.MINIMIZE_DUST,
    priority: Enumerations.FeePriorities.FAST,
 });
 const recipients = [ 
     new Models.RecipientModel("tb1q8qrk9pxkjcuk4a29ec7snskaxll55jzfhrcq24", '0.000031')
 ];

 const preparedUTXOTransaction = await client.prepareUTXOBasedTransactionFromHDWallet({
     xPub: xPub,
     recipients: recipients,
     feeOptions,
 }).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
     console.log(error)
 })

Parameters

NameTypeDescriptionNotes
xPubStringAccount Extended Public Key
recipientsArray<RecipientModel>Represents a list of recipient addresses with the respective amounts
feeOptionsUTXOBasedFeeOptionsRepresents the fee options
feeOptions.addressstringRepresents the fee address[optional]
feeOptions.prioritystringRepresents the fee priority[optional]
feeOptions.feeAmountstringRepresents the fee amount[optional]
locktimeNumberRepresents the time at which a particular transaction can be added to the blockchain[optional]
replaceableBooleanRepresentation of whether the transaction is replaceable[optional]
datastringRepresentation of the additional data[optional]

Return type

UTXOBasedTransactionDTO

Authorization

ApiKey

prepareAccountBasedTransactionFromHDWallet (xPub, yPub, zPub)

Through the “Prepare an account-based transaction from HD Wallet” endpoint users can prepare a transaction for signing from a synced with Crypto APIs address from the specific xPub. This endpoint applies to all supported account-based blockchain protocols, e.g. Ethereum, BSC, etc

Example

 const {Enumerations, Client, Services, Models } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.ETHEREUM;
 const network = Enumerations.Networks[blockchain].NETWORK_ETHEREUM_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const xPub = "xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB
 const sender = '0x0b7155094947d785530f66d250b097b25c30a557';
 const recipient = '0xd4e2a5949359e95c7c604050dd9d54af419689c0';
 const amount = '1.2123';
 const feeOptions = new Models.AccountBasedFeeOptionsModel({
    priority: Enumerations.FeePriorities.FAST,
 });
 const preparedAccountTransaction = await client.prepareAccountBasedTransactionFromHDWallet({
     xPub,
     sender,
     recipient,
     amount,
     feeOptions
 }).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
     console.log(error)
 })

Parameters

NameTypeDescriptionNotes
xPubstringAccount Extended Public Key
senderstringRepresents a sender address
recipientstringRepresents a recipient addresses
amountstringRepresentation of the amount of the transaction
feeOptionsUTXOBasedFeeOptionsRepresents the fee options
feeOptions.prioritystringRepresents the fee priority[optional]
feeOptions.feeAmountstringRepresents the fee amount[optional]
noncestringRepresentation of the nonce value[optional]
datastringRepresentation of the additional data[optional]

Return type

AccountBasedTransactionDTO

Authorization

ApiKey

signPreparedTransactionLocally

Through this endpoint users sign their transactions locally(offline) using the transaction response from Prepare Transaction From HD Wallet endpoint, both for account-based and UTXO-based

Example

const {Enumerations, Client, Services, Models} = require('cryptoapis-kms');
const blockchain = Enumerations.Blockchains.BITCOIN;
const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
const client = new Client('YOUR API KEY', blockchain, network);
const signService = new Services.SignService(blockchain, network)
const accountXpriv = 'xprv8gdau6KURKnX7mcKNjLMWx3a3tEzHCMiJDBtFCJrvmXCsHNj3wvSuJ3T8g67WvN9hkFa4y1Mnr9ZbyUzs9fdhi8mhegLufkEuwSdmDeBXvz';
const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';

const preparedUTXO = await client.prepareUTXOBasedTransactionFromHDWallet({
    xPub: xPub,
    recipients: [
        new Models.RecipientModel("tb1q8qrk9pxkjcuk4a29ec7snskaxll55jzfhrcq24", '0.000031')
    ],
    feeOptions: new Models.UTXOBasedFeeOptionsModel({
        prepareStrategy: Enumerations.PrepareStrategies.MINIMIZE_DUST,
        priority: Enumerations.FeePriorities.FAST,
    })
});

const signedTx = signService.signPreparedTransactionLocally(accountXpriv, preparedUTXO);
const callbackSecretKey = 'yourSecretString';
const callbackUrl = 'https://example.com'; // your URL for callback must be verifyed from dashboard  

client.broadcastSignedTx(signedTx.raw, callbackSecretKey, callbackUrl).then((data) => {
    console.dir('API called successfully. Returned data:');
    console.dir(data);
}, (error) => {
    console.log(error);
});

Parameters

NameTypeDescriptionNotes
accountXprivStringAccount Extended Private Key
transactionTransactionDTOPrepared Transaction From Xpub (Account-based or UTXO-based)

Return type

SignDTO

Authorization

ApiKey

broadcastSignedTx

broadcast locally signed transaction

Example

 const {Enumerations, Client, Services, Models } = require('cryptoapis-kms');
 const blockchain = Enumerations.Blockchains.BITCOIN;
 const network = Enumerations.Networks[blockchain].NETWORK_BITCOIN_MAINNET;
 const client = new Client('YOUR API KEY', blockchain, network);
 const signService = new Services.SignService(blockchain, network)
 const accountXpriv = 'xprv8gdau6KURKnX7mcKNjLMWx3a3tEzHCMiJDBtFCJrvmXCsHNj3wvSuJ3T8g67WvN9hkFa4y1Mnr9ZbyUzs9fdhi8mhegLufkEuwSdmDeBXvz';
 const xPub = 'xpub6BsFsonVJR5vPChKQamp55R7veBCMD2CL3LtL83B3FS5DiayYgmoHCGQodeLTukaa4anZRQD9kNtPFHuPnCzjCiT9nrXdf3voNLhXQryBRB';

 const preparedUTXO = await client.prepareUTXOBasedTransactionFromHDWallet({
     xPub: xPub,
     recipients: [
         new Models.RecipientModel("tb1q8qrk9pxkjcuk4a29ec7snskaxll55jzfhrcq24", '0.000031')
     ],
     feeOptions: new Models.UTXOBasedFeeOptionsModel({
         prepareStrategy: Enumerations.PrepareStrategies.MINIMIZE_DUST,
         priority: Enumerations.FeePriorities.FAST,
     })
 });

 const signedTx = signService.signPreparedTransactionLocally(accountXpriv, preparedUTXO);
 const callbackSecretKey = 'yourSecretString';
 const callbackUrl = 'https://example.com'; // your URL for callback must be verifyed from dashboard  
 
 client.broadcastSignedTx(signedTx.raw, callbackSecretKey, callbackUrl).then((data) => {
     console.dir('API called successfully. Returned data:');
     console.dir(data);
 }, (error) => {
     console.log(error);
 });

Parameters

NameTypeDescriptionNotes
signedTransactionHexStringString identifier of the transaction
callbackSecretKeyStringRepresents the Secret Key value provided by the customer. This field is used for security purposes during the callback notification, in order to prove the sender of the callback as Crypto APIs
callbackUrlStringRepresents the URL that is set by the customer where the callback will be received at. The callback notification will be received only if and when the event occurs.
contextStringIn batch situations the user can use the context to correlate responses with requests. This property is present regardless of whether the response was successful or returned as an error. `context` is specified by the user.[optional]

Return type

BroadcastSignedTxDTO

Authorization

ApiKey

ApiKey

  • Type: API key
  • API key parameter name: x-api-key
  • Location: HTTP header

Contributing

Anyone who wants to contribute is welcome. Use the Issues as a way to make suggestions that you want to see or even want to create.

FAQs

Package last updated on 31 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc