Skip to content

Commit

Permalink
chore: lint all files
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Aug 9, 2023
1 parent 4914284 commit 5ccfe9c
Show file tree
Hide file tree
Showing 33 changed files with 727 additions and 720 deletions.
10 changes: 5 additions & 5 deletions packages/sdk/src/addresses/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ export const addressFormats = {
p2wpkh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2tr: /^(tb1p|bcrt1p)[a-zA-HJ-NP-Z0-9]{14,74}$/
}
} as const;
} as const

export const addressTypeToName = {
p2pkh: "legacy",
p2sh: "nested-segwit",
p2wpkh: "segwit",
p2tr: "taproot"
} as const;
} as const

export const addressNameToType = {
legacy: "p2pkh",
segwit: "p2wpkh",
"nested-segwit": "p2sh",
taproot: "p2tr"
} as const;
} as const

export type AddressTypes = keyof typeof addressTypeToName;
export type AddressFormats = (typeof addressTypeToName)[AddressTypes];
export type AddressTypes = keyof typeof addressTypeToName
export type AddressFormats = (typeof addressTypeToName)[AddressTypes]
152 changes: 76 additions & 76 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import * as ecc from "@bitcoinerlab/secp256k1";
import BIP32Factory, { BIP32Interface } from "bip32";
import * as ecc from "@bitcoinerlab/secp256k1"
import BIP32Factory, { BIP32Interface } from "bip32"

import { Network } from "../config/types";
import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "../utils";
import { AddressFormats, addressFormats, addressNameToType, AddressTypes, addressTypeToName } from "./formats";
import { Network } from "../config/types"
import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "../utils"
import { AddressFormats, addressFormats, addressNameToType, AddressTypes, addressTypeToName } from "./formats"

export function getAddressFormat(address: string, network: Network) {
let format = {
address,
format: "unknown"
};
}

const addressTypes = addressFormats[network];
const addressTypesList = Object.keys(addressTypes);
const addressTypes = addressFormats[network]
const addressTypesList = Object.keys(addressTypes)

for (let i = 0; i < addressTypesList.length; i++) {
const addressType = addressTypesList[i] as AddressTypes;
const addressTypeReg = addressTypes[addressType];
const addressName = addressTypeToName[addressType];
const addressType = addressTypesList[i] as AddressTypes
const addressTypeReg = addressTypes[addressType]
const addressName = addressTypeToName[addressType]

if (addressTypeReg.test(address)) {
format = {
address,
format: addressName
};
}
}
}

return format;
return format
}

export function getAddressType(address: string, network: Network): AddressTypes {
const addressFormat = getAddressFormat(address, network).format;
return addressNameToType[addressFormat as AddressFormats];
const addressFormat = getAddressFormat(address, network).format
return addressNameToType[addressFormat as AddressFormats]
}

export function getAddressesFromPublicKey(
Expand All @@ -41,64 +41,64 @@ export function getAddressesFromPublicKey(
format: AddressTypes | "all" = "all"
) {
if (!Buffer.isBuffer(pubKey)) {
pubKey = Buffer.from(pubKey, "hex");
pubKey = Buffer.from(pubKey, "hex")
}
const bip32 = BIP32Factory(ecc);
const networkObj = getNetwork(network);
const chainCode = Buffer.alloc(32).fill(1);
const bip32 = BIP32Factory(ecc)
const networkObj = getNetwork(network)
const chainCode = Buffer.alloc(32).fill(1)

const addresses: Address[] = [];
const addresses: Address[] = []

let childNodeXOnlyPubkey = pubKey;
let childNodeXOnlyPubkey = pubKey

const keys = bip32.fromPublicKey(pubKey, chainCode, networkObj);
const keys = bip32.fromPublicKey(pubKey, chainCode, networkObj)

childNodeXOnlyPubkey = keys.publicKey.subarray(1, 33);
childNodeXOnlyPubkey = keys.publicKey.subarray(1, 33)

if (format === "all") {
const addressTypesList = Object.keys(addressTypeToName) as AddressTypes[];
const addressTypesList = Object.keys(addressTypeToName) as AddressTypes[]

addressTypesList.forEach((addrType) => {
if (addrType === "p2tr") {
const paymentObj = createTransaction(childNodeXOnlyPubkey, addrType, network);
const paymentObj = createTransaction(childNodeXOnlyPubkey, addrType, network)

addresses.push({
address: paymentObj.address,
xkey: childNodeXOnlyPubkey.toString("hex"),
format: addressTypeToName[addrType],
pub: keys.publicKey.toString("hex")
});
})
} else {
const paymentObj = createTransaction(keys.publicKey, addrType, network);
const paymentObj = createTransaction(keys.publicKey, addrType, network)

addresses.push({
address: paymentObj.address,
format: addressTypeToName[addrType],
pub: keys.publicKey.toString("hex")
});
})
}
});
})
} else {
const key = format === "p2tr" ? childNodeXOnlyPubkey : keys.publicKey;
const paymentObj = createTransaction(key, format, network);
const key = format === "p2tr" ? childNodeXOnlyPubkey : keys.publicKey
const paymentObj = createTransaction(key, format, network)

addresses.push({
address: paymentObj.address,
format: addressTypeToName[format],
pub: keys.publicKey.toString("hex"),
xkey: format === "p2tr" ? childNodeXOnlyPubkey.toString("hex") : undefined
});
})
}

