Ethereum address encoding (#394)

* Ethereum address encoding

* Remove encode
This commit is contained in:
Jaco Greeff
2020-11-06 12:10:26 +01:00
committed by GitHub
parent 6356f9d0b7
commit e63b08f053
2 changed files with 52 additions and 47 deletions
+11 -6
View File
@@ -195,12 +195,17 @@ export class Keyring extends Base implements KeyringStruct {
return;
}
const address = this.encodeAddress(
isHex(json.address)
? hexToU8a(json.address)
// FIXME Just for the transition period (ignoreChecksum)
: this.decodeAddress(json.address, true)
);
// We assume anything hex that is not 32bytes (64 + 2 bytes hex) is an Ethereum-like address
// (this caters for both H160 addresses as well as full or compressed publicKeys) - in the case
// of both ecdsa and ethereum, we keep it as-is
const address = isHex(json.address) && json.address.length !== 66
? json.address
: this.encodeAddress(
isHex(json.address)
? hexToU8a(json.address)
// FIXME Just for the transition period (ignoreChecksum)
: this.decodeAddress(json.address, true)
);
const [, hexAddr] = key.split(':');
this.addresses.add(this._store, address, json);
+41 -41
View File
@@ -46,7 +46,41 @@ export default class Ledger {
this.#transport = transport;
}
private async _getApp (): Promise<SubstrateApp> {
public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerAddress> {
return this.#withApp(async (app: SubstrateApp): Promise<LedgerAddress> => {
const { address, pubKey } = await this.#wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
return {
address,
publicKey: `0x${pubKey}`
};
});
}
public async getVersion (): Promise<LedgerVersion> {
return this.#withApp(async (app: SubstrateApp): Promise<LedgerVersion> => {
const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await this.#wrapError(app.getVersion());
return {
isLocked,
isTestMode,
version: [major, minor, patch]
};
});
}
public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerSignature> {
return this.#withApp(async (app: SubstrateApp): Promise<LedgerSignature> => {
const buffer = u8aToBuffer(message);
const { signature } = await this.#wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer));
return {
signature: u8aToHex(bufferToU8a(signature))
};
});
}
#getApp = async (): Promise<SubstrateApp> => {
if (!this.#app) {
const def = transports.find(({ type }) => type === this.#transport);
@@ -58,11 +92,11 @@ export default class Ledger {
}
return this.#app;
}
};
private async _withApp <T> (fn: (app: SubstrateApp) => Promise<T>): Promise<T> {
#withApp = async <T> (fn: (app: SubstrateApp) => Promise<T>): Promise<T> => {
try {
const app = await this._getApp();
const app = await this.#getApp();
return await fn(app);
} catch (error) {
@@ -70,47 +104,13 @@ export default class Ledger {
throw error;
}
}
};
private async _wrapError <T extends ResponseBase> (promise: Promise<T>): Promise<T> {
#wrapError = async <T extends ResponseBase> (promise: Promise<T>): Promise<T> => {
const result = await promise;
assert(result.return_code === SUCCESS_CODE, result.error_message);
return result;
}
public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerAddress> {
return this._withApp(async (app: SubstrateApp): Promise<LedgerAddress> => {
const { address, pubKey } = await this._wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
return {
address,
publicKey: `0x${pubKey}`
};
});
}
public async getVersion (): Promise<LedgerVersion> {
return this._withApp(async (app: SubstrateApp): Promise<LedgerVersion> => {
const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await this._wrapError(app.getVersion());
return {
isLocked,
isTestMode,
version: [major, minor, patch]
};
});
}
public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerSignature> {
return this._withApp(async (app: SubstrateApp): Promise<LedgerSignature> => {
const buffer = u8aToBuffer(message);
const { signature } = await this._wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer));
return {
signature: u8aToHex(bufferToU8a(signature))
};
});
}
};
}