-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtweak.go
More file actions
54 lines (43 loc) · 1.62 KB
/
Copy pathtweak.go
File metadata and controls
54 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package arkade
import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
var (
TagArkScriptHash = []byte("ArkScriptHash")
TagArkWitnessHash = []byte("ArkWitnessHash")
)
// ArkadeScriptHash computes the hash of an ark script.
// scripthash = h_arkScriptHash(script)
func ArkadeScriptHash(script []byte) []byte {
hash := chainhash.TaggedHash(TagArkScriptHash, script)
return hash[:]
}
// ComputeArkadeScriptPublicKey calculates a top-level ark script public key given the hash of the arkscript
func ComputeArkadeScriptPublicKey(pubKey *btcec.PublicKey, scriptHash []byte) *btcec.PublicKey {
pubKey, _ = schnorr.ParsePubKey(schnorr.SerializePubKey(pubKey))
var (
pubKeyJacobian btcec.JacobianPoint
tweakJacobian btcec.JacobianPoint
resultJacobian btcec.JacobianPoint
)
tweakKey, _ := btcec.PrivKeyFromBytes(scriptHash)
btcec.ScalarBaseMultNonConst(&tweakKey.Key, &tweakJacobian)
pubKey.AsJacobian(&pubKeyJacobian)
btcec.AddNonConst(&pubKeyJacobian, &tweakJacobian, &resultJacobian)
resultJacobian.ToAffine()
return btcec.NewPublicKey(&resultJacobian.X, &resultJacobian.Y)
}
func ComputeArkadeScriptPrivateKey(privKey *btcec.PrivateKey, scriptHash []byte) *btcec.PrivateKey {
privKeyScalar := privKey.Key
pubKeyBytes := privKey.PubKey().SerializeCompressed()
if pubKeyBytes[0] == secp256k1.PubKeyFormatCompressedOdd {
privKeyScalar.Negate()
}
tweakScalar := new(btcec.ModNScalar)
tweakScalar.SetByteSlice(scriptHash)
tweakScalar.Add(&privKeyScalar)
return &btcec.PrivateKey{Key: *tweakScalar}
}