Skip to content

Commit

Permalink
feat: implement tao specific functions
Browse files Browse the repository at this point in the history
TICKET: WIN-4296
  • Loading branch information
yash-bitgo committed Jan 28, 2025
1 parent dac5faa commit 0447326
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
10 changes: 1 addition & 9 deletions modules/abstract-substrate/src/abstractSubstrateCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,7 @@ export class SubstrateCoin extends BaseCoin {

/** @inheritDoc **/
generateKeyPair(seed?: Buffer): KeyPair {
const keyPair = seed ? utils.keyPairFromSeed(new Uint8Array(seed)) : new SubstrateKeyPair();
const keys = keyPair.getKeys();
if (!keys.prv) {
throw new Error('Missing prv in key generation.');
}
return {
pub: keys.pub,
prv: keys.prv,
};
throw new Error('Method not implemented.');
}

/** @inheritDoc **/
Expand Down
9 changes: 5 additions & 4 deletions modules/abstract-substrate/src/lib/keyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as nacl from 'tweetnacl';
const TYPE = 'ed25519';
const keyring = new Keyring({ type: TYPE });

export class KeyPair extends Ed25519KeyPair {
export abstract class KeyPair extends Ed25519KeyPair {
/**
* Public constructor. By default, creates a key pair with a random master seed.
*
Expand Down Expand Up @@ -46,10 +46,11 @@ export class KeyPair extends Ed25519KeyPair {
* or substrate format used for westend (starts with 5)
*/
getAddress(format: DotAddressFormat): string {
let encodedAddress = this.createPolkadotPair().address;
encodedAddress = keyring.encodeAddress(encodedAddress, format as number);
// let encodedAddress = this.createPolkadotPair().address;
// encodedAddress = keyring.encodeAddress(encodedAddress, format as number);

return encodedAddress;
// return encodedAddress;
throw new Error('Method not implemented.');
}

/** @inheritdoc */
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-coin-tao/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Utils from './utils';
import * as Interface from './iface';

export { KeyPair } from './keyPair';
export { TaoKeyPair } from './keyPair';
export { Transaction } from './transaction';
export { TransactionBuilder } from './transactionBuilder';
export { TransferBuilder } from './transferBuilder';
Expand Down
44 changes: 28 additions & 16 deletions modules/sdk-coin-tao/src/lib/keyPair.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { DefaultKeys, Ed25519KeyPair } from '@bitgo/sdk-core';
import { DotAddressFormat } from '@bitgo/sdk-core';
import { KeyPair } from '@bitgo/abstract-substrate';
import { Keyring } from '@polkadot/keyring';
import { createPair } from '@polkadot/keyring/pair';
import { KeyringPair } from '@polkadot/keyring/types';

export class KeyPair extends Ed25519KeyPair {
/** @inheritdoc */
getKeys(): DefaultKeys {
throw new Error('Method not implemented.');
}
export class TaoKeyPair extends KeyPair {
TYPE = 'ed25519';
keyring = new Keyring({ type: TYPE });

/** @inheritdoc */
recordKeysFromPrivateKeyInProtocolFormat(prv: string): DefaultKeys {
throw new Error('Method not implemented.');
/**
* Helper function to create the KeyringPair for signing a dot transaction.
*
* @returns {KeyringPair} dot KeyringPair
*
* @see https://polkadot.js.org/docs/api/start/keyring
*/
protected createPolkadotPair(): KeyringPair {
const secretKey = this.keyPair.prv ? new Uint8Array(Buffer.from(this.keyPair.prv, 'hex')) : undefined;
const publicKey = new Uint8Array(Buffer.from(this.keyPair.pub, 'hex'));
return createPair({ toSS58: keyring.encodeAddress, type: TYPE }, { secretKey, publicKey });
}

/** @inheritdoc */
recordKeysFromPublicKeyInProtocolFormat(pub: string): DefaultKeys {
throw new Error('Method not implemented.');
}
/**
// https://wiki.polkadot.network/docs/learn-accounts#address-format
* Returns the address in either mainnet polkadot format (starts with 1)
* or substrate format used for westend (starts with 5)
*/
getAddress(format: DotAddressFormat): string {
let encodedAddress = this.createPolkadotPair().address;
encodedAddress = keyring.encodeAddress(encodedAddress, format as number);

/** @inheritdoc */
getAddress(): string {
throw new Error('Method not implemented.');
return encodedAddress;
}
}
17 changes: 16 additions & 1 deletion modules/sdk-coin-tao/src/tao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
VerifyTransactionOptions,
} from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
import { SubstrateCoin } from '@bitgo/abstract-substrate';
import { SubstrateCoin, Utils } from '@bitgo/abstract-substrate';
import { TaoKeyPair } from './lib';

const utils = Utils.default;

export class Tao extends SubstrateCoin {
protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
Expand Down Expand Up @@ -42,4 +45,16 @@ export class Tao extends SubstrateCoin {
parseTransaction(params: ParseTransactionOptions): Promise<ParsedTransaction> {
return {};
}

generateKeyPair(seed?: Buffer): TaoKeyPair {
const keyPair = seed ? utils.keyPairFromSeed(new Uint8Array(seed)) : new TaoKeyPair();
const keys = keyPair.getKeys();
if (!keys.prv) {
throw new Error('Missing prv in key generation.');
}
return {
pub: keys.pub,
prv: keys.prv,
};
}
}

0 comments on commit 0447326

Please sign in to comment.