Skip to content

Commit

Permalink
chore(wallet): resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thedoublejay committed Jul 8, 2024
1 parent be39c1b commit c3fddfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export function getAllAccountsFromHdNode({ hdNode, network = "testnet", account
hdNode,
format: addrType,
network,
account, addressIndex
account,
addressIndex
})

accounts.push(walletAccount)
Expand Down
21 changes: 10 additions & 11 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import ECPairFactory, { ECPairInterface } from "ecpair"
import {
Account,
AddressFormats,
addressNameToType,
addressNameToType, DerivationIndex,
getAccountDataFromHdNode,
getAddressesFromPublicKey,
getAllAccountsFromHdNode,
getNetwork,
mintFromCollection,
publishCollection,
publishCollection, SigningMessageOptions,
tweakSigner
} from ".."
import { Network } from "../config/types"
Expand Down Expand Up @@ -98,12 +98,12 @@ export class Ordit {
this.#network = value
}

getAddressByType(type: AddressFormats, accountIndex: number, addressIndex: number) {
getAddressByType(type: AddressFormats, derivationIndex: DerivationIndex) {
if (!this.#initialized || !this.allAddresses.length) {
throw new OrditSDKError("Wallet not fully initialized.")
}
return this.allAddresses.find((address) => address.format === type && address.derivationPath.account === accountIndex
&& address.derivationPath.addressIndex === addressIndex);
return this.allAddresses.find((address) => address.format === type && address.derivationPath.account === derivationIndex.accountIndex
&& address.derivationPath.addressIndex === derivationIndex.addressIndex);
}

getAllAddresses() {
Expand All @@ -118,7 +118,7 @@ export class Ordit {
if (this.selectedAddressType === type) return
let addressToSelect: Account;

const account = this.getAddressByType(type, accountIndex, addressIndex) as Account
const account = this.getAddressByType(type, { accountIndex, addressIndex }) as Account
if (!account) {
addressToSelect = this.generateAddress(type, accountIndex, addressIndex);
// Push to current list of addresses
Expand Down Expand Up @@ -235,12 +235,11 @@ export class Ordit {
return psbt.toHex()
}

signMessage(message: string, type?: AddressFormats, accountIndex?: number, addressIndex?: number) {
signMessage(message: string, type?: AddressFormats, opts?: SigningMessageOptions) {
const addressType = type || this.selectedAddressType
const accountIndexToSign: number = accountIndex === undefined ? 0 : accountIndex;
const addressIndexToSign: number = addressIndex === undefined ? 0 : addressIndex;
const node = this.allAddresses.find((wallet) => wallet.format === addressType && wallet.derivationPath.account === accountIndexToSign
&& wallet.derivationPath.addressIndex === addressIndexToSign) as Account
const accountIndexToSign: number = opts?.accountIndex === undefined ? 0 : opts?.accountIndex;
const addressIndexToSign: number = opts?.addressIndex === undefined ? 0 : opts?.addressIndex;
const node = this.getAddressByType(addressType!, { accountIndex: accountIndexToSign, addressIndex: addressIndexToSign }) as Account
const signature = AddressUtils.isP2PKH(node.address!)
? sign(message, node.child.privateKey!)
: Signer.sign(node.child.toWIF(), node.address!, message, getNetwork(this.#network))
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ export type GetWalletOptions = {
format: AddressTypes | "all"
safeMode?: OnOffUnion
}

export interface DerivationIndex {
accountIndex: number
addressIndex: number
}

export type SigningMessageOptions = Partial<DerivationIndex>

0 comments on commit c3fddfa

Please sign in to comment.