As descriped before, this library uses SubtleCrypto to perform it's cryptographic finctions. It needs to be supported on your platform in order for you to use it.
First thing you need in order to make your first transaction on TRINCI platform is a pair of ECDSA secp384r1 cryptographic keys.
Let's create an empty key pair:
const ecdsaKeyPair = new T2lib.ECDSAKeyPair();
ECDSAKeyPair()
is just a wrapper class for T2lib.BaseECKeyPair()
, which is a base elliptic curve keypair class, created with T2lib.CryptoDefaults.ECDSAP384R1KeyPairParams
parameters.
All standard key parameters are stored in T2lib.CryptoDefaults
object. Use IntelliSense to expore it.
In order to achieve the same effect as that of the code above we also could've done this:
const keyPairParams = T2lib.CryptoDefaults.ECDSAP384R1KeyPairParams;
const ecdsaKeyPair = new T2lib.BaseECKeyPair(keyPairParams);
Both keypair class and parameters object can further be split in private and public key classes/parameters like in example below. See your IntelliSense and/or detailed lib api documentation generated by jsdoc for further details.
const privKey1 = new T2lib.BaseECKey(T2lib.CryptoDefaults.ECDSAP384R1PrivKeyParams);
// or
const privKey2 = new T2lib.ECDSAKey('private');
const pubKey1 = new T2lib.BaseECKey(T2lib.CryptoDefaults.ECDSAP384R1PubKeyParams);
// or
const pubKey2 = new T2lib.ECDSAKey('public');
After a new KeyPair class object has been created with correct set of parameters, actual keys can be generated in the following manner:
ecdsaKeyPair.generate()
.then(() => {
// do something
})
.catch();
.generate()
method is only available for key pairs
Now you can access individual keys:
const pubKey = ecdsaKeyPair.publicKey;
const privKey = ecdsaKeyPair.privateKey;
const ecdsaKeyPair2 = new T2lib.ECDSAKeyPair();
ecdsaKeyPair2.publicKey = pubKey;
ecdsaKeyPair2.privateKey = privKey;
Elliptic curve keys can be exported in a number of formats using key's methods like:
// only public
ecdsaKeyPair.publicKey.getRaw();
ecdsaKeyPair.publicKey.getSPKI();
// only private
ecdsaKeyPair.privateKey.getPKCS8();
// both
ecdsaKeyPair.privateKey.getCryptoKey();
ecdsaKeyPair.privateKey.getJWK();
In the same way keys can be imported with corresponding "set" methods:
// only public
ecdsaKeyPair.publicKey.setRaw();
ecdsaKeyPair.publicKey.setSPKI();
// only private
ecdsaKeyPair.privateKey.setPKCS8();
// both
ecdsaKeyPair.privateKey.setCryptoKey();
ecdsaKeyPair.privateKey.setJWK();
ecdsaKeyPair.privateKey.importBin(); // detects which import format to use
Binary data returned by some of te methods mentioned above can be converted to/from more storage-friendly formats using methods in
T2lib.binConversions
Also imported private ECDSA keys can be used to derive a public key with the following method:
privKey1.extractPublic()
.then((publicKey) => {
// do something
})
.catch();