Use the new btcec Signature.Serialize API.
This commit modifies the code to use the new btcec Signature.Serialize API instead of the internal sigDER which has now been removed. This closes #3. ok @owainga
This commit is contained in:
parent
c0c167cc15
commit
4f4afedf39
1 changed files with 2 additions and 45 deletions
47
script.go
47
script.go
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1000,7 +999,8 @@ func signatureScriptCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot sign tx input: %s", err)
|
return nil, fmt.Errorf("cannot sign tx input: %s", err)
|
||||||
}
|
}
|
||||||
sig := append(sigDER(r, s), hashType)
|
ecSig := btcec.Signature{R: r, S: s}
|
||||||
|
sig := append(ecSig.Serialize(), hashType)
|
||||||
|
|
||||||
pk := (*btcec.PublicKey)(&privkey.PublicKey)
|
pk := (*btcec.PublicKey)(&privkey.PublicKey)
|
||||||
var pubkeyOpcode *parsedOpcode
|
var pubkeyOpcode *parsedOpcode
|
||||||
|
@ -1025,49 +1025,6 @@ func signatureScriptCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int,
|
||||||
return unparseScript(pops)
|
return unparseScript(pops)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sigDER returns the ECDSA signature r, s in the DER format used by
|
|
||||||
// signature scripts. The signature does not include the appended hashtype.
|
|
||||||
//
|
|
||||||
// encoding/asn1 is broken so we hand roll this output:
|
|
||||||
//
|
|
||||||
// 0x30 <length> 0x02 <length r> r 0x02 <length s> s
|
|
||||||
func sigDER(r, s *big.Int) []byte {
|
|
||||||
// In DER format, a leading 0x00 octet must be prepended to
|
|
||||||
// the byte slice so it cannot be interpreted as a negative
|
|
||||||
// big-endian number. This is only done if the sign bit on
|
|
||||||
// the first byte is set.
|
|
||||||
var rb, sb []byte
|
|
||||||
if r.Bytes()[0]&0x80 != 0 {
|
|
||||||
paddedBytes := make([]byte, len(r.Bytes())+1)
|
|
||||||
copy(paddedBytes[1:], r.Bytes())
|
|
||||||
rb = paddedBytes
|
|
||||||
} else {
|
|
||||||
rb = r.Bytes()
|
|
||||||
}
|
|
||||||
if s.Bytes()[0]&0x80 != 0 {
|
|
||||||
paddedBytes := make([]byte, len(s.Bytes())+1)
|
|
||||||
copy(paddedBytes[1:], s.Bytes())
|
|
||||||
sb = paddedBytes
|
|
||||||
} else {
|
|
||||||
sb = s.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
// total length of returned signature is 1 byte for each magic and
|
|
||||||
// length (6 total), plus lengths of r and s
|
|
||||||
length := 6 + len(rb) + len(sb)
|
|
||||||
b := make([]byte, length, length)
|
|
||||||
|
|
||||||
b[0] = 0x30
|
|
||||||
b[1] = byte(length - 2)
|
|
||||||
b[2] = 0x02
|
|
||||||
b[3] = byte(len(rb))
|
|
||||||
offset := copy(b[4:], rb) + 4
|
|
||||||
b[offset] = 0x02
|
|
||||||
b[offset+1] = byte(len(sb))
|
|
||||||
copy(b[offset+2:], sb)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// expectedInputs returns the number of arguments required by a script.
|
// expectedInputs returns the number of arguments required by a script.
|
||||||
// If the script is of unnown type such that the number can not be determined
|
// If the script is of unnown type such that the number can not be determined
|
||||||
// then -1 is returned. We are an interanl function and thus assume that class
|
// then -1 is returned. We are an interanl function and thus assume that class
|
||||||
|
|
Loading…
Reference in a new issue