Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some docs issues and improve fromSecretKey methods on keypair classes #19581

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/green-tables-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@mysten/sui': minor
'@mysten/bcs': minor
---

Updated hex, base64, and base58 utility names for better consistency

All existing methods will continue to work, but the following methods have been deprecated and replaced with methods with improved names:

- `toHEX` -> `toHEX`
- `fromHEX` -> `fromHex`
- `toB64` -> `toBase64`
- `fromB64` -> `fromBase64`
- `toB58` -> `toBase58`
- `fromB58` -> `fromBase58`
5 changes: 5 additions & 0 deletions .changeset/thin-trees-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/sui': minor
---

support Bech32 secrets in the Keypair.fromSecretKey methods
4 changes: 2 additions & 2 deletions apps/wallet/src/background/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type MethodPayload,
} from '_src/shared/messaging/messages/payloads/MethodPayload';
import { type WalletStatusChange } from '_src/shared/messaging/messages/payloads/wallet-status-change';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';
import Dexie from 'dexie';

import { getAccountSourceByID } from '../account-sources';
Expand Down Expand Up @@ -236,7 +236,7 @@ export async function accountsHandleUIMessage(msg: Message, uiConnection: UiConn
{
type: 'method-payload',
method: 'signDataResponse',
args: { signature: await account.signData(fromB64(data)) },
args: { signature: await account.signData(fromBase64(data)) },
},
msg.id,
),
Expand Down
12 changes: 6 additions & 6 deletions apps/wallet/src/dapp-interface/WalletStandardInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { type SignMessageRequest } from '_src/shared/messaging/messages/payloads
import { isWalletStatusChangePayload } from '_src/shared/messaging/messages/payloads/wallet-status-change';
import { bcs } from '@mysten/sui/bcs';
import { isTransaction } from '@mysten/sui/transactions';
import { fromB64, toB64 } from '@mysten/sui/utils';
import { fromBase64, toBase64 } from '@mysten/sui/utils';
import {
ReadonlyWalletAccount,
SUI_CHAINS,
Expand Down Expand Up @@ -171,7 +171,7 @@ export class SuiWallet implements Wallet {
new ReadonlyWalletAccount({
address,
label: nickname || undefined,
publicKey: publicKey ? fromB64(publicKey) : new Uint8Array(),
publicKey: publicKey ? fromBase64(publicKey) : new Uint8Array(),
chains: this.#activeChain ? [this.#activeChain] : [],
features: ['sui:signAndExecuteTransaction'],
}),
Expand Down Expand Up @@ -334,15 +334,15 @@ export class SuiWallet implements Wallet {
txSignatures: [signature],
intentMessage: { value: bcsTransaction },
},
] = bcs.SenderSignedData.parse(fromB64(rawTransaction!));
] = bcs.SenderSignedData.parse(fromBase64(rawTransaction!));

const bytes = bcs.TransactionData.serialize(bcsTransaction).toBase64();

return {
digest,
signature,
bytes,
effects: toB64(new Uint8Array(rawEffects!)),
effects: toBase64(new Uint8Array(rawEffects!)),
};
},
);
Expand All @@ -353,7 +353,7 @@ export class SuiWallet implements Wallet {
this.#send<SignMessageRequest, SignMessageRequest>({
type: 'sign-message-request',
args: {
message: toB64(message),
message: toBase64(message),
accountAddress: account.address,
},
}),
Expand All @@ -371,7 +371,7 @@ export class SuiWallet implements Wallet {
this.#send<SignMessageRequest, SignMessageRequest>({
type: 'sign-message-request',
args: {
message: toB64(message),
message: toBase64(message),
accountAddress: account.address,
},
}),
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/shared/utils/from-exported-keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { Secp256k1Keypair } from '@mysten/sui/keypairs/secp256k1';
import { Secp256r1Keypair } from '@mysten/sui/keypairs/secp256r1';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';

