Skip to content

Commit

Permalink
made some methods public. Added a new createSignature method on the w…
Browse files Browse the repository at this point in the history
…allet class.
  • Loading branch information
vekexasia committed May 6, 2018
1 parent cee24c5 commit cda6de8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/trxTypes/BaseTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ITransaction<AssetType = {}> {
id: string;
signature: string;
signSignature?: string;
signatures?: string[];
}

/**
Expand Down Expand Up @@ -219,17 +220,14 @@ export abstract class BaseTx<T = {}> {
* @param {string} privKey
* @returns {Buffer}
*/
protected createSignature(privKey: string) {
public createSignature(privKey: string) {
const hash = this.getHash();
return sodium.crypto_sign_detached(
hash,
new Buffer(privKey, 'hex'));
}

get signature() {
if (empty(this._signature)) {
throw new Error('Call create first');
}
return this._signature;
}

Expand Down Expand Up @@ -294,7 +292,7 @@ export abstract class BaseTx<T = {}> {
* Calculates Tx id!
* @returns {string}
*/
protected calcId(): string {
public calcId(): string {
const hash = this.getHash();
const temp = new Buffer(8);
for (let i = 0; i < 8; i++) {
Expand Down
26 changes: 24 additions & 2 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export abstract class GenericWallet {
}
return true;
}

// tslint:disable variable-name
protected _privKey: string;
protected _publicKey: string;
Expand Down Expand Up @@ -72,11 +73,32 @@ export abstract class GenericWallet {
*/
public signTransaction<T>(tx: ITransaction<T> | BaseTx<T>, coSign?: GenericWallet): ITransaction<T> {
if (!(tx instanceof BaseTx)) {
tx = createTransactionFromOBJ<T>(tx);
tx = createTransactionFromOBJ<any>(tx);
}
tx.signature = this.getSignatureOfTransaction(tx);
if (coSign) {
tx.signSignature = coSign.getSignatureOfTransaction(tx);
tx.id = tx.calcId();
}
return tx.toObj();
}

/**
* Returns signature of provided transaction without modifiying the original tx.
* @return {string} signature buffer.
*/
public getSignatureOfTransaction(tx: ITransaction<any> | BaseTx<any>): string {
if (!(tx instanceof BaseTx)) {
tx = createTransactionFromOBJ<any>(tx);
}
tx.addressSuffixLength = this.addressOptions.suffixLength;
if (!tx.signature) {
const signedTx = tx.sign(this);
return signedTx.signature;
}
return tx
.sign(this, coSign ? coSign.privKey : undefined);
.createSignature(this.privKey)
.toString('hex');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/genericwallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ describe('genericwallet', () => {
});

});

describe('getSignatureOfTransaction', () => {

})
});

0 comments on commit cda6de8

Please sign in to comment.