Skip to content

Commit

Permalink
Attestclient SignTranscation to sign multiple inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nkostoulas committed Nov 30, 2018
1 parent 6fdb29f commit 27c7d85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
52 changes: 33 additions & 19 deletions attestation/attestclient.go
Expand Up @@ -103,19 +103,15 @@ func NewAttestClient(config *confpkg.Config, signerFlag ...bool) *AttestClient {
var errPkWif error
pkWif, errPkWif = crypto.GetWalletPrivKey(pk)
if errPkWif != nil {
log.Printf("%s %s\n", ERROR_INVALID_PK, pk)
log.Fatal(errPkWif)
log.Fatalf("%s %s\n%v\n", ERROR_INVALID_PK, pk, errPkWif)
}
importErr := config.MainClient().ImportPrivKeyRescan(pkWif, "init", false)
if importErr != nil {
log.Printf("%s %s\n", ERROR_FAILURE_IMPORTING_PK, pk)
log.Fatal(importErr)
log.Fatalf("%s %s\n%v\n", ERROR_FAILURE_IMPORTING_PK, pk, importErr)
}
} else if multisig == "" {
log.Fatal(ERROR_MISSING_MULTISIG)
}

if multisig != "" { // if multisig attestation, parse pubkeys
} else { // if multisig is set, parse pubkeys
pubkeys, numOfSigs := crypto.ParseRedeemScript(config.InitScript())

// verify our key is one of the multisig keys in signer case
Expand Down Expand Up @@ -294,29 +290,47 @@ func (w *AttestClient) GetScriptFromHash(hash chainhash.Hash) string {

// Sign transaction using key/redeemscript pair generated by previous attested hash
// This method should only be used in the attestation client signer case
// Any excess transaction inputs are signed using the initial/topup private key
// and the initial/topup script, assuming they are used to topup the attestation service
func (w *AttestClient) SignTransaction(hash chainhash.Hash, msgTx wire.MsgTx) (
*wire.MsgTx, string, error) {

// Calculate private key and redeemScript from hash
key := w.GetKeyFromHash(hash)
redeemScript := w.GetScriptFromHash(hash)
// Can't get redeem script from unspent as importaddress P2SH not supported
// if txunspent.RedeemScript != "" {
// redeemScript = txunspent.RedeemScript
// }

// sign tx and send signature to main attestation client
// fetch previous attestation transaction
prevTxId := msgTx.TxIn[0].PreviousOutPoint.Hash
prevTx, errRaw := w.MainClient.GetRawTransaction(&prevTxId)
if errRaw != nil {
return nil, "", errRaw
prevTx, prevTxErr := w.MainClient.GetRawTransaction(&prevTxId)
if prevTxErr != nil {
return nil, "", prevTxErr
}

var inputs []btcjson.RawTxInput // new tx inputs
var keys []string // keys to sign inputs

// add prev attestation tx input info and priv key
inputs = append(inputs, btcjson.RawTxInput{prevTxId.String(), 0,
hex.EncodeToString(prevTx.MsgTx().TxOut[0].PkScript), redeemScript})
keys = append(keys, key.String())

// for any remaining vins - sign with initial/topup privkey
// this should be a very rare occasion
for i := 1; i < len(msgTx.TxIn); i++ {
// fetch previous attestation transaction
prevTxId = msgTx.TxIn[i].PreviousOutPoint.Hash
prevTx, prevTxErr = w.MainClient.GetRawTransaction(&prevTxId)
if prevTxErr != nil {
return nil, "", prevTxErr
}
inputs = append(inputs, btcjson.RawTxInput{prevTxId.String(), 0,
hex.EncodeToString(prevTx.MsgTx().TxOut[0].PkScript), w.script0})
keys = append(keys, w.WalletPriv.String())
}

// Sign transaction
rawTxInput := btcjson.RawTxInput{prevTxId.String(), 0,
hex.EncodeToString(prevTx.MsgTx().TxOut[0].PkScript), redeemScript}
// attempt to sign transcation with provided inputs - keys
signedMsgTx, _, errSign := w.MainClient.SignRawTransaction3(
&msgTx, []btcjson.RawTxInput{rawTxInput}, []string{key.String()})
&msgTx, inputs, keys)
if errSign != nil {
return nil, "", errSign
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/run-tests.sh
Expand Up @@ -4,4 +4,4 @@ alias dir="$GOPATH/src/mainstay"

# run tests sequentially
cd $GOPATH/src/mainstay
go test -v -p=1 ./...
go test -v=0 -p=1 ./...

0 comments on commit 27c7d85

Please sign in to comment.