Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add const accountType{SINGLE: 0, MULTI: 1}
  • Loading branch information
wk3368 committed Oct 26, 2021
1 parent cd4e81a commit e19d61f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/encoding/index.ts
Expand Up @@ -13,6 +13,7 @@ import {
TransactionPayload,
TypeTag,
ReceiptIdentifierView,
accountType,
} from '../types';
import { fromHexString, toHexString } from '../utils/hex';
import { Deserializer } from '../lib/runtime/serde';
Expand Down Expand Up @@ -271,7 +272,7 @@ export async function privateKeyToPublicKey(privateKey: string): Promise<string>
}

// singleMulti: 0-single, 1-multi
export function publicKeyToAuthKey(publicKey: string, singleMulti = 0): string {
export function publicKeyToAuthKey(publicKey: string, singleMulti = accountType.SINGLE): string {
const hasher = sha3_256.create()
hasher.update(fromHexString(publicKey))
hasher.update(fromHexString(hexlify(singleMulti)))
Expand All @@ -280,7 +281,7 @@ export function publicKeyToAuthKey(publicKey: string, singleMulti = 0): string {
}

// singleMulti: 0-single, 1-multi
export function publicKeyToAddress(publicKey: string, singleMulti = 0): string {
export function publicKeyToAddress(publicKey: string, singleMulti = accountType.SINGLE): string {
const hasher = sha3_256.create()
hasher.update(fromHexString(publicKey))
hasher.update(fromHexString(hexlify(singleMulti)))
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Expand Up @@ -20,6 +20,12 @@ export type MultiEd25519PublicKey = string;
export type MultiEd25519Signature = string;
export type EventKey = string;
export type HexString = string;

export const accountType = {
SINGLE: 0,
MULTI: 1,
}

export interface StructTag {
address: string;
module: string;
Expand Down
5 changes: 3 additions & 2 deletions src/utils/account.ts
Expand Up @@ -4,6 +4,7 @@ import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import { hexlify } from '@ethersproject/bytes';
import { privateKeyToPublicKey, publicKeyToAuthKey, publicKeyToAddress, encodeReceiptIdentifier, bcsEncode } from "../encoding";
import { MultiEd25519KeyShard } from "../lib/runtime/starcoin_types";
import { accountType } from "../types";

export function generatePrivateKey(): string {
// 32-byte Uint8Array
Expand Down Expand Up @@ -46,9 +47,9 @@ export function showMultiEd25519Account(shard: MultiEd25519KeyShard): Record<str

const publicKey = hexlify(multiEd25519PublicKey.into())

const authKey = publicKeyToAuthKey(publicKey, 1)
const authKey = publicKeyToAuthKey(publicKey, accountType.MULTI)

const address = publicKeyToAddress(publicKey, 1)
const address = publicKeyToAddress(publicKey, accountType.MULTI)

// same with Rust, receiptIdentifier do not include authKey
const receiptIdentifier = encodeReceiptIdentifier(stripHexPrefix(address))
Expand Down

0 comments on commit e19d61f

Please sign in to comment.