Skip to content

Commit

Permalink
Fix signing error(Inconsistent network) for sending TPC transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi authored and azuchi committed Jan 8, 2021
1 parent c4a1b36 commit 56c1b41
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/data_store/cordova_data_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CordovaDataStore {
processTx(keys, tx) {
return __awaiter(this, void 0, void 0, function*() {
const hashes = util.keyToPubkeyHashes(keys);
const txid = tx.getId();
return new Promise((resolve, reject) => {
this.database.transaction(
db => {
Expand All @@ -94,7 +95,7 @@ class CordovaDataStore {
db.executeSql(
'INSERT INTO utxos(txid, height, outIndex, value, scriptPubkey, colorId) values (?, ?, ?, ?, ?, ?)',
[
tx.getId(),
txid,
0,
i,
tx.outs[i].value,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ const DataStore = require('./data_store');
exports.DataStore = DataStore;
const KeyStore = require('./key_store');
exports.KeyStore = KeyStore;
const Signer = require('./signer');
exports.Signer = Signer;
const Wallet = require('./wallet');
exports.Wallet = Wallet;
17 changes: 13 additions & 4 deletions src/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ var __awaiter =
};
Object.defineProperty(exports, '__esModule', { value: true });
const tapyrus = require('tapyrusjs-lib');
function sign(wallet, txb, utxos) {
class SignOptions {}
exports.SignOptions = SignOptions;
function sign(wallet, txb, utxos, options) {
return __awaiter(this, void 0, void 0, function*() {
yield Promise.all(
utxos.map((utxo, index) =>
__awaiter(this, void 0, void 0, function*() {
const keyPair = yield keyForScript(wallet, utxo.scriptPubkey);
const keyPair = yield keyForScript(
wallet,
utxo.scriptPubkey,
options,
);
if (keyPair) {
txb.sign({
prevOutScriptType: utxo.type(),
Expand All @@ -49,12 +55,15 @@ function sign(wallet, txb, utxos) {
});
}
exports.sign = sign;
function keyForScript(wallet, script) {
function keyForScript(wallet, script, options) {
return __awaiter(this, void 0, void 0, function*() {
const targetHash = outputToPubkeyHash(Buffer.from(script, 'hex'));
const keys = yield wallet.keyStore.keys();
const network = (options || {}).network || tapyrus.networks.prod;
return keys
.map(k => tapyrus.ECPair.fromPrivateKey(Buffer.from(k, 'hex')))
.map(k =>
tapyrus.ECPair.fromPrivateKey(Buffer.from(k, 'hex'), { network }),
)
.find(keyPair => {
const hash = tapyrus.payments.p2pkh({ pubkey: keyPair.publicKey }).hash;
return hash.toString('hex') === targetHash.toString('hex');
Expand Down
4 changes: 2 additions & 2 deletions ts_src/data_store/cordova_data_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class CordovaDataStore implements DataStore {

async processTx(keys: string[], tx: tapyrus.Transaction): Promise<void> {
const hashes = util.keyToPubkeyHashes(keys);

const txid = tx.getId();
return new Promise(
(resolve, reject): void => {
this.database.transaction(
Expand All @@ -74,7 +74,7 @@ export default class CordovaDataStore implements DataStore {
db.executeSql(
'INSERT INTO utxos(txid, height, outIndex, value, scriptPubkey, colorId) values (?, ?, ?, ?, ?, ?)',
[
tx.getId(),
txid,
0,
i,
tx.outs[i].value,
Expand Down
3 changes: 2 additions & 1 deletion ts_src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Config } from './config';
import * as DataStore from './data_store';
import * as KeyStore from './key_store';
import * as Signer from './signer';
import * as Wallet from './wallet';

export { DataStore, KeyStore, Wallet, Config };
export { DataStore, KeyStore, Wallet, Config, Signer };
12 changes: 10 additions & 2 deletions ts_src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import * as tapyrus from 'tapyrusjs-lib';
import { Utxo } from './utxo';
import Wallet from './wallet';

export class SignOptions {
network?: tapyrus.Network;
}

export async function sign(
wallet: Wallet,
txb: tapyrus.TransactionBuilder,
utxos: Utxo[],
options?: SignOptions,
): Promise<tapyrus.TransactionBuilder> {
await Promise.all(
utxos.map(async (utxo: Utxo, index: number) => {
const keyPair = await keyForScript(wallet, utxo.scriptPubkey);
const keyPair = await keyForScript(wallet, utxo.scriptPubkey, options);
if (keyPair) {
txb.sign({
prevOutScriptType: utxo.type(),
Expand All @@ -25,11 +30,14 @@ export async function sign(
export async function keyForScript(
wallet: Wallet,
script: string,
options?: SignOptions,
): Promise<tapyrus.ECPair.ECPairInterface | undefined> {
const targetHash = outputToPubkeyHash(Buffer.from(script, 'hex'));
const keys = await wallet.keyStore.keys();
const network: tapyrus.Network =
(options || {}).network || tapyrus.networks.prod;
return keys
.map(k => tapyrus.ECPair.fromPrivateKey(Buffer.from(k, 'hex')))
.map(k => tapyrus.ECPair.fromPrivateKey(Buffer.from(k, 'hex'), { network }))
.find(keyPair => {
const hash = tapyrus.payments.p2pkh({ pubkey: keyPair.publicKey }).hash!;
return hash.toString('hex') === targetHash.toString('hex');
Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Config } from './config';
import * as DataStore from './data_store';
import * as KeyStore from './key_store';
import * as Signer from './signer';
import * as Wallet from './wallet';
export { DataStore, KeyStore, Wallet, Config };
export { DataStore, KeyStore, Wallet, Config, Signer };
8 changes: 6 additions & 2 deletions types/signer.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference types="node" />
import * as tapyrus from 'tapyrusjs-lib';
import { Utxo } from './utxo';
import Wallet from './wallet';
export declare function sign(wallet: Wallet, txb: tapyrus.TransactionBuilder, utxos: Utxo[]): Promise<tapyrus.TransactionBuilder>;
export declare function keyForScript(wallet: Wallet, script: string): Promise<tapyrus.ECPair.ECPairInterface | undefined>;
export declare class SignOptions {
network?: tapyrus.Network;
}
export declare function sign(wallet: Wallet, txb: tapyrus.TransactionBuilder, utxos: Utxo[], options?: SignOptions): Promise<tapyrus.TransactionBuilder>;
export declare function keyForScript(wallet: Wallet, script: string, options?: SignOptions): Promise<tapyrus.ECPair.ECPairInterface | undefined>;
export declare function outputToPubkeyHash(output: Buffer): Buffer;
1 change: 1 addition & 0 deletions types/token.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import * as tapyrus from 'tapyrusjs-lib';
import { Utxo } from './utxo';
import Wallet from './wallet';
Expand Down

0 comments on commit 56c1b41

Please sign in to comment.