export async function buildKey()

in packages/sqrl/src/function/KeyFunctions.ts [19:55]


export async function buildKey(
  ctx: Context,
  counterEntity: SqrlEntity,
  ...featureValues: Array<any>
): Promise<SqrlKey | null> {
  const hasEmpty = featureValues.some((v) => {
    return v === null || v === "";
  });
  if (hasEmpty) {
    return null;
  }

  // Search through all the values for Entity/UniqueId
  let timeMs = counterEntity.uniqueId.getTimeMs();
  for (const value of featureValues) {
    if (value instanceof SqrlObject) {
      timeMs = Math.max(timeMs, value.tryGetTimeMs() || 0);
    }
  }

  const basicFeatureValues = await nice(() =>
    SqrlObject.ensureBasic(featureValues)
  );
  let featuresHash: Buffer;
  if (featureValues.length) {
    featuresHash = await murmurhashJson(basicFeatureValues);
  } else {
    featuresHash = new Buffer(16);
  }
  return new SqrlKey(
    ctx.requireDatabaseSet(),
    counterEntity,
    basicFeatureValues,
    timeMs,
    featuresHash
  );
}