Skip to content

Commit

Permalink
update to hash function to ensure unique seed generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersavery committed Apr 1, 2024
1 parent b25f2c6 commit d75d8c8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 69 deletions.
116 changes: 63 additions & 53 deletions src/__tests__/index.test.ts
Expand Up @@ -52,96 +52,106 @@ describe('Keypairs', () => {
test('can generate correct address from email password', () => {
const data = keypairService.keypairFromEmailPassword("tyler@tylersavery.com", 'password123', 0);
expect(data).toBeTruthy();
expect(data.address).toEqual("tb1qwh4zgpkysn8j53flz303u8tn0nvj293ey72u3k")
expect(data.address).toEqual("tb1q5vrsqhy9rtufemt643mx3j3nu6d2fh7gu5ncaj")
});


test('generates different addresses from different email/password combos', () => {
const data = keypairService.keypairFromEmailPassword("tyler@tylersavery.com", 'password123', 0);
expect(data).toBeTruthy();

const data2 = keypairService.keypairFromEmailPassword("tyler2@tylersavery.com", 'password456', 0);
expect(data2).toBeTruthy();
expect(data.address == data2.address).toBeFalsy();
});


});



describe('Transactions', () => {
let transactionService: TransactionService;
// describe('Transactions', () => {
// let transactionService: TransactionService;

beforeAll(() => {
transactionService = new TransactionService(true);
});
// beforeAll(() => {
// transactionService = new TransactionService(true);
// });

test('can create tx', async () => {
// test('can create tx', async () => {

const senderWif = "cPQ5kbnuj8YmBoCaFmsPsZENVykN1GGmF18mg6sEZsJPX2np6PRa"
const senderAddress = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"
const recipientAddress = "tb1q4lahda9feljf695q473z4m8m7xhgzv35n6226q"
const amount = 0.000003
// const senderWif = "cPQ5kbnuj8YmBoCaFmsPsZENVykN1GGmF18mg6sEZsJPX2np6PRa"
// const senderAddress = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"
// const recipientAddress = "tb1q4lahda9feljf695q473z4m8m7xhgzv35n6226q"
// const amount = 0.000003

const data = await transactionService.createTransaction(senderWif, senderAddress, recipientAddress, amount);
expect(data.success).toEqual(true);
const hash = data.result?.tx.hash;
expect(hash).toBeTruthy();
});
// const data = await transactionService.createTransaction(senderWif, senderAddress, recipientAddress, amount);
// expect(data.success).toEqual(true);
// const hash = data.result?.tx.hash;
// expect(hash).toBeTruthy();
// });


});
// });



describe('Account', () => {
let accountService: AccountService;
// describe('Account', () => {
// let accountService: AccountService;

beforeAll(() => {
accountService = new AccountService(true);
});
// beforeAll(() => {
// accountService = new AccountService(true);
// });

test('can get address info', async () => {
// test('can get address info', async () => {

const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"
// const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"

const data = await accountService.addressInfo(address);
expect(data).toBeTruthy();
expect(data.balance).toBeTruthy();
// const data = await accountService.addressInfo(address);
// expect(data).toBeTruthy();
// expect(data.balance).toBeTruthy();

const dataBtc = await accountService.addressInfo(address, false);
expect(dataBtc).toBeTruthy();
expect(dataBtc.balance).toEqual(data.balance * SATOSHI_TO_BTC_MULTIPLIER);
// const dataBtc = await accountService.addressInfo(address, false);
// expect(dataBtc).toBeTruthy();
// expect(dataBtc.balance).toEqual(data.balance * SATOSHI_TO_BTC_MULTIPLIER);

await new Promise(resolve => setTimeout(resolve, 3000));
// await new Promise(resolve => setTimeout(resolve, 3000));

});
// });

test('can get transactions and outputs', async () => {
// test('can get transactions and outputs', async () => {

const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"
// const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"

const data = await accountService.transactions(address);
expect(data).toBeTruthy();
expect(data.transactions.length).toBeGreaterThan(1);
// const data = await accountService.transactions(address);
// expect(data).toBeTruthy();
// expect(data.transactions.length).toBeGreaterThan(1);

await new Promise(resolve => setTimeout(resolve, 3000));
// await new Promise(resolve => setTimeout(resolve, 3000));

});
// });

// test('can paginate txs', async () => {
// test('can paginate txs', async () => {

// const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"
// const address = "tb1qh0nx4epkftfz3gmztkg9qmcyez604q36snzg0n"

// const data = await accountService.transactions(address, 2);
// expect(data).toBeTruthy();
// expect(data.transactions.length).toBeGreaterThan(1);
// const data = await accountService.transactions(address, 2);
// expect(data).toBeTruthy();
// expect(data.transactions.length).toBeGreaterThan(1);

// const lastResult = data.transactions[data.transactions.length - 1];
// const dataPage2 = await accountService.transactions(address, 2, lastResult.block_height);
// const lastResult = data.transactions[data.transactions.length - 1];
// const dataPage2 = await accountService.transactions(address, 2, lastResult.block_height);

// await new Promise(resolve => setTimeout(resolve, 3000));
// await new Promise(resolve => setTimeout(resolve, 3000));


// expect(dataPage2).toBeTruthy();
// expect(dataPage2.transactions.length).toBeGreaterThan(1);
// expect(dataPage2).toBeTruthy();
// expect(dataPage2.transactions.length).toBeGreaterThan(1);

// const page2lastResult = dataPage2.transactions[dataPage2.transactions.length - 1];
// const page2lastResult = dataPage2.transactions[dataPage2.transactions.length - 1];

// expect(page2lastResult.block_height).toBeLessThan(lastResult.block_height);
// expect(page2lastResult.block_height).toBeLessThan(lastResult.block_height);

// });
// });


});
// });
1 change: 1 addition & 0 deletions src/btc/keypair.ts
Expand Up @@ -91,6 +91,7 @@ export default class KeypairService {
seed = hashSeed(seed)
}


const privateKey = seedToPrivateKey(seed, index, this.network);
if (!privateKey) throw new Error('Invalid private key');

Expand Down
17 changes: 1 addition & 16 deletions src/btc/utils.ts
Expand Up @@ -34,29 +34,14 @@ export const seedToPrivateKey = (seed: string, index = 0, network: bitcoin.Netwo

export function hashSeed(seed: string) {

const seedBuffer = Buffer.from(seed, 'hex');
const seedBuffer = Buffer.from(seed);
const hashBuffer = bitcoin.crypto.sha256(seedBuffer);

// Convert the resulting hash Buffer to a hexadecimal string
const hashHex = hashBuffer.toString('hex');

return hashHex;



// // Encode the seed string into a Uint8Array using TextEncoder
// const encoder = new TextEncoder();
// const data = encoder.encode(seed);

// // Use the SubtleCrypto interface to hash the data with SHA-256
// bitcoin.crypto.sha256(data);
// const hashBuffer = await crypto.subtle.digest('SHA-256', data);

// // Convert the hash from an ArrayBuffer to a hexadecimal string
// const hashArray = Array.from(new Uint8Array(hashBuffer));
// const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');

// return hashHex;
}


Expand Down

0 comments on commit d75d8c8

Please sign in to comment.