Shielded Bitcoin Multisig using Public Key tweaking
npm install @hyperdivision/shielded-multisig@hyperdivision/shielded-multisig
> Shielded Bitcoin Multisig using Public Key Tweaking
Create multisig wallets based on a set of public keys without access to secret
keys or revealing public keys.
``js
const shield = require('@hyperdivision/shielded-multisig')
// secp256k1 master keys used to redeem. Here we construct a 2-of-3 address
const threshold = 2
const masterKeys = [
Buffer.from('...', 'hex'),
Buffer.from('...', 'hex'),
Buffer.from('...', 'hex')
]
// id must be 32-byte Buffer.
// Could for example be a 256 bit hash of some user string
const id = Buffer.alloc(32)
id.writeUInt32LE(1) // Just use the Uint32LE value of 1 for this example
// Save the tweak somewhere, and give out the hash. The counter may be
// incremented internally if a specific value causes an invalid tweaked key
const { tweakData, address } = shield.address({ id, counter: 0 }, masterKeys, threshold)
// deepEqual(tweakDataS, tweakData) === true
const { tweakData: tweakDataS, script } = shield.redeemScript(tweakData, masterKeys, threshold)
`
Signing using bcoin:
`js
const shield = require('@hyperdivision/shielded-multisig')
const { tweakPrivate } = require('@hyperdivision/shielded-multisig/tweak')
const threshold = 2
const masterKeys = [
Buffer.from('...', 'hex'),
Buffer.from('...', 'hex'),
Buffer.from('...', 'hex')
]
const { tweakData, script } = shield.redeemScript({ / ... / }, masterKeys, threshold)
const privateKey = Buffer.from('...', 'hex')
// bcoin part
const { MTX, KeyRing } = require('bcoin')
const spend = MTX.fromJSON(/ transaction data /)
const ring = KeyRing.fromPrivate(tweakPrivate(tweakData, privateKey))
ring.witness = true
ring.script = script
var signed = spend.sign(ring)
if (signed !== spend.inputs.length) throw new Error('Did not sign all inputs')
// now spend is signed
`
Generate a new address from { id, counter }, masterKeys and threshold.address
The inputs uniquely and deterministically determine the .
id must be a 32-byte Buffer and could be eg. a username, account number,0
persisted random buffer or hash of some user information. The counter can be
used to generate multiple addresses for a single user or can be left at .tweakData
Note that may return a counter different from the one passed, if
the details of the algorithm results in an invalid public key. This is extremely
rare, but do not rely on the passed counter actually being the one that is used.
masterKeys must be an array of valid secp256k1 public keys encoded asBuffers. Threshold must be an integer less than or equal to the number of
master keys.
Returns a tweakData object, that you should persist for future use, andaddress which is a bcoin Address of a P2SH(P2WSH(m of n)) script that
can be encoded as desired.
Exactly the same as above, but return a bcoin Script instead of an Address.
Validate masterKeys for being points on the curve (ie valid keys)
Low-level facility to compute the tweaked public key. Note that you must provide
the full tweakData as returned by the above functions. Returns a Buffer
Low-level facility to compute the tweaked private key. Note that you must
provide the full tweakData as returned by the above functions. Returns aBuffer
Compute a non-reduced scalar from tweakData as a 32 byte Buffer. UsedtweakPublic
internally by and tweakPrivate
This module uses key tweaking based on Diffie-Hellman and hash functions.
A given master key pair is tweaked using a hash of tweakData, which works as
follows:
1. The tweakData is hashed using keyed BLAKE2b, as followstweak = BLAKE2b-256( 32-byte id || U32LE(counter) || U8(threshold), key = nonce).nonce
The is computed as the hash of the set of master keys sortednonce = BLAKE2b-256(CONCAT(SORT(masterKeys)))
lexicographically, and then concatenated:PK' = [tweak]·PK
2. The tweaked key pair can now be computed as andSK' = tweak * SK, where [x]·E denotes scalar multiplication of group elementE with scalar x, and x * y denotes field multiplication. The reason thissecp256k1
works is because is a prime order group, so every element of thePK = [SK]·G
group is a generator. Private/Secret keys are simply scalars over the finite
field, while public keys are , where G is the predefined base[tweak]·[SK]·G = [tweak * SK]·G
point of the group. However since any element of the group is a generator we can
use the public key as a generator for a new derived key. Since scalar
multiplication is associative, , so we can[tweak]·PK
recover the derived private key when we need to sign with for the tweaked public
key, by applying our tweak to our private key.
3. If the above results in the invalid public key we increment the
counter from step 1 and try again.
`sh``
npm install @hyperdivision/shielded-multisig