Skip to content

Commit

Permalink
Merge pull request #204 from bitcoin-sv/fix-644-stable-pki
Browse files Browse the repository at this point in the history
fix(BUX-644): handle new paymail pub key
  • Loading branch information
arkadiuszos4chain authored Mar 26, 2024
2 parents cdf9527 + b5d5dd3 commit 0c46148
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ func (b *WalletClient) UpdateTransactionMetadata(ctx context.Context, txID strin

// FinalizeTransaction will finalize the transaction
func (b *WalletClient) FinalizeTransaction(draft *models.DraftTransaction) (string, transports.ResponseError) {
return transports.SignInputs(draft, b.xPriv)
res, err := transports.GetSignedHex(draft, b.xPriv)
if err != nil {
return "", transports.WrapError(err)
}

return res, nil
}

// SendToRecipients send to recipients
Expand Down
106 changes: 53 additions & 53 deletions transports/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,77 +32,77 @@ func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString stri
return setSignatureHeaders(header, authData)
}

// SignInputs will sign all the inputs using the given xPriv key
func SignInputs(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (signedHex string, resError ResponseError) {
var err error
// Start a bt draft transaction
var txDraft *bt.Tx
if txDraft, err = bt.NewTxFromString(dt.Hex); err != nil {
resError = WrapError(err)
// GetSignedHex will sign all the inputs using the given xPriv key
func GetSignedHex(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (signedHex string, err error) {
var tx *bt.Tx
if tx, err = bt.NewTxFromString(dt.Hex); err != nil {
return
}

// Sign the inputs
for index, input := range dt.Configuration.Inputs {
// Enrich inputs
for index, draftInput := range dt.Configuration.Inputs {
tx.Inputs[index].PreviousTxSatoshis = draftInput.Satoshis

// Get the locking script
var ls *bscript.Script
if ls, err = bscript.NewFromHexString(
input.Destination.LockingScript,
); err != nil {
resError = WrapError(err)
dst := draftInput.Destination
if err = setPreviousTxScript(tx, uint32(index), &dst); err != nil {
return
}
txDraft.Inputs[index].PreviousTxScript = ls
txDraft.Inputs[index].PreviousTxSatoshis = input.Satoshis

// Derive the child key (chain)
var chainKey *bip32.ExtendedKey
if chainKey, err = xPriv.Child(
input.Destination.Chain,
); err != nil {
resError = WrapError(err)
if err = setUnlockingScript(tx, uint32(index), xPriv, &dst); err != nil {
return
}
}

// Derive the child key (num)
var numKey *bip32.ExtendedKey
if numKey, err = chainKey.Child(
input.Destination.Num,
); err != nil {
resError = WrapError(err)
return
}
// Return the signed hex
signedHex = tx.String()
return
}

// Get the private key
var privateKey *bec.PrivateKey
if privateKey, err = bitcoin.GetPrivateKeyFromHDKey(
numKey,
); err != nil {
resError = WrapError(err)
return
}
func setPreviousTxScript(tx *bt.Tx, inputIndex uint32, dst *models.Destination) (err error) {
var ls *bscript.Script
if ls, err = bscript.NewFromHexString(dst.LockingScript); err != nil {
return
}

// Get the unlocking script
var s *bscript.Script
if s, err = getUnlockingScript(
txDraft, uint32(index), privateKey,
); err != nil {
resError = WrapError(err)
return
}
tx.Inputs[inputIndex].PreviousTxScript = ls
return
}

func setUnlockingScript(tx *bt.Tx, inputIndex uint32, xPriv *bip32.ExtendedKey, dst *models.Destination) (err error) {
var key *bec.PrivateKey
if key, err = getDerivedKeyForDestination(xPriv, dst); err != nil {
return
}

var s *bscript.Script
if s, err = getUnlockingScript(tx, inputIndex, key); err != nil {
return
}

tx.Inputs[inputIndex].UnlockingScript = s
return
}

func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destination) (key *bec.PrivateKey, err error) {
// Derive the child key (m/chain/num)
var derivedKey *bip32.ExtendedKey
if derivedKey, err = bitcoin.GetHDKeyByPath(xPriv, dst.Chain, dst.Num); err != nil {
return
}

// Insert the locking script
if err = txDraft.InsertInputUnlockingScript(
uint32(index), s,
// Derive key for paymail destination (m/chain/num/paymailNum)
if dst.PaymailExternalDerivationNum != nil {
if derivedKey, err = derivedKey.Child(
*dst.PaymailExternalDerivationNum,
); err != nil {
resError = WrapError(err)
return
}
}

// Return the signed hex
signedHex = txDraft.String()
if key, err = bitcoin.GetPrivateKeyFromHDKey(derivedKey); err != nil {
return
}

return
}

Expand Down

0 comments on commit 0c46148

Please sign in to comment.