yarn add @smartholdem/crypto
const { Identities } = require("@smartholdem/crypto");
// Throughout this document, the keys object used is:
const keys = Identities.Keys.fromPassphrase("this is a top-secret passphrase");
// Throughout this document, the recipientId variable used is:
const recipientId = Identities.Address.fromPassphrase("this is a top-secret passphrase");
// Throughout this document, the senderPublicKey variable used is:
const senderPublicKey = Identities.PublicKey.fromPassphrase("this is a top-secret passphrase");
A transaction is an object specifying the transfer of funds from the sender’s wallet to the recipient’s. Each transaction must be signed by the sender’s private key to prove authenticity and origin. After broadcasting through the client SDK, a transaction is permanently incorporated in the blockchain by a Delegate Node.
The crypto SDK can sign a transaction using your private key or passphrase (from which the private key is generated). Ensure you are familiar with digital signatures before using the crypto SDKs.
const { Transactions } = require("@smartholdem/crypto");
const transaction = {
type: 0,
amount: 200000000,
fee: 100000000,
recipientId,
timestamp: 121212,
asset: {},
senderPublicKey
};
Transactions.Signer.sign(transaction, keys);
>>> string
Serialization of a transaction object ensures it is compact and properly formatted to be incorporated in the SmartHoldem blockchain. If you are using the crypto SDK in combination with the public API SDK, you should not need to serialize manually.
const { Transactions } = require("@smartholdem/crypto");
const transaction = Transactions.BuilderFactory
.transfer()
.amount(1000)
.fee(100000000)
.recipientId(recipientId)
.senderPublicKey(senderPublicKey)
.sign("sender")
.build();
const serialized = Transactions.Serializer.serialize(transaction).toString("hex");
>>> string
A serialized transaction may be deserialized for inspection purposes. The public API does not return serialized transactions, so you should only need to deserialize in exceptional circumstances.
const { Transactions } = require("@smartholdem/crypto");
const deserialized = Transactions.deserializer.deserialize(serialized);
>>> ITransaction
The crypto SDK not only supports transactions but can also work with other arbitrary data (expressed as strings).
Signing a string works much like signing a transaction: in most implementations, the message is hashed, and the resulting hash is signed using the
private keyorpassphrase.
const { Crypto } = require("@smartholdem/crypto");
const message = "Arbitrary entry of data";
const hash = Crypto.HashAlgorithms.sha256(message);
const signature = Crypto.Hash.signECDSA(hash, keys);
const signed = {
message,
hash,
signature
};
>>> IMessage
const { Crypto } = require("@smartholdem/crypto");
const message = "Arbitrary entry of data";
const hash = Crypto.HashAlgorithms.sha256(message);
const signature = Crypto.Hash.signSchnorr(hash, keys);
const signed = {
message,
hash,
signature
};
>>> IMessage
A message’s signature can easily be verified by hash, without the private key that signed the message, by using the
verifymethod.
Crypto.Hash.verifyECDSA(
signed.hash,
signed.signature,
"72b45a1978dd7669470ba67abbe5c220062924380c9c364b"
);
>>> boolean
Crypto.Hash.verifySchnorr(
signed.hash,
signed.signature,
"72b45a1978dd7669470ba67abbe5c220062924380c9c364b"
);
>>> boolean
The identities class allows for the creation and inspection of keyPairs from
passphrases. Here you find vital functions when creating transactions and managing wallets.
const { Identities } = require("@smartholdem/crypto");
Identities.Address.fromPassphrase("this is a top secret passphrase");
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.Address.fromPublicKey(
"validPublicKey"
);
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.Address.fromPrivateKey(
"validPrivateKey"
);
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.Address.fromWIF(
"validWif"
);
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.Address.validate("validAddress");
>>> boolean
As the name implies, private keys and passphrases are to remain private. Never store these unencrypted and minimize access to these secrets
const { Identities } = require("@smartholdem/crypto");
Identities.PrivateKey.fromPassphrase("this is a top secret passphrase");
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.PrivateKey.fromWIF(
"validWif"
);
>>> string
Public Keys may be freely shared, and are included in transaction objects to validate the authenticity.
const { Identities } = require("@smartholdem/crypto");
Identities.PublicKey.fromPassphrase("this is a top secret passphrase");
>>> string
const { Identities } = require("@smartholdem/crypto");
Identities.PublicKey.validate(
"validPublicKey"
);
>>> boolean
The WIF should remain secret, just like your
passphraseandprivate key.
const { Identities } = require("@smartholdem/crypto");
Identities.WIF.fromPassphrase("this is a top secret passphrase");
>>> string