UNPKG

@payburner/keyburner-core

Version:

KeyburnerJs -- A library to enable arbitrary transaction signing and verification using XRPL-compatible keys and addresses.

64 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KeyBurner = void 0; const KeyPairsApi = require('ripple-keypairs'); const addressCodecApi = require('ripple-address-codec'); const tweetnacl_1 = require("tweetnacl"); const hashes_1 = require("../hashes"); class KeyBurner { generateSeed() { return KeyPairsApi.generateSeed({ entropy: tweetnacl_1.randomBytes(16), algorithm: 'ed25519' }); } deriveKeyPair(seed) { return KeyPairsApi.deriveKeypair(seed); } deriveAddress(keypair) { const publicKey = keypair.publicKey; return KeyPairsApi.deriveAddress(publicKey); } hexAddress(address) { return this.toHex(addressCodecApi.decodeAccountID(address)); } toHex(bytes) { return Buffer.from(bytes).toString('hex').toUpperCase(); } encodePayload(payload) { return Buffer.from(JSON.stringify(payload)).toString('hex').toUpperCase(); } decodePayload(base64) { return JSON.parse(Buffer.from(base64, 'hex').toString('utf-8')); } signTransaction(payload, keypair) { payload['SigningPubKey'] = keypair.publicKey; const preSigned = this.encodePayload(payload); const signed = KeyPairsApi.sign(preSigned, keypair.privateKey); payload['TxnSignature'] = signed; const pl = this.encodePayload(payload); const txId = hashes_1.transactionID(Buffer.from(pl, 'hex')); return { id: txId.toString(), signedTransaction: pl }; } decodeTransaction(signedPayloadInHex) { const decoded = this.decodePayload(signedPayloadInHex); const signingPubKey = decoded.SigningPubKey; const derivedAddress = KeyPairsApi.deriveAddress(signingPubKey); const signature = decoded.TxnSignature; delete decoded.TxnSignature; delete decoded.Signers; const encoded = this.encodePayload(decoded); const verified = KeyPairsApi.verify(encoded, signature, signingPubKey); delete decoded.SigningPubKey; return { id: hashes_1.transactionID(Buffer.from(signedPayloadInHex, 'hex')).toString(), raw: signedPayloadInHex, verified: verified, address: derivedAddress, signingKey: signingPubKey, signature: signature, payload: decoded }; } } exports.KeyBurner = KeyBurner; //# sourceMappingURL=KeyBurner.js.map