Resolve "feat(Primitives): Have Bytes Primitive extends Uint8Array (if possible)"
This commit is contained in:
@@ -209,7 +209,7 @@ export class Address {
|
||||
type: 'P2SH20' | 'P2SH32' = 'P2SH32',
|
||||
): Address {
|
||||
// Return a new instance of address.
|
||||
return this.fromRedeemScript(Bytes.fromHex(hex).toBytes(), type);
|
||||
return this.fromRedeemScript(Bytes.fromHex(hex).toUint8Array(), type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,7 +257,7 @@ export class Address {
|
||||
* @returns {Address} Instance of Address.
|
||||
*/
|
||||
public static fromLockscriptHex(hex: string): Address {
|
||||
return this.fromLockscriptBytes(Bytes.fromHex(hex).toBytes());
|
||||
return this.fromLockscriptBytes(Bytes.fromHex(hex).toUint8Array());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,7 +298,10 @@ export class Address {
|
||||
hash160Hex: string,
|
||||
type: AddressType = 'P2PKH',
|
||||
): Address {
|
||||
return this.fromHash160Bytes(Bytes.fromHex(hash160Hex).toBytes(), type);
|
||||
return this.fromHash160Bytes(
|
||||
Bytes.fromHex(hash160Hex).toUint8Array(),
|
||||
type,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -73,7 +73,7 @@ export class BlockHeader {
|
||||
* @throws Error if the hex string length is not 160
|
||||
*/
|
||||
static fromHex(hex: string): BlockHeader {
|
||||
return this.fromBytes(Bytes.fromHex(hex).toBytes());
|
||||
return this.fromBytes(Bytes.fromHex(hex).toUint8Array());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,7 +119,7 @@ export class BlockHeader {
|
||||
* @returns 160-character hex string representing the block header
|
||||
*/
|
||||
toHex(): string {
|
||||
return Bytes.fromBytes(this.toBytes()).toHex();
|
||||
return Bytes.fromBytes(this.bytes).toHex();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +207,7 @@ export class BlockHeader {
|
||||
* @returns 32-byte array containing the block hash
|
||||
*/
|
||||
getHash(): Uint8Array {
|
||||
return hash256(this.toBytes()).toReversed();
|
||||
return hash256(this.bytes).toReversed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,16 +15,19 @@ import {
|
||||
/**
|
||||
* Bytes Primitive.
|
||||
*/
|
||||
export class Bytes {
|
||||
protected readonly bytes: Uint8Array;
|
||||
|
||||
export class Bytes extends Uint8Array {
|
||||
/**
|
||||
* Instantiate Bytes from a Uint8Array.
|
||||
*
|
||||
* @param bytes {Uint8Array} The bytes to use.
|
||||
*/
|
||||
protected constructor(bytes: Uint8Array) {
|
||||
this.bytes = bytes;
|
||||
super(bytes);
|
||||
}
|
||||
|
||||
// Override the constructor to ensure correct inheritance
|
||||
static from(uint8Array: Uint8Array): Bytes {
|
||||
return new Bytes(uint8Array);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,11 +122,11 @@ export class Bytes {
|
||||
* @returns Bytes as a Base64 string.
|
||||
*/
|
||||
public toBase64(): string {
|
||||
return binToBase64(this.bytes);
|
||||
return binToBase64(this);
|
||||
}
|
||||
|
||||
public toBigInt(): bigint {
|
||||
return binToBigIntUintLE(this.bytes);
|
||||
return binToBigIntUintLE(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +135,7 @@ export class Bytes {
|
||||
* @returns Bytes as a hex string.
|
||||
*/
|
||||
public toHex(): string {
|
||||
return binToHex(this.bytes);
|
||||
return binToHex(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +144,7 @@ export class Bytes {
|
||||
* @returns Bytes as a UTF8 string.
|
||||
*/
|
||||
public toUtf8(): string {
|
||||
return binToUtf8(this.bytes);
|
||||
return binToUtf8(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,11 +152,12 @@ export class Bytes {
|
||||
*
|
||||
* @returns Bytes as a Uint8Array.
|
||||
*/
|
||||
public toBytes(): Uint8Array {
|
||||
return this.bytes;
|
||||
public toUint8Array(): Uint8Array {
|
||||
// NOTE: We clone the instance of Uint8Array so that we don't accidentally mutate it.
|
||||
return new Uint8Array([...this]);
|
||||
}
|
||||
|
||||
public invert(): Bytes {
|
||||
return new Bytes(Uint8Array.from([...this.bytes].reverse()));
|
||||
return new Bytes(Uint8Array.from([...this].reverse()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ export class HDPrivateNode {
|
||||
public static fromSeed(seed: Uint8Array | string): HDPrivateNode {
|
||||
// Cast from hex if a string was provided.
|
||||
if (typeof seed === 'string') {
|
||||
seed = Bytes.fromHex(seed).toBytes();
|
||||
seed = Bytes.fromHex(seed);
|
||||
}
|
||||
|
||||
// If the seed is not 64 bytes, throw an error.
|
||||
|
||||
@@ -98,7 +98,7 @@ export class PrivateKey {
|
||||
*/
|
||||
public static fromHex(privateKeyHex: string): PrivateKey {
|
||||
// Convert the hex to binary (Uint8Array).
|
||||
const privateKeyBin = Bytes.fromHex(privateKeyHex).toBytes();
|
||||
const privateKeyBin = Bytes.fromHex(privateKeyHex);
|
||||
|
||||
// Create a Private Key Entity from the binary.
|
||||
return new this(privateKeyBin);
|
||||
@@ -268,7 +268,7 @@ export class PrivateKey {
|
||||
*/
|
||||
public signMessageRecoverableCompact(message: Uint8Array | string) {
|
||||
if (typeof message === 'string') {
|
||||
message = Bytes.fromUtf8(message).toBytes();
|
||||
message = Bytes.fromUtf8(message);
|
||||
}
|
||||
|
||||
const messageHash = sha256.hash(message);
|
||||
@@ -292,7 +292,7 @@ export class PrivateKey {
|
||||
*/
|
||||
public signMessageCompact(message: Uint8Array | string) {
|
||||
if (typeof message === 'string') {
|
||||
message = Bytes.fromUtf8(message).toBytes();
|
||||
message = Bytes.fromUtf8(message);
|
||||
}
|
||||
|
||||
const messageHash = sha256.hash(message);
|
||||
@@ -316,7 +316,7 @@ export class PrivateKey {
|
||||
*/
|
||||
public signMessageSchnorr(message: Uint8Array | string) {
|
||||
if (typeof message === 'string') {
|
||||
message = Bytes.fromUtf8(message).toBytes();
|
||||
message = Bytes.fromUtf8(message);
|
||||
}
|
||||
|
||||
const messageHash = sha256.hash(message);
|
||||
@@ -340,7 +340,7 @@ export class PrivateKey {
|
||||
*/
|
||||
public signMessageDER(message: Uint8Array | string) {
|
||||
if (typeof message === 'string') {
|
||||
message = Bytes.fromUtf8(message).toBytes();
|
||||
message = Bytes.fromUtf8(message);
|
||||
}
|
||||
|
||||
const messageHash = sha256.hash(message);
|
||||
@@ -363,10 +363,8 @@ export class PrivateKey {
|
||||
*/
|
||||
public signMessageElectron(message: string): Uint8Array {
|
||||
// Compile the message.
|
||||
const magicBytes = Bytes.fromUtf8(
|
||||
'\x18Bitcoin Signed Message:\n',
|
||||
).toBytes();
|
||||
const messageBytes = Bytes.fromUtf8(message).toBytes();
|
||||
const magicBytes = Bytes.fromUtf8('\x18Bitcoin Signed Message:\n');
|
||||
const messageBytes = Bytes.fromUtf8(message);
|
||||
const messageLengthBytes = bigIntToCompactUint(BigInt(messageBytes.length)); // hexToBin(messageBytes.length.toString(16)); //
|
||||
const messageFinal = new Uint8Array([
|
||||
...magicBytes,
|
||||
@@ -406,7 +404,7 @@ export class PrivateKey {
|
||||
// If a string was given, convert it from hex to bytes.
|
||||
// TODO: Consider using Bytes class - it ensures valid hex.
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (messageHash.length !== 32) {
|
||||
@@ -441,7 +439,7 @@ export class PrivateKey {
|
||||
// If a string was given, convert it from hex to bytes.
|
||||
// TODO: Consider using Bytes class - it ensures valid hex.
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (messageHash.length !== 32) {
|
||||
@@ -476,7 +474,7 @@ export class PrivateKey {
|
||||
// If a string was given, convert it from hex to bytes.
|
||||
// TODO: Consider using Bytes class - it ensures valid hex.
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (messageHash.length !== 32) {
|
||||
@@ -511,7 +509,7 @@ export class PrivateKey {
|
||||
// If a string was given, convert it from hex to bytes.
|
||||
// TODO: Consider using Bytes class - it ensures valid hex.
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (messageHash.length !== 32) {
|
||||
|
||||
@@ -78,7 +78,7 @@ export class PublicKey {
|
||||
* @returns The created Public Key entity.
|
||||
*/
|
||||
public static fromHex(publicKeyHex: string): PublicKey {
|
||||
return new this(Bytes.fromHex(publicKeyHex).toBytes());
|
||||
return new this(Bytes.fromHex(publicKeyHex));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,11 +232,11 @@ export class PublicKey {
|
||||
signature: Uint8Array | string,
|
||||
): boolean {
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (typeof signature === 'string') {
|
||||
signature = Bytes.fromHex(signature).toBytes();
|
||||
signature = Bytes.fromHex(signature);
|
||||
}
|
||||
|
||||
const validity = secp256k1.verifySignatureDER(
|
||||
@@ -261,11 +261,11 @@ export class PublicKey {
|
||||
signature: Uint8Array | string,
|
||||
): boolean {
|
||||
if (typeof messageHash === 'string') {
|
||||
messageHash = Bytes.fromHex(messageHash).toBytes();
|
||||
messageHash = Bytes.fromHex(messageHash);
|
||||
}
|
||||
|
||||
if (typeof signature === 'string') {
|
||||
signature = Bytes.fromHex(signature).toBytes();
|
||||
signature = Bytes.fromHex(signature);
|
||||
}
|
||||
|
||||
const validity = secp256k1.verifySignatureSchnorr(
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
* console.log(script.toHex());
|
||||
*
|
||||
* // Output as bytecode.
|
||||
* console.log(script.toBytes());
|
||||
* console.log(script);
|
||||
* ```
|
||||
*/
|
||||
export class Script {
|
||||
@@ -177,7 +177,7 @@ export class Script {
|
||||
*/
|
||||
public static fromHex(hex: string): Script {
|
||||
// Return new Script entity constructed from the binary.
|
||||
return this.fromBytes(Bytes.fromHex(hex).toBytes());
|
||||
return this.fromBytes(Bytes.fromHex(hex));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -212,7 +212,7 @@ export class Transaction extends BaseTransaction {
|
||||
* @returns {Transaction} The created Transaction Primitive.
|
||||
*/
|
||||
public static fromHex(hex: string): Transaction {
|
||||
const bytes = Bytes.fromHex(hex).toBytes();
|
||||
const bytes = Bytes.fromHex(hex).toUint8Array();
|
||||
return this.fromBytes(bytes);
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ export class TransactionResolved extends BaseTransaction {
|
||||
* @returns {Transaction} The created Transaction Primitive.
|
||||
*/
|
||||
public static fromHex(hex: string, sourceOutputs: Array<any>) {
|
||||
const bytes = Bytes.fromHex(hex).toBytes();
|
||||
const bytes = Bytes.fromHex(hex).toUint8Array();
|
||||
return this.fromBytes(bytes, sourceOutputs);
|
||||
}
|
||||
|
||||
@@ -425,7 +425,7 @@ export class TransactionHash {
|
||||
* @returns {TransactionHash} Instance of TransactionHash.
|
||||
*/
|
||||
public static fromHex(hex: string): TransactionHash {
|
||||
const bytes = Bytes.fromHex(hex).toBytes();
|
||||
const bytes = Bytes.fromHex(hex).toUint8Array();
|
||||
return this.fromBytes(bytes);
|
||||
}
|
||||
|
||||
@@ -469,7 +469,7 @@ export class TransactionHash {
|
||||
* @returns {TransactionHash} Instance of TransactionHash.
|
||||
*/
|
||||
public static fromTransactionHex(transactionHex: string): TransactionHash {
|
||||
const bytes = Bytes.fromHex(transactionHex).toBytes();
|
||||
const bytes = Bytes.fromHex(transactionHex).toUint8Array();
|
||||
return this.fromTransactionBytes(bytes);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user