Skip to content

Commit

Permalink
support metadata and remove scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosala authored and emmanuelm41 committed Apr 17, 2024
1 parent a31bc1a commit f626e9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
5 changes: 0 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ export const enum P1_VALUES {
SHOW_ADDRESS_IN_DEVICE = 0x01,
}

export const enum SCHEME {
ED25519 = 0x00,
SR25519 = 0x01,
}

export const enum ERROR_CODE {
NoError = 0x9000,
}
Expand Down
62 changes: 32 additions & 30 deletions src/substrate_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
type ResponseAllowlistPubKey,
type ResponseSign,
type ResponseVersion,
SCHEME,
type INS_SIGN,
} from "./common";

Expand Down Expand Up @@ -75,11 +74,27 @@ export class SubstrateApp {
return chunks;
}

static signGetChunks(slip0044: number, account: number, change: number, addressIndex: number, message: Buffer) {
const chunks = [];
static signGetChunks(
slip0044: number,
account: number,
change: number,
addressIndex: number,
blob: Buffer,
metadata?: Buffer,
) {
const chunks: Buffer[] = [];
const bip44Path = SubstrateApp.serializePath(slip0044, account, change, addressIndex);
chunks.push(bip44Path);
chunks.push(...SubstrateApp.GetChunks(message));
if (metadata == null) {
chunks.push(bip44Path);
chunks.push(...SubstrateApp.GetChunks(blob));
} else {
const blobLen = Buffer.alloc(4);
blobLen.writeUInt32LE(blob.length);
const firstChunk = Buffer.concat([bip44Path, blobLen]);
chunks.push(firstChunk);
chunks.push(...SubstrateApp.GetChunks(Buffer.concat([blob, metadata])));
}

return chunks;
}

Expand Down Expand Up @@ -146,17 +161,13 @@ export class SubstrateApp {
change: number,
addressIndex: number,
requireConfirmation = false,
scheme = SCHEME.ED25519,
): Promise<ResponseAddress> {
const bip44Path = SubstrateApp.serializePath(this.slip0044, account, change, addressIndex);

let p1 = 0;
if (requireConfirmation) p1 = 1;

let p2 = 0;
if (!isNaN(scheme)) p2 = scheme;

return await this.transport.send(this.cla, INS.GET_ADDR, p1, p2, bip44Path).then((response) => {
return await this.transport.send(this.cla, INS.GET_ADDR, p1, 0, bip44Path).then((response) => {
const errorCodeData = response.subarray(-2);
const errorCode = errorCodeData[0] * 256 + errorCodeData[1];

Expand All @@ -169,13 +180,7 @@ export class SubstrateApp {
}, processErrorResponse);
}

async signSendChunk(
chunkIdx: number,
chunkNum: number,
chunk: Buffer,
scheme = SCHEME.ED25519,
ins: INS_SIGN = INS.SIGN,
) {
async signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, ins: INS_SIGN = INS.SIGN) {
let payloadType = PAYLOAD_TYPE.ADD;
if (chunkIdx === 1) {
payloadType = PAYLOAD_TYPE.INIT;
Expand All @@ -184,11 +189,8 @@ export class SubstrateApp {
payloadType = PAYLOAD_TYPE.LAST;
}

let p2 = 0;
if (!isNaN(scheme)) p2 = scheme;

return await this.transport
.send(this.cla, ins, payloadType, p2, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
.send(this.cla, ins, payloadType, 0, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
.then((response) => {
const errorCodeData = response.subarray(-2);
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
Expand All @@ -213,15 +215,15 @@ export class SubstrateApp {
account: number,
change: number,
addressIndex: number,
message: Buffer,
ins: INS_SIGN,
scheme = SCHEME.ED25519,
blob: Buffer,
metadata?: Buffer,
): Promise<ResponseSign> {
const chunks = SubstrateApp.signGetChunks(this.slip0044, account, change, addressIndex, message);
return await this.signSendChunk(1, chunks.length, chunks[0], scheme, ins).then(async () => {
const chunks = SubstrateApp.signGetChunks(this.slip0044, account, change, addressIndex, blob, metadata);
return await this.signSendChunk(1, chunks.length, chunks[0], ins).then(async () => {
let result;
for (let i = 1; i < chunks.length; i += 1) {
result = await this.signSendChunk(1 + i, chunks.length, chunks[i], scheme, ins);
result = await this.signSendChunk(1 + i, chunks.length, chunks[i], ins);
if (result.return_code !== ERROR_CODE.NoError) {
break;
}
Expand All @@ -235,12 +237,12 @@ export class SubstrateApp {
}, processErrorResponse);
}

async sign(account: number, change: number, addressIndex: number, message: Buffer, scheme = SCHEME.ED25519) {
return await this.signImpl(account, change, addressIndex, message, INS.SIGN, scheme);
async sign(account: number, change: number, addressIndex: number, blob: Buffer, metadata: Buffer) {
return await this.signImpl(account, change, addressIndex, INS.SIGN, blob, metadata);
}

async signRaw(account: number, change: number, addressIndex: number, message: Buffer, scheme = SCHEME.ED25519) {
return await this.signImpl(account, change, addressIndex, message, INS.SIGN_RAW, scheme);
async signRaw(account: number, change: number, addressIndex: number, blob: Buffer) {
return await this.signImpl(account, change, addressIndex, INS.SIGN_RAW, blob);
}

/// Allow list related commands. They are NOT available on all apps
Expand Down

0 comments on commit f626e9c

Please sign in to comment.