How to use the cbor.encodeCanonical function in cbor

To help you get started, we’ve selected a few cbor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kryptco / kr-u2f / src / webauthn.ts View on Github external
): Promise {
    const withoutAttestation = await createAuthenticatorDataWithoutAttestation(rpId, counter);

    const aaguid = KRYPTON_AAGUID;

    const credIdLen = new Uint8Array(2);
    credIdLen[0] = (credId.length >> 8) & 0xff;
    credIdLen[1] = credId.length & 0xff;

    const attData = new Map();
    attData.set(1, 2);
    attData.set(3, -7);
    attData.set(-1, 1);
    attData.set(-2, new Buffer(publicKey.slice(1, 33).buffer));    // x-coord
    attData.set(-3, new Buffer(publicKey.slice(33, 65).buffer));    // y-coord
    const attCBOR = new Uint8Array(CBOR.encodeCanonical(attData));

    const authenticatorData = new Uint8Array(   withoutAttestation.length
                                                + aaguid.length
                                                + credIdLen.byteLength
                                                + credId.length + attCBOR.byteLength);
    let offset = 0;

    authenticatorData.set(withoutAttestation, offset);
    offset += withoutAttestation.length;

    authenticatorData.set(aaguid, offset);
    offset += aaguid.length;

    authenticatorData.set(credIdLen, offset);
    offset += credIdLen.byteLength;
github kryptco / kr-u2f / src / background.ts View on Github external
throw new Error('no u2f_register_response');
    }
    if (response.u2f_register_response.error) {
        throw response.u2f_register_response.error;
    }

    const u2fRegisterResponse = response.u2f_register_response;

    const authenticatorData = await createAuthenticatorDataWithAttestation(rpId,
                                                                           u2fRegisterResponse.counter,
                                                                           u2fRegisterResponse.key_handle,
                                                                           u2fRegisterResponse.public_key);

    let attestationObject: ArrayBuffer;
    if (pkOptions.attestation == null || pkOptions.attestation === 'none') {
        attestationObject = CBOR.encodeCanonical({
            attStmt: {},
            authData: new Buffer(authenticatorData.buffer),
            fmt: 'none',
        }).buffer;
    } else {
        attestationObject = CBOR.encodeCanonical({
            attStmt: {
                sig: new Buffer(u2fRegisterResponse.signature.buffer),
                x5c: [new Buffer(u2fRegisterResponse.attestation_certificate.buffer)],
            },
            authData: new Buffer(authenticatorData.buffer),
            fmt: 'fido-u2f',
        }).buffer;
    }

    const credential: Credential = {
github Emurgo / tangata-manu / src / entities / byron-validator.js View on Github external
}
      const txOutputs = fullOutputs[inputTxId]
      if (!txOutputs) {
        throw new Error(`No UTxO is found for tx ${inputTxId}! Maybe the blockchain is still syncing? If not - something is wrong.`)
      }
      const { address: inputAddress, amount: inputAmount } = txOutputs[inputIdx]
      this.logger.debug(`Validating witness for input: ${inputTxId}.${inputIdx} (${inputAmount} coin from ${inputAddress})`)
      const { addressRoot, addrAttr, addressType } = ByronValidator.deconstructAddress(inputAddress)
      if (addressType !== 0) {
        this.logger.debug(`Unsupported address type: ${addressType}. Skipping witness validation for this input.`)
        return
      }
      const addressRootHex = addressRoot.toString('hex')
      const expectedStruct = [0, [0, sign[0]], addrAttr]
      const encodedStruct = Buffer.from(sha3_256.update(
        cbor.encodeCanonical(expectedStruct)).digest())
      const expectedRootHex = blake.blake2bHex(encodedStruct, undefined, 28)
      if (addressRootHex !== expectedRootHex) {
        throw new Error(`Witness does not match! ${JSON.stringify({ addressRootHex, expectedRoot: expectedRootHex })}`)
      }
    })
  }
github Emurgo / tangata-manu / src / entities / shelley-validator.js View on Github external
}
      const { address: inputAddress, amount: inputAmount } = txOutputs[inputIdx]
      this.logger.debug(`Validating witness for input: ${inputTxId}.${inputIdx} (${inputAmount} coin from ${inputAddress})`)
      const {
        addressRoot,
        addrAttr,
        addressType,
      } = ShelleyValidator.deconstructAddress(inputAddress)
      if (addressType !== 0) {
        this.logger.debug(`Unsupported address type: ${addressType}. Skipping witness validation for this input.`)
        return
      }
      const addressRootHex = addressRoot.toString('hex')
      const expectedStruct = [0, [0, sign[0]], addrAttr]
      const encodedStruct = Buffer.from(sha3_256.update(
        cbor.encodeCanonical(expectedStruct)).digest())
      const expectedRootHex = blake.blake2bHex(encodedStruct, undefined, 28)
      if (addressRootHex !== expectedRootHex) {
        throw new Error(`Witness does not match! ${JSON.stringify({ addressRootHex, expectedRoot: expectedRootHex })}`)
      }
    })
  }

cbor

Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).

MIT
Latest version published 3 months ago

Package Health Score

78 / 100
Full package analysis