All files / src/chain signature.ts

94.11% Statements 48/51
84.21% Branches 16/19
100% Functions 11/11
94.11% Lines 48/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                  1x 1x                 215x 43x   172x 145x 145x 145x 145x 145x   145x 145x 145x 145x   27x     27x 27x 27x     27x 27x 27x 27x               9x 9x 1x 1x 1x 1x 1x 1x 1x 1x   8x         182x 182x       2x 2x         24x 24x 23x         23x         28x 28x         25x         38x         9x 9x         32x      
import {ABIDecoder} from '../serializer/decoder'
import {ABIEncoder} from '../serializer/encoder'
import {ABISerializableObject} from '../serializer/serializable'
 
import {Base58} from '../base58'
import {isInstanceOf} from '../utils'
 
import {recover} from '../crypto/recover'
import {verify} from '../crypto/verify'
 
import {Bytes, BytesType, Checksum256, Checksum256Type, KeyType, PublicKey} from '../'
 
export type SignatureType =
    | Signature
    | string
    | {type: string; r: Uint8Array; s: Uint8Array; recid: number}
 
export class Signature implements ABISerializableObject {
    static abiName = 'signature'
 
    /** Type, e.g. `K1` */
    type: KeyType
    /** Signature data. */
    data: Bytes
 
    /** Create Signature object from representing types. */
    static from(value: SignatureType) {
        if (isInstanceOf(value, Signature)) {
            return value
        }
        if (typeof value === 'object' && value.r && value.s) {
            const data = new Uint8Array(1 + 32 + 32)
            let recid = value.recid
            const type = KeyType.from(value.type)
            if (value.type === KeyType.K1 || value.type === KeyType.R1) {
                recid += 31
            }
            data[0] = recid
            data.set(value.r, 1)
            data.set(value.s, 33)
            return new Signature(type, new Bytes(data))
        }
        Iif (typeof value !== 'string') {
            throw new Error('Invalid signature')
        }
        if (value.startsWith('SIG_')) {
            const parts = value.split('_')
            Iif (parts.length !== 3) {
                throw new Error('Invalid signature string')
            }
            const type = KeyType.from(parts[1])
            const size = type === KeyType.K1 || type === KeyType.R1 ? 65 : undefined
            const data = Base58.decodeRipemd160Check(parts[2], size, type)
            return new Signature(type, data)
        } else {
            Ethrow new Error('Invalid signature string')
        }
    }
 
    /** @internal */
    static fromABI(decoder: ABIDecoder) {
        const type = KeyType.from(decoder.readByte())
        if (type === KeyType.WA) {
            const startPos = decoder.getPosition()
            decoder.advance(65) // compact_signature
            decoder.advance(decoder.readVaruint32()) // auth_data
            decoder.advance(decoder.readVaruint32()) // client_json
            const len = decoder.getPosition() - startPos
            decoder.setPosition(startPos)
            const data = Bytes.from(decoder.readArray(len))
            return new Signature(KeyType.WA, data)
        }
        return new Signature(type, new Bytes(decoder.readArray(65)))
    }
 
    /** @internal */
    constructor(type: KeyType, data: Bytes) {
        this.type = type
        this.data = data
    }
 
    equals(other: SignatureType) {
        const otherSig = Signature.from(other)
        return this.type === otherSig.type && this.data.equals(otherSig.data)
    }
 
    /** Recover public key from given message digest. */
    recoverDigest(digest: Checksum256Type) {
        digest = Checksum256.from(digest)
        const compressed = recover(this.data.array, digest.array, this.type)
        return PublicKey.from({compressed, type: this.type})
    }
 
    /** Recover public key from given message. */
    recoverMessage(message: BytesType) {
        return this.recoverDigest(Checksum256.hash(message))
    }
 
    /** Verify this signature with given message digest and public key. */
    verifyDigest(digest: Checksum256Type, publicKey: PublicKey) {
        digest = Checksum256.from(digest)
        return verify(this.data.array, digest.array, publicKey.getCompressedKeyBytes(), this.type)
    }
 
    /** Verify this signature with given message and public key. */
    verifyMessage(message: BytesType, publicKey: PublicKey) {
        return this.verifyDigest(Checksum256.hash(message), publicKey)
    }
 
    /** Base58check encoded string representation of this signature (`SIG_<type>_<data>`). */
    toString() {
        return `SIG_${this.type}_${Base58.encodeRipemd160Check(this.data, this.type)}`
    }
 
    /** @internal */
    toABI(encoder: ABIEncoder) {
        encoder.writeByte(KeyType.indexFor(this.type))
        encoder.writeArray(this.data.array)
    }
 
    /** @internal */
    toJSON() {
        return this.toString()
    }
}