Skip to content

Commit

Permalink
Add Keychain.build_transaction function
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Oct 10, 2023
1 parent ea15e41 commit d201809
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
wordArrayToUint8Array
} from "./utils.js";
import { Curve, HashAlgorithm, Keypair, Services } from "./types.js";
import TransactionBuilder from "./transaction_builder.js";
import {
curveToID,
deriveAddress,
Expand All @@ -18,6 +19,7 @@ import {
randomSecretKey,
ecEncrypt,
aesEncrypt,
sign,
IDToCurve,
IDToHashAlgo
} from "./crypto.js";
Expand Down Expand Up @@ -101,6 +103,34 @@ export default class Keychain {
return this;
}

buildTransaction(tx: TransactionBuilder, service: string, index: number, suffix: string = "") {
if (!this.services[service]) {
throw "Service doesn't exist in the keychain"
}

if (typeof index !== 'number' || index < 0) {
throw "'index' must be a positive number"
}

if (typeof (suffix) !== "string") {
throw "'suffix must be a string"
}

const keypair: Keypair = this.deriveKeypair(service, index, suffix)
const address: Uint8Array = this.deriveAddress(service, index + 1, suffix)

tx.setAddress(address)

const payloadForPreviousSignature = tx.previousSignaturePayload()
const previousSignature = sign(payloadForPreviousSignature, keypair.privateKey)

tx.setPreviousSignatureAndPreviousPublicKey(
previousSignature,
keypair.publicKey
)

return tx
}
/**
* Encode the keychain
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/keychain.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import Keychain, { keyToJWK } from "../src/keychain"
import { uint8ArrayToHex, concatUint8Arrays, wordArrayToUint8Array } from "../src/utils"
import { deriveAddress, deriveKeyPair, ecDecrypt, aesDecrypt } from "../src/crypto"
import { deriveAddress, deriveKeyPair, ecDecrypt, aesDecrypt, verify } from "../src/crypto"
import TransactionBuilder from "../src/transaction_builder";
// @ts-ignore
import CryptoJS from "crypto-js";
//import TransactionBuilder from "../lib/transaction_builder.js"
Expand Down Expand Up @@ -166,7 +167,7 @@ describe("Keychain", () => {
})
})

/*describe("buildTransaction", () => {
describe("buildTransaction", () => {
it("should build the transaction and the related signature", () => {
const keychain = new Keychain("seed")
keychain.addService("uco", "m/650'/0/0")
Expand All @@ -180,13 +181,12 @@ describe("Keychain", () => {
const { publicKey } = keychain.deriveKeypair("uco")
const address = keychain.deriveAddress("uco", 1)

assert.deepStrictEqual(tx.address, address)
assert.deepStrictEqual(tx.previousPublicKey, publicKey)
expect(tx.address).toStrictEqual(address)
expect(tx.previousPublicKey).toStrictEqual(publicKey)

assert.strictEqual(verify(tx.previousSignature, tx.previousSignaturePayload(), tx.previousPublicKey), true)
expect(verify(tx.previousSignature, tx.previousSignaturePayload(), tx.previousPublicKey)).toStrictEqual(true)
})
})*/

})
})


0 comments on commit d201809

Please sign in to comment.