Skip to content

Commit

Permalink
Refactor fingerprint construction
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Sep 14, 2021
1 parent 8737bf3 commit 15899b4
Show file tree
Hide file tree
Showing 7 changed files with 2,397 additions and 2,754 deletions.
19 changes: 17 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2017,
sourceType: "module"
project: './tsconfig.typecheck.json',
sourceType: "module",
},
env: {
browser: true,
mocha: true,
node: true,
'jest/globals': true,
webextensions: true,
},
extends: [
"plugin:@typescript-eslint/recommended"
'airbnb-typescript/base',
"plugin:@typescript-eslint/recommended",
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:jest/recommended',
'prettier',
],
plugins: ['@typescript-eslint', 'import', 'prettier'],
rules: {
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': 'off',
}
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
dist/
dist/
.vscode
41 changes: 27 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@ import { bech32 } from "bech32";

const DATA = "asset";

export default class assetFingerprint {
hashBuf: Uint8Array;
export default class AssetFingerprint {
readonly hashBuf: Uint8Array;

constructor(policyId: Uint8Array = new Uint8Array(32), assetName: Uint8Array = new Uint8Array(0)) {
private constructor(hashBuf: Uint8Array) {
this.hashBuf = hashBuf;
}

static fromHash(hash: Uint8Array): AssetFingerprint {
return new AssetFingerprint(hash);
}

static fromParts(
policyId: Uint8Array,
assetName: Uint8Array
): AssetFingerprint {
// see https://github.com/cardano-foundation/CIPs/pull/64
this.hashBuf = blake2b(20)
const hashBuf = blake2b(20)
.update(new Uint8Array([...policyId, ...assetName]))
.digest("binary");
}

fromHash(hash: Buffer): this {
this.hashBuf = hash
return this;
return AssetFingerprint.fromHash(hashBuf);
}

fromBech32(fingerprint: string): this {
static fromBech32(fingerprint: string): AssetFingerprint {
const { prefix, words } = bech32.decode(fingerprint);
if(prefix !== DATA){
if (prefix !== DATA) {
throw new Error("Invalid asset fingerprint");
}
this.hashBuf = Buffer.from(bech32.fromWords(words));
return this;

const hashBuf = Buffer.from(bech32.fromWords(words));
return AssetFingerprint.fromHash(hashBuf);
}

fingerprint(): string {
Expand All @@ -36,10 +44,15 @@ export default class assetFingerprint {
}

hash(): string {
return Buffer.from(this.hashBuf).toString('hex');
return Buffer.from(this.hashBuf).toString("hex");
}

prefix(): string {
return DATA;
}

// The last six characters of the data part form a checksum and contain no information
checksum(): string {
return this.fingerprint().slice(-6);
}
}
Loading

0 comments on commit 15899b4

Please sign in to comment.