Change ECPair to be compatible with payment({pubkey:ecpair.publicKey})

This commit is contained in:
junderw 2018-12-30 11:23:28 +09:00
parent 35adaa8471
commit b8c2e9e339
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 9 additions and 9 deletions

View file

@ -20,8 +20,8 @@ interface ECPairOptions {
export interface ECPairInterface { export interface ECPairInterface {
compressed: boolean compressed: boolean
network: Network network: Network
privateKey: Buffer | null privateKey?: Buffer
publicKey: Buffer | null publicKey?: Buffer
toWIF(): string toWIF(): string
sign(hash: Buffer): Buffer sign(hash: Buffer): Buffer
verify(hash: Buffer, signature: Buffer): Buffer verify(hash: Buffer, signature: Buffer): Buffer
@ -31,25 +31,25 @@ export interface ECPairInterface {
class ECPair implements ECPairInterface { class ECPair implements ECPairInterface {
compressed: boolean compressed: boolean
network: Network network: Network
private __d: Buffer | null private __d?: Buffer
private __Q: Buffer | null private __Q?: Buffer
constructor (d?: Buffer, Q?: Buffer, options?: ECPairOptions) { constructor (d?: Buffer, Q?: Buffer, options?: ECPairOptions) {
if (options === undefined) options = {} if (options === undefined) options = {}
this.compressed = options.compressed === undefined ? true : options.compressed this.compressed = options.compressed === undefined ? true : options.compressed
this.network = options.network || NETWORKS.bitcoin this.network = options.network || NETWORKS.bitcoin
this.__d = null this.__d = undefined
this.__Q = null this.__Q = undefined
if (d !== undefined) this.__d = d if (d !== undefined) this.__d = d
if (Q !== undefined) this.__Q = ecc.pointCompress(Q, this.compressed) if (Q !== undefined) this.__Q = ecc.pointCompress(Q, this.compressed)
} }
get privateKey (): Buffer | null { get privateKey (): Buffer | undefined {
return this.__d return this.__d
} }
get publicKey (): Buffer | null { get publicKey (): Buffer | undefined {
if (!this.__Q) this.__Q = ecc.pointFromScalar(this.__d, this.compressed) if (!this.__Q) this.__Q = ecc.pointFromScalar(this.__d, this.compressed)
return this.__Q return this.__Q
} }

View file

@ -30,7 +30,7 @@ describe('ECPair', function () {
}) })
it('calls pointFromScalar lazily', hoodwink(function () { it('calls pointFromScalar lazily', hoodwink(function () {
assert.strictEqual(keyPair.__Q, null) assert.strictEqual(keyPair.__Q, undefined)
// .publicKey forces the memoization // .publicKey forces the memoization
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')