/**
* Wallet stored data might contain imported accounts with their keys stored in the previous format.
Expand All @@ -31,7 +31,7 @@ export function fromExportedKeypair(
if (!legacySupport) {
throw new Error('Invalid type of secret key. A string value was expected.');
}
secretKey = fromB64(secret.privateKey);
secretKey = fromBase64(secret.privateKey);
schema = secret.schema;
} else {
const decoded = decodeSuiPrivateKey(secret);
Expand Down
6 changes: 3 additions & 3 deletions apps/wallet/src/shared/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useAppSelector } from '_hooks';
import { setAttributes } from '_src/shared/experimentation/features';
import { useGrowthBook } from '@growthbook/growthbook-react';
import { fromB64, toB64 } from '@mysten/sui/utils';
import { fromBase64, toBase64 } from '@mysten/sui/utils';
import * as Sentry from '@sentry/browser';
import { useEffect } from 'react';
import Browser from 'webextension-polyfill';
Expand Down Expand Up @@ -77,8 +77,8 @@ export function toSearchQueryString(searchParams: URLSearchParams) {
}

export function toUtf8OrB64(message: string | Uint8Array) {
const messageBytes = typeof message === 'string' ? fromB64(message) : message;
let messageToReturn: string = typeof message === 'string' ? message : toB64(message);
const messageBytes = typeof message === 'string' ? fromBase64(message) : message;
let messageToReturn: string = typeof message === 'string' ? message : toBase64(message);
let type: 'utf8' | 'base64' = 'base64';
try {
messageToReturn = new TextDecoder('utf8', { fatal: true }).decode(messageBytes);
Expand Down
8 changes: 4 additions & 4 deletions apps/wallet/src/ui/app/QredoSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '_src/shared/qredo-api';
import { type SuiClient } from '@mysten/sui/client';
import { messageWithIntent } from '@mysten/sui/cryptography';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import mitt from 'mitt';

import { WalletSigner } from './WalletSigner';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class QredoSigner extends WalletSigner {
clientIdentifier,
);
return {
messageBytes: toB64(input.message),
messageBytes: toBase64(input.message),
signature,
};
};
Expand All @@ -98,7 +98,7 @@ export class QredoSigner extends WalletSigner {
clientIdentifier,
);
return {
transactionBlockBytes: toB64(transactionBlockBytes),
transactionBlockBytes: toBase64(transactionBlockBytes),
signature,
};
};
Expand Down Expand Up @@ -157,7 +157,7 @@ export class QredoSigner extends WalletSigner {
throw new Error(`Unsupported network ${networkNames[this.#apiEnv]}`);
}
const qredoTransaction = await this.#qredoAPI.createTransaction({
messageWithIntent: toB64(intent),
messageWithIntent: toBase64(intent),
network: this.#network,
broadcast,
from: await this.getAddress(),
Expand Down
8 changes: 4 additions & 4 deletions apps/wallet/src/ui/app/WalletSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@mysten/sui/client';
import { messageWithIntent } from '@mysten/sui/cryptography';
import { isTransaction, type Transaction } from '@mysten/sui/transactions';
import { fromB64, toB64 } from '@mysten/sui/utils';
import { fromBase64, toBase64 } from '@mysten/sui/utils';

export type SignedTransaction = {
transactionBlockBytes: string;
Expand Down Expand Up @@ -43,7 +43,7 @@ export abstract class WalletSigner {
);

return {
messageBytes: toB64(input.message),
messageBytes: toBase64(input.message),
signature,
};
}
Expand All @@ -59,7 +59,7 @@ export abstract class WalletSigner {
}

if (typeof transactionBlock === 'string') {
return fromB64(transactionBlock);
return fromBase64(transactionBlock);
}

if (transactionBlock instanceof Uint8Array) {
Expand All @@ -78,7 +78,7 @@ export abstract class WalletSigner {
const signature = await this.signData(messageWithIntent('TransactionData', bytes));

return {
transactionBlockBytes: toB64(bytes),
transactionBlockBytes: toBase64(bytes),
signature,
};
}
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/ui/app/background-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { type SignedMessage, type SignedTransaction } from '_src/ui/app/WalletSigner';
import type { AppDispatch } from '_store';
import { type SuiTransactionBlockResponse } from '@mysten/sui/client';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import { type QueryKey } from '@tanstack/react-query';
import { lastValueFrom, map, take } from 'rxjs';

Expand Down Expand Up @@ -156,7 +156,7 @@ export class BackgroundClient {
createMessage<MethodPayload<'signData'>>({
type: 'method-payload',
method: 'signData',
args: { data: toB64(data), id: addressOrID },
args: { data: toBase64(data), id: addressOrID },
}),
).pipe(
take(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useAppsBackend } from '@mysten/core';
import { useSuiClient } from '@mysten/dapp-kit';
import { type Transaction } from '@mysten/sui/transactions';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import { useQuery } from '@tanstack/react-query';

import {
Expand Down Expand Up @@ -45,7 +45,7 @@ export function useDappPreflight({

if (requestType === RequestType.SIGN_TRANSACTION && transaction) {
const transactionBytes = await transaction.build({ client });
body.transactionBytes = toB64(transactionBytes);
body.transactionBytes = toBase64(transactionBytes);
}

return request<DappPreflightResponse>(
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/ui/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { setNavVisibility } from '_redux/slices/app';
import { isLedgerAccountSerializedUI } from '_src/background/accounts/LedgerAccount';
import { persistableStorage } from '_src/shared/analytics/amplitude';
import { type LedgerAccountsPublicKeys } from '_src/shared/messaging/messages/payloads/MethodPayload';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import { useEffect, useMemo } from 'react';
import { Navigate, Outlet, Route, Routes, useLocation } from 'react-router-dom';
import { throttle } from 'throttle-debounce';
Expand Down Expand Up @@ -115,7 +115,7 @@ const App = () => {
const { publicKey } = await suiLedgerClient.getPublicKey(derivationPath);
publicKeysToStore.push({
accountID: id,
publicKey: toB64(publicKey),
publicKey: toBase64(publicKey),
});
} catch (e) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Text } from '_src/ui/app/shared/text';
import { ChevronDown12, ChevronRight12 } from '@mysten/icons';
import { type Argument, type Commands, type TransactionData } from '@mysten/sui/transactions';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import { useState } from 'react';

type TransactionType = TransactionData['commands'][0];
Expand Down Expand Up @@ -34,7 +34,7 @@ function convertCommandArgumentToString(
if (Array.isArray(arg)) {
// Publish transaction special casing:
if (typeof arg[0] === 'number') {
return toB64(new Uint8Array(arg as number[]));
return toBase64(new Uint8Array(arg as number[]));
}

return `[${arg.map((argVal) => convertCommandArgumentToString(argVal)).join(', ')}]`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useGetQredoTransaction } from '_src/ui/app/hooks/useGetQredoTransaction
import { Text } from '_src/ui/app/shared/text';
import { formatDate, useOnScreen } from '@mysten/core';
import { bcs } from '@mysten/sui/bcs';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';
import { useMemo, useRef } from 'react';

export type QredoTransactionProps = {
Expand All @@ -26,7 +26,7 @@ export function QredoTransaction({ qredoID, qredoTransactionID }: QredoTransacti
});
const messageWithIntent = useMemo(() => {
if (data?.MessageWithIntent) {
return fromB64(data.MessageWithIntent);
return fromBase64(data.MessageWithIntent);
}
return null;
}, [data?.MessageWithIntent]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import type { AppThunkConfig } from '_store/thunk-extras';
import { type SuiTransactionBlockResponse } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';
import { createAsyncThunk, createEntityAdapter, createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';

Expand Down Expand Up @@ -56,7 +56,7 @@ export const respondToTransactionRequest = createAsyncThunk<
if (txRequest.tx.type === 'sign-message') {
txResult = await signer.signMessage(
{
message: fromB64(txRequest.tx.message),
message: fromBase64(txRequest.tx.message),
},
clientIdentifier,
);
Expand Down
4 changes: 2 additions & 2 deletions dapps/multisig-toolkit/src/routes/signature-analyzer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { parseSerializedSignature, PublicKey, SignatureScheme } from '@mysten/sui/cryptography';
import { parsePartialSignatures } from '@mysten/sui/multisig';
import { toB64 } from '@mysten/sui/utils';
import { toBase64 } from '@mysten/sui/utils';
import { publicKeyFromRawBytes } from '@mysten/sui/verify';
import { AlertCircle } from 'lucide-react';
import { useState } from 'react';
Expand Down Expand Up @@ -47,7 +47,7 @@ function Signature({ signature, index }: { signature: SignaturePubkeyPair; index
{ label: 'Signature Public Key', value: pubkey },
{ label: 'Sui Format Public Key ( flag | pk )', value: pubkey_base64_sui_format },
{ label: 'Sui Address', value: suiAddress },
{ label: 'Signature', value: toB64(signature.signature) },
{ label: 'Signature', value: toBase64(signature.signature) },
];

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ import { readFileSync } from 'fs';
import { homedir } from 'os';
import path from 'path';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';

const sender = execSync(`${SUI} client active-address`, { encoding: 'utf8' }).trim();
const signer = (() => {
Expand All @@ -562,7 +562,7 @@ const signer = (() => {
);

for (const priv of keystore) {
const raw = fromB64(priv);
const raw = fromBase64(priv);
if (raw[0] !== 0) {
continue;
}
Expand Down Expand Up @@ -648,7 +648,7 @@ import { fileURLToPath } from 'url';
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { Transaction } from '@mysten/sui/transactions';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';

const SUI = 'sui';
const POLICY_PACKAGE_ID = '<POLICY-PACKAGE>';
Expand All @@ -659,7 +659,7 @@ const signer = (() => {
);

for (const priv of keystore) {
const raw = fromB64(priv);
const raw = fromBase64(priv);
if (raw[0] !== 0) {
continue;
}
Expand Down Expand Up @@ -888,7 +888,7 @@ import { fileURLToPath } from 'url';
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { Transaction, UpgradePolicy } from '@mysten/sui/transactions';
import { fromB64 } from '@mysten/sui/utils';
import { fromBase64 } from '@mysten/sui/utils';

const SUI = 'sui';
const POLICY_PACKAGE_ID = '<POLICY-PACKAGE>';
Expand All @@ -901,7 +901,7 @@ const signer = (() => {
);

for (const priv of keystore) {
const raw = fromB64(priv);
const raw = fromBase64(priv);
if (raw[0] !== 0) {
continue;
}
Expand Down
Loading
Loading