Skip to content
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"private": true,
"packageManager": "pnpm@10.6.2",
"scripts": {
"build": "turbo run build --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types",
"build": "turbo run build --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types --filter=@gridplus/xrp",
"test": "turbo run test --filter=gridplus-sdk --filter=@gridplus/btc",
"test-unit": "turbo run test-unit --filter=gridplus-sdk --filter=@gridplus/btc",
"lint": "turbo run lint --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types",
"lint:fix": "turbo run lint:fix --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types",
"typecheck": "turbo run typecheck --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types",
"lint": "turbo run lint --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types --filter=@gridplus/xrp",
"lint:fix": "turbo run lint:fix --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types --filter=@gridplus/xrp",
"typecheck": "turbo run typecheck --filter=gridplus-sdk --filter=@gridplus/chain-core --filter=@gridplus/btc --filter=@gridplus/cosmos --filter=@gridplus/evm --filter=@gridplus/solana --filter=@gridplus/types --filter=@gridplus/xrp",
"e2e": "turbo run e2e --filter=gridplus-sdk",
"docs:build": "pnpm --filter gridplus-sdk-docs run build",
"docs:start": "pnpm --filter gridplus-sdk-docs run start"
Expand Down
1 change: 1 addition & 0 deletions packages/chains/btc/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const resolvePath = (
params?: BtcGetAddressParams,
options?: BtcAdapterOptions,
): DerivationPath => {
if (params?.path) return params.path;
return buildPath(
resolvePurpose(params, options),
resolveCoinType(params, options),
Expand Down
16 changes: 1 addition & 15 deletions packages/chains/btc/src/devices/shared.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
export function compressSecp256k1Pubkey(pubkey: Uint8Array): Uint8Array {
if (pubkey.length === 33 && (pubkey[0] === 0x02 || pubkey[0] === 0x03)) {
return pubkey;
}
if (pubkey.length === 65 && pubkey[0] === 0x04) {
const x = pubkey.slice(1, 33);
const yLastByte = pubkey[64];
const prefix = yLastByte % 2 === 0 ? 0x02 : 0x03;
const out = new Uint8Array(33);
out[0] = prefix;
out.set(x, 1);
return out;
}
return pubkey;
}
export { compressSecp256k1Pubkey } from '@gridplus/chain-core';

export function normalizeBtcSignedTxHex(tx?: string): string | undefined {
if (!tx) return undefined;
Expand Down
84 changes: 84 additions & 0 deletions packages/chains/chain-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,87 @@ export function createChainRegistry<TContext = DeviceContext>(
resolve,
};
}

// ---------------------------------------------------------------------------
// Shared chain utilities
// ---------------------------------------------------------------------------

export function compressSecp256k1Pubkey(pubkey: Uint8Array): Uint8Array {
if (pubkey.length === 33 && (pubkey[0] === 0x02 || pubkey[0] === 0x03)) {
return pubkey;
}
if (pubkey.length === 65 && pubkey[0] === 0x04) {
const x = pubkey.slice(1, 33);
const yLastByte = pubkey[64];
const prefix = yLastByte % 2 === 0 ? 0x02 : 0x03;
const out = new Uint8Array(33);
out[0] = prefix;
out.set(x, 1);
return out;
}
return pubkey;
}

export function toBuffer(value: unknown): Buffer {
if (Buffer.isBuffer(value)) return value;
if (value instanceof Uint8Array) return Buffer.from(value);
if (typeof value === 'string') {
const hex = value.startsWith('0x') ? value.slice(2) : value;
return Buffer.from(hex, 'hex');
}
throw new Error('Unsupported byte input');
}

export function parseHexBytes(
value: unknown,
expectedLen?: number,
): Uint8Array {
if (typeof value === 'string') {
const hex = value.startsWith('0x') ? value.slice(2) : value;
const buf = Buffer.from(hex, 'hex');
if (
expectedLen !== undefined &&
buf.length !== expectedLen &&
buf.length < expectedLen
) {
const out = Buffer.alloc(expectedLen);
buf.copy(out, expectedLen - buf.length);
return new Uint8Array(out);
}
return new Uint8Array(buf);
}
if (Buffer.isBuffer(value)) return new Uint8Array(value);
if (value instanceof Uint8Array) return value;
throw new Error('Unsupported signature component type');
}

export function buildSigResultFromRsv(sig: {
r?: unknown;
s?: unknown;
v?: unknown;
}): {
signature: {
bytes: Uint8Array;
r?: Uint8Array;
s?: Uint8Array;
v?: bigint | number;
};
} {
const r = sig.r !== undefined ? parseHexBytes(sig.r, 32) : undefined;
const s = sig.s !== undefined ? parseHexBytes(sig.s, 32) : undefined;

let v: bigint | number | undefined;
if (typeof sig.v === 'bigint') v = sig.v;
else if (typeof sig.v === 'number') v = sig.v;
else if (typeof sig.v === 'string') v = BigInt(sig.v);
else if (Buffer.isBuffer(sig.v) || sig.v instanceof Uint8Array) {
const buf = Buffer.from(sig.v);
v = buf.length === 0 ? 0n : BigInt(`0x${buf.toString('hex')}`);
}

const bytes =
r && s
? new Uint8Array(Buffer.concat([Buffer.from(r), Buffer.from(s)]))
: new Uint8Array();
return { signature: { bytes, r, s, v } };
}
5 changes: 4 additions & 1 deletion packages/chains/chain-registry-architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ flowchart LR
CoreRegistry["createChainRegistry()\nChainPlugin + DeviceId + resolve()"]
end

subgraph Chains["@gridplus/{btc,evm,solana,cosmos}"]
subgraph Chains["@gridplus/{btc,evm,solana,cosmos,xrp}"]
Btc["btc/src/devices/lattice.ts\nlatticePlugin"]
Evm["evm/src/devices/lattice.ts\nlatticePlugin"]
Sol["solana/src/devices/lattice.ts\nlatticePlugin"]
Cos["cosmos/src/devices/lattice.ts\nlatticePlugin"]
Xrp["xrp/src/devices/lattice.ts\nlatticePlugin"]
Cadix["future: */src/devices/cadix.ts\ncadixPlugin"]
end

Expand All @@ -36,6 +37,7 @@ flowchart LR
Manifest --> Evm
Manifest --> Sol
Manifest --> Cos
Manifest --> Xrp
CustomReg --> Cadix

Registry --> Plugin["resolved plugin (chainId:device)"]
Expand Down Expand Up @@ -137,3 +139,4 @@ stateDiagram-v2
- `packages/chains/evm/src/devices/lattice.ts`
- `packages/chains/solana/src/devices/lattice.ts`
- `packages/chains/cosmos/src/devices/lattice.ts`
- `packages/chains/xrp/src/devices/lattice.ts`
18 changes: 1 addition & 17 deletions packages/chains/cosmos/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
SignResult,
Signer as CoreSigner,
} from '@gridplus/chain-core';
import { compressSecp256k1Pubkey } from '@gridplus/chain-core';
import { bech32 } from 'bech32';
import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
Expand Down Expand Up @@ -81,23 +82,6 @@ const buildPath = (
];
};

function compressSecp256k1Pubkey(pubkey: Uint8Array): Uint8Array {
if (pubkey.length === 33 && (pubkey[0] === 0x02 || pubkey[0] === 0x03)) {
return pubkey;
}
if (pubkey.length === 65 && pubkey[0] === 0x04) {
const x = pubkey.slice(1, 33);
const yLastByte = pubkey[64];
const prefix = yLastByte % 2 === 0 ? 0x02 : 0x03;
const out = new Uint8Array(33);
out[0] = prefix;
out.set(x, 1);
return out;
}
// Unknown format, return as-is.
return pubkey;
}

const pubkeyToBech32Address = (pubkeyCompressed: Uint8Array, hrp: string) => {
const digest = ripemd160(sha256(pubkeyCompressed));
const words = bech32.toWords(digest);
Expand Down
74 changes: 5 additions & 69 deletions packages/chains/cosmos/src/devices/shared.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,5 @@
export function compressSecp256k1Pubkey(pubkey: Uint8Array): Uint8Array {
if (pubkey.length === 33 && (pubkey[0] === 0x02 || pubkey[0] === 0x03)) {
return pubkey;
}
if (pubkey.length === 65 && pubkey[0] === 0x04) {
const x = pubkey.slice(1, 33);
const yLastByte = pubkey[64];
const prefix = yLastByte % 2 === 0 ? 0x02 : 0x03;
const out = new Uint8Array(33);
out[0] = prefix;
out.set(x, 1);
return out;
}
return pubkey;
}

export function parseHexBytes(
value: unknown,
expectedLen?: number,
): Uint8Array {
if (typeof value === 'string') {
const hex = value.startsWith('0x') ? value.slice(2) : value;
const buf = Buffer.from(hex, 'hex');
if (
expectedLen !== undefined &&
buf.length !== expectedLen &&
buf.length < expectedLen
) {
const out = Buffer.alloc(expectedLen);
buf.copy(out, expectedLen - buf.length);
return new Uint8Array(out);
}
return new Uint8Array(buf);
}
if (Buffer.isBuffer(value)) return new Uint8Array(value);
if (value instanceof Uint8Array) return value;
throw new Error('Unsupported signature component type');
}

export function buildSigResultFromRsv(sig: {
r?: unknown;
s?: unknown;
v?: unknown;
}): {
signature: {
bytes: Uint8Array;
r?: Uint8Array;
s?: Uint8Array;
v?: bigint | number;
};
} {
const r = sig.r !== undefined ? parseHexBytes(sig.r, 32) : undefined;
const s = sig.s !== undefined ? parseHexBytes(sig.s, 32) : undefined;

let v: bigint | number | undefined;
if (typeof sig.v === 'bigint') v = sig.v;
else if (typeof sig.v === 'number') v = sig.v;
else if (typeof sig.v === 'string') v = BigInt(sig.v);
else if (Buffer.isBuffer(sig.v) || sig.v instanceof Uint8Array) {
const buf = Buffer.from(sig.v);
v = buf.length === 0 ? 0n : BigInt(`0x${buf.toString('hex')}`);
}

const bytes =
r && s
? new Uint8Array(Buffer.concat([Buffer.from(r), Buffer.from(s)]))
: new Uint8Array();
return { signature: { bytes, r, s, v } };
}
export {
compressSecp256k1Pubkey,
parseHexBytes,
buildSigResultFromRsv,
} from '@gridplus/chain-core';
85 changes: 6 additions & 79 deletions packages/chains/evm/src/devices/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,9 @@ export function isHexString(value: unknown): value is Hex {
return typeof value === 'string' && value.startsWith('0x');
}

export function toBuffer(value: unknown): Buffer {
if (Buffer.isBuffer(value)) return value;
if (value instanceof Uint8Array) return Buffer.from(value);
if (typeof value === 'string') {
const hex = value.startsWith('0x') ? value.slice(2) : value;
return Buffer.from(hex, 'hex');
}
throw new Error('Unsupported byte input');
}

export function compressSecp256k1Pubkey(pubkey: Uint8Array): Uint8Array {
if (pubkey.length === 33 && (pubkey[0] === 0x02 || pubkey[0] === 0x03)) {
return pubkey;
}
if (pubkey.length === 65 && pubkey[0] === 0x04) {
const x = pubkey.slice(1, 33);
const yLastByte = pubkey[64];
const prefix = yLastByte % 2 === 0 ? 0x02 : 0x03;
const out = new Uint8Array(33);
out[0] = prefix;
out.set(x, 1);
return out;
}
return pubkey;
}

export function parseHexBytes(
value: unknown,
expectedLen?: number,
): Uint8Array {
if (typeof value === 'string') {
const hex = value.startsWith('0x') ? value.slice(2) : value;
const buf = Buffer.from(hex, 'hex');
if (
expectedLen !== undefined &&
buf.length !== expectedLen &&
buf.length < expectedLen
) {
const out = Buffer.alloc(expectedLen);
buf.copy(out, expectedLen - buf.length);
return new Uint8Array(out);
}
return new Uint8Array(buf);
}
if (Buffer.isBuffer(value)) return new Uint8Array(value);
if (value instanceof Uint8Array) return value;
throw new Error('Unsupported signature component type');
}

export function buildSigResultFromRsv(sig: {
r?: unknown;
s?: unknown;
v?: unknown;
}): {
signature: {
bytes: Uint8Array;
r?: Uint8Array;
s?: Uint8Array;
v?: bigint | number;
};
} {
const r = sig.r !== undefined ? parseHexBytes(sig.r, 32) : undefined;
const s = sig.s !== undefined ? parseHexBytes(sig.s, 32) : undefined;

let v: bigint | number | undefined;
if (typeof sig.v === 'bigint') v = sig.v;
else if (typeof sig.v === 'number') v = sig.v;
else if (typeof sig.v === 'string') v = BigInt(sig.v);
else if (Buffer.isBuffer(sig.v) || sig.v instanceof Uint8Array) {
const buf = Buffer.from(sig.v);
v = buf.length === 0 ? 0n : BigInt(`0x${buf.toString('hex')}`);
}

const bytes =
r && s
? new Uint8Array(Buffer.concat([Buffer.from(r), Buffer.from(s)]))
: new Uint8Array();
return { signature: { bytes, r, s, v } };
}
export {
compressSecp256k1Pubkey,
toBuffer,
parseHexBytes,
buildSigResultFromRsv,
} from '@gridplus/chain-core';
Loading
Loading