return addresses;
return addresses
}

export async function getAddresses({
pubKey,
network,
format
}: GetAddressesOptions): Promise<ReturnType<typeof getAddressesFromPublicKey>> {
return getAddressesFromPublicKey(pubKey, network, format);
return getAddressesFromPublicKey(pubKey, network, format)
}

export function getAccountDataFromHdNode({
Expand All @@ -109,18 +109,18 @@ export function getAccountDataFromHdNode({
addressIndex = 0
}: GetAccountDataFromHdNodeOptions) {
if (!hdNode) {
throw new Error("Invalid options provided.");
throw new Error("Invalid options provided.")
}

const addressType = addressNameToType[format];
const addressType = addressNameToType[format]

const fullDerivationPath = getDerivationPath(format, account, addressIndex);
const child = hdNode.derivePath(fullDerivationPath);
const fullDerivationPath = getDerivationPath(format, account, addressIndex)
const child = hdNode.derivePath(fullDerivationPath)

const pubKey = format === "taproot" ? toXOnly(child.publicKey) : child.publicKey;
const paymentObj = createTransaction(pubKey, addressType, network);
const pubKey = format === "taproot" ? toXOnly(child.publicKey) : child.publicKey
const paymentObj = createTransaction(pubKey, addressType, network)

const address = paymentObj.address!;
const address = paymentObj.address!

const accountData: Account = {
address,
Expand All @@ -134,66 +134,66 @@ export function getAccountDataFromHdNode({
path: fullDerivationPath
},
child
};
}

if (format === "taproot") {
accountData.xkey = toXOnly(child.publicKey).toString("hex");
accountData.xkey = toXOnly(child.publicKey).toString("hex")
}

return accountData;
return accountData
}

export function getAllAccountsFromHdNode({ hdNode, network = "testnet" }: GetAllAccountsFromHDNodeOptions) {
const accounts: Account[] = [];
const addressTypesList = Object.values(addressTypeToName) as AddressFormats[];
const accounts: Account[] = []
const addressTypesList = Object.values(addressTypeToName) as AddressFormats[]

addressTypesList.forEach((addrType) => {
const account = getAccountDataFromHdNode({
hdNode,
format: addrType,
network
});
})

accounts.push(account);
});
accounts.push(account)
})

return accounts;
return accounts
}

export type Address = {
address: string | undefined;
xkey?: string;
format: string;
pub: string;
};
address: string | undefined
xkey?: string
format: string
pub: string
}

export type Derivation = {
account: number;
addressIndex: number;
path: string;
};
account: number
addressIndex: number
path: string
}

export type Account = Address & {
priv: string;
type: AddressTypes;
derivationPath: Derivation;
child: BIP32Interface;
};
priv: string
type: AddressTypes
derivationPath: Derivation
child: BIP32Interface
}

type GetAddressesOptions = {
pubKey: string;
network: Network;
format: AddressTypes | "all";
};
pubKey: string
network: Network
format: AddressTypes | "all"
}

type GetAccountDataFromHdNodeOptions = {
hdNode: BIP32Interface;
format?: AddressFormats;
network?: Network;
account?: number;
addressIndex?: number;
};
hdNode: BIP32Interface
format?: AddressFormats
network?: Network
account?: number
addressIndex?: number
}

type GetAllAccountsFromHDNodeOptions = Omit<GetAccountDataFromHdNodeOptions, "format">;
type GetAllAccountsFromHDNodeOptions = Omit<GetAccountDataFromHdNodeOptions, "format">

export * from "./formats";
export * from "./formats"
Loading

0 comments on commit 5ccfe9c

Please sign in to comment.