WIP (BlockchainLocal working)

Signed-off-by: James Zuccon <zuccon@gmail.com>
This commit is contained in:
James Zuccon
2024-09-12 21:33:11 +10:00
parent 52da32ad4d
commit e308206d9f
33 changed files with 1274 additions and 397 deletions
+4 -4
View File
@@ -31,7 +31,7 @@ export class Address {
/**
* Decode a CashAddr into an Address Entity.
*
* @param address {string} The CashAddr to decode.
* @param cashAddr The CashAddr to decode.
*
* @throws {Error} If address cannot be decoded.
*
@@ -71,7 +71,7 @@ export class Address {
/**
* Decode a Legacy Address into an Address Entity.
*
* @param address {string} The Legacy (Base58) Address to decode.
* @param legacyAddress The Legacy (Base58) Address to decode.
*
* @throws {Error} If address cannot be decoded.
*
@@ -128,7 +128,7 @@ export class Address {
/**
* Instantiate an address from Contract Bytecode.
*
* @param contractBytes {Uint8Array} The contract bytecode
* @param contractBytes The contract bytecode
*
* @throws {Error} If address cannot be instantiated from Contract Bytecode.
*
@@ -156,7 +156,7 @@ export class Address {
/**
* Instantiate an address from Contract Bytecode as a hex string.
*
* @param contractBytes {Uint8Array} The contract bytecode
* @param hex The contract bytecode as hex
*
* @throws {Error} If address cannot be instantiated from Contract Bytecode.
*
+1 -1
View File
@@ -30,7 +30,7 @@ export class Bytes {
/**
* Instantiates bytes from a Base64 string.
*
* @param utf8 {string} The Base64 string.
* @param base64 The Base64 string.
*
* @throws {Error} If Base64 is invalid.
*
+42 -24
View File
@@ -7,6 +7,7 @@ import {
deriveHdPath,
deriveHdPublicNode,
deriveHdPrivateNodeFromSeed,
deriveSeedFromBip39Mnemonic,
generateRandomBytes,
} from '@bitauth/libauth';
@@ -26,38 +27,16 @@ export class HDPrivateNode {
/**
* Construct a new HD Private Node.
*
* @param node {HdPrivateNodeValid} The HD Private Node.
* @param node The HD Private Node.
*/
constructor(node: HdPrivateNodeValid) {
this.node = node;
}
/**
* Creates a HD Private Node from the given XPriv Key.
*
* @param xpriv {string} The XPriv Key.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromXPriv(xpriv: string): HDPrivateNode {
// Attempt to decode the XPriv Key.
const decodeResult = decodeHdPrivateKey(xpriv);
// If a string is returned, this indicates an error...
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
// Return a new HD Private Node from the given XPriv.
return new HDPrivateNode(decodeResult.node);
}
/**
* Creates a HD Private Node from a raw seed.
*
* @param seed {Uint8Array} The raw seed bytes.
* @param seed The raw seed bytes.
*
* @throws {Error} If HD Private Node cannot be created.
*
@@ -83,6 +62,45 @@ export class HDPrivateNode {
return new HDPrivateNode(deriveResult);
}
/**
* Creates a HD Private Node from the given mnemonic.
*
* @param mnemonic The mnemonic.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromMnemonic(mnemonic: string): HDPrivateNode {
// Derive the seed from the mnemonic.
const seed = deriveSeedFromBip39Mnemonic(mnemonic);
// Return a new HDPrivateNode from the given seed.
return HDPrivateNode.fromSeed(seed);
}
/**
* Creates a HD Private Node from the given XPriv Key.
*
* @param xpriv The XPriv Key.
*
* @throws {Error} If HD Private Node cannot be created.
*
* @returns {HDPrivateNode} The created HD Private Node.
*/
public static fromXPriv(xpriv: string): HDPrivateNode {
// Attempt to decode the XPriv Key.
const decodeResult = decodeHdPrivateKey(xpriv);
// If a string is returned, this indicates an error...
if (typeof decodeResult === 'string') {
throw new Error(decodeResult);
}
// Return a new HD Private Node from the given XPriv.
return new HDPrivateNode(decodeResult.node);
}
/**
* Generates a random HD Private Node.
*
+14 -1
View File
@@ -44,6 +44,19 @@ export class PrivateKey {
this.bytes = bytes;
}
/**
* Create a Private Key Entity from the given bytes.
*
* @param privateKeyBytes {string} Raw Private Key bytes.
*
* @throws {Error} If Private Key could not be created.
*
* @returns {PrivateKey} The created Private Key Entity.
*/
public static fromBytes(privateKeyBytes: Uint8Array): PrivateKey {
return new PrivateKey(privateKeyBytes);
}
/**
* Create a Private Key Entity from the given bytes (as hexadecimal string).
*
@@ -232,7 +245,7 @@ export class PrivateKey {
*
* NOTE: Electron can verify this message successfully, but generates them differently internally.
*
* @param messageHash {Uint8Array} The hash of the message.
* @param message The message.
*
* @throws {Error} If message hash cannot be signed.
*
+21
View File
@@ -106,4 +106,25 @@ export class Transaction {
public getLocktime() {
return this.transaction.locktime;
}
public isCoinbase(): boolean {
// TODO: This definitely needs checking.
// A coinbase transaction MUST only have one input.
if (this.getInputs().length > 1) {
return false;
}
// Get the input's unlocking bytecode.
const unlockingBytecode = this.getInputs()[0].unlockingBytecode;
// Get the firt byte.
const firstByte = unlockingBytecode[0];
if (firstByte >= 0x01 && firstByte <= 0x4b) {
return true;
}
return false;
}
}
View File