{"version":3,"sources":["../../src/core/authenticationKey.ts"],"sourcesContent":["// Copyright © Aptos Foundation\n// SPDX-License-Identifier: Apache-2.0\n\nimport { sha3_256 as sha3Hash } from \"@noble/hashes/sha3\";\nimport { AccountAddress } from \"./accountAddress\";\nimport type { AccountPublicKey } from \"./crypto\";\nimport { Hex } from \"./hex\";\nimport { AuthenticationKeyScheme, HexInput } from \"../types\";\nimport { Serializable, Serializer } from \"../bcs/serializer\";\nimport { Deserializer } from \"../bcs/deserializer\";\n\n/**\n * Each account stores an authentication key. Authentication key enables account owners to rotate\n * their private key(s) associated with the account without changing the address that hosts their account.\n * @see {@link https://aptos.dev/concepts/accounts | Account Basics}\n *\n * Account addresses can be derived from AuthenticationKey\n */\nexport class AuthenticationKey extends Serializable {\n  /**\n   * An authentication key is always a SHA3-256 hash of data, and is always 32 bytes.\n   *\n   * The data to hash depends on the underlying public key type and the derivation scheme.\n   */\n  static readonly LENGTH: number = 32;\n\n  /**\n   * The raw bytes of the authentication key.\n   */\n  public readonly data: Hex;\n\n  constructor(args: { data: HexInput }) {\n    super();\n    const { data } = args;\n    const hex = Hex.fromHexInput(data);\n    if (hex.toUint8Array().length !== AuthenticationKey.LENGTH) {\n      throw new Error(`Authentication Key length should be ${AuthenticationKey.LENGTH}`);\n    }\n    this.data = hex;\n  }\n\n  serialize(serializer: Serializer): void {\n    serializer.serializeFixedBytes(this.data.toUint8Array());\n  }\n\n  /**\n   * Deserialize an AuthenticationKey from the byte buffer in a Deserializer instance.\n   * @param deserializer The deserializer to deserialize the AuthenticationKey from.\n   * @returns An instance of AuthenticationKey.\n   */\n  static deserialize(deserializer: Deserializer): AuthenticationKey {\n    const bytes = deserializer.deserializeFixedBytes(AuthenticationKey.LENGTH);\n    return new AuthenticationKey({ data: bytes });\n  }\n\n  toString(): string {\n    return this.data.toString();\n  }\n\n  toUint8Array(): Uint8Array {\n    return this.data.toUint8Array();\n  }\n\n  static fromSchemeAndBytes(args: { scheme: AuthenticationKeyScheme; input: HexInput }): AuthenticationKey {\n    const { scheme, input } = args;\n    const inputBytes = Hex.fromHexInput(input).toUint8Array();\n    const hashInput = new Uint8Array([...inputBytes, scheme]);\n    const hash = sha3Hash.create();\n    hash.update(hashInput);\n    const hashDigest = hash.digest();\n    return new AuthenticationKey({ data: hashDigest });\n  }\n\n  /**\n   * @deprecated Use `fromPublicKey` instead\n   * Derives an AuthenticationKey from the public key seed bytes and an explicit derivation scheme.\n   *\n   * This facilitates targeting a specific scheme for deriving an authentication key from a public key.\n   *\n   * @param args - the public key and scheme to use for the derivation\n   */\n  public static fromPublicKeyAndScheme(args: { publicKey: AccountPublicKey; scheme: AuthenticationKeyScheme }) {\n    const { publicKey } = args;\n    return publicKey.authKey();\n  }\n\n  /**\n   * Converts a PublicKey(s) to an AuthenticationKey, using the derivation scheme inferred from the\n   * instance of the PublicKey type passed in.\n   *\n   * @param args.publicKey\n   * @returns AuthenticationKey\n   */\n  static fromPublicKey(args: { publicKey: AccountPublicKey }): AuthenticationKey {\n    const { publicKey } = args;\n    return publicKey.authKey();\n  }\n\n  /**\n   * Derives an account address from an AuthenticationKey. Since an AccountAddress is also 32 bytes,\n   * the AuthenticationKey bytes are directly translated to an AccountAddress.\n   *\n   * @returns AccountAddress\n   */\n  derivedAddress(): AccountAddress {\n    return new AccountAddress(this.data.toUint8Array());\n  }\n}\n"],"mappings":"2HAGA,OAAS,YAAYA,MAAgB,qBAe9B,IAAMC,EAAN,MAAMA,UAA0BC,CAAa,CAalD,YAAYC,EAA0B,CACpC,MAAM,EACN,GAAM,CAAE,KAAAC,CAAK,EAAID,EACXE,EAAMC,EAAI,aAAaF,CAAI,EACjC,GAAIC,EAAI,aAAa,EAAE,SAAWJ,EAAkB,OAClD,MAAM,IAAI,MAAM,uCAAuCA,EAAkB,MAAM,EAAE,EAEnF,KAAK,KAAOI,CACd,CAEA,UAAUE,EAA8B,CACtCA,EAAW,oBAAoB,KAAK,KAAK,aAAa,CAAC,CACzD,CAOA,OAAO,YAAYC,EAA+C,CAChE,IAAMC,EAAQD,EAAa,sBAAsBP,EAAkB,MAAM,EACzE,OAAO,IAAIA,EAAkB,CAAE,KAAMQ,CAAM,CAAC,CAC9C,CAEA,UAAmB,CACjB,OAAO,KAAK,KAAK,SAAS,CAC5B,CAEA,cAA2B,CACzB,OAAO,KAAK,KAAK,aAAa,CAChC,CAEA,OAAO,mBAAmBN,EAA+E,CACvG,GAAM,CAAE,OAAAO,EAAQ,MAAAC,CAAM,EAAIR,EACpBS,EAAaN,EAAI,aAAaK,CAAK,EAAE,aAAa,EAClDE,EAAY,IAAI,WAAW,CAAC,GAAGD,EAAYF,CAAM,CAAC,EAClDI,EAAOC,EAAS,OAAO,EAC7BD,EAAK,OAAOD,CAAS,EACrB,IAAMG,EAAaF,EAAK,OAAO,EAC/B,OAAO,IAAIb,EAAkB,CAAE,KAAMe,CAAW,CAAC,CACnD,CAUA,OAAc,uBAAuBb,EAAwE,CAC3G,GAAM,CAAE,UAAAc,CAAU,EAAId,EACtB,OAAOc,EAAU,QAAQ,CAC3B,CASA,OAAO,cAAcd,EAA0D,CAC7E,GAAM,CAAE,UAAAc,CAAU,EAAId,EACtB,OAAOc,EAAU,QAAQ,CAC3B,CAQA,gBAAiC,CAC/B,OAAO,IAAIC,EAAe,KAAK,KAAK,aAAa,CAAC,CACpD,CACF,EAzFajB,EAMK,OAAiB,GAN5B,IAAMkB,EAANlB","names":["sha3Hash","_AuthenticationKey","Serializable","args","data","hex","Hex","serializer","deserializer","bytes","scheme","input","inputBytes","hashInput","hash","sha3Hash","hashDigest","publicKey","AccountAddress","AuthenticationKey"